SomeoneNew
SomeoneNew

Reputation: 91

Why the panel is not showing up?

So I have a GUI with a button for starting the game. The menuPanel represents the main-panel where everything should appear. The actual project is more complex, but here i summarised the problem. There is the main:

package bbGame;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;



public class Main {

public static void main(String[] args) {
    JFrame gameFrame = new JFrame();
    gameFrame.setSize(700, 600);
    gameFrame.setVisible(true);
    JPanel menuPanel = new JPanel();
    menuPanel.setLayout(null);
    menuPanel.setBounds(0, 0, 700, 600);
    menuPanel.setBackground(new Color(243, 207, 252));
    menuPanel.setVisible(true);


    gameFrame.add(menuPanel);

    JButton startBricksBreakerButton = new JButton();
    startBricksBreakerButton.setText("START BRICKS BREAKER");
    startBricksBreakerButton.setLayout(null);
    startBricksBreakerButton.setBounds(250, 200, 180, 50);
    startBricksBreakerButton.setBackground(new Color(37, 242, 10));
    startBricksBreakerButton.setVisible(true);
    menuPanel.add(startBricksBreakerButton);
    startBricksBreakerButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            menuPanel.removeAll();
            BricksBreakerGame bbGame=new BricksBreakerGame();
            menuPanel.add(bbGame);
            bbGame.setVisible(true);
            menuPanel.repaint();

        }
    });

}}

There is the game. It's not done yet,but it's displaying something(no errors).

package bbGame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JPanel;
import javax.swing.Timer;

public class BricksBreakerGame extends JPanel implements KeyListener,    ActionListener {

private boolean play = false;
private int score = 0;

private int totalBricks = 21;

private Timer time;
private int speed = 8;

private int playerX = 310;

private int ballposX = 120;
private int ballposY = 350;
private int ballXdir = -1;
private int ballYdir = -2;

public BricksBreakerGame() {
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    // timer = new Timer(delay, this);
    // timer.start();
}

public void paint(Graphics g) {
    // background
    g.setColor(Color.black);
    g.fillRect(1, 1, 692, 592);
    // borders
    g.setColor(Color.yellow);
    g.fillRect(0, 0, 3, 592);
    g.fillRect(0, 0, 692, 3);
    g.fillRect(691, 0, 3, 592);

    // the paddle
    g.setColor(Color.green);
    g.fillRect(playerX, 550, 100, 8);

    // the ball
    g.setColor(Color.yellow);
    g.fillRect(ballposX, ballposY, 20, 20);
}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void keyPressed(KeyEvent e) {
    // TODO Auto-generated method stub
    if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
        if (playerX >= 600) {
            playerX = 600;
        } else {
            moveRight();
        }
    }
    if (e.getKeyCode() == KeyEvent.VK_LEFT) {
        if (playerX < 100) {
            playerX = 100;
        } else {
            moveLeft();
        }
    }

}

private void moveRight() {
    play = true;
    playerX += 20;

}

private void moveLeft() {
    play = true;
    playerX -= 20;

}

@Override
public void keyReleased(KeyEvent arg0) {
}

@Override
public void keyTyped(KeyEvent arg0) {
}}

When i click the startBricksBreaker-button, the panel generated by the BricksBreakerGame class it's not showing up. What is wrong? I don't have errors, but the game-panel it's not showing up. I assume that there is a problem with the actionPerformed method, but i don't know what is wrong there...I'm quite new to Java...

Upvotes: 1

Views: 472

Answers (2)

GAlexMES
GAlexMES

Reputation: 355

I guess, your problem is, that you never add something to your BricksBrakerGame. Try to add the following to the BricksBrakerGame Constructor:

this.setSize(100, 100);
this.setBackground(Color.BLACK);

You should see a black JPanel in the upper left corner.

Or you simply call your paint() method and give it a Graphic to paint ;)

Upvotes: 0

davidxxx
davidxxx

Reputation: 131326

1) menuPanel.removeAll(). Here you remove all components of the panel. I think your should rather remove the menu panel of the JFrame in order to add then the new BricksBreakerGame inside. You should do : gameFrame.remove(menuPanel).

2) menuPanel.add(bbGame). Here you add the panel to the panel. Why ? You should rather add the new panel to the exiting JFrame as explained above: gameFrame.add(bbGame)

2) menuPanel.repaint() should not be required. You should rather call revalidate() and besides you should call it on the more high level (the JFrame) container and not on the JPanel:

 gameFrame.revalidate()

3) bbGame.setVisible(true) is not required since the JFrame is already visible.

Try that :

    @Override
    public void actionPerformed(ActionEvent e) {
        gameFrame.remove(menuPanel);
        BricksBreakerGame bbGame=new BricksBreakerGame();
        gameFrame.add(bbGame);
        gameFrame.revalidate();
    }

Upvotes: 1

Related Questions