Coming soon - Get a detailed view of why an account is flagged as spam!
view details
1
[Java] paintComponent not drawing in my JFrame
Author Summary
pds12345 is in Java
Post Body

I am building a breakout game. The one with the paddle and the bricks at the top and you try to break all the bricks. In my main class I have two inner classes, one to draw my GUI (Buttons to start, pause, quit ect.) and the other to draw the graphics of the game. The issue I am having is when I try to add both of these to my JFrame my graphics class does not appear, I only have my GUI showing up.

Any help in pinpointing this issue would be greatly appreciated!

private static int score, lives;
private static Timer t;
private static JLabel scoreLabel, livesLabel;
private static GraphicsPanel p1;
private static GUIPanel p2;

public static void main(String[] args){
    JFrame frame = new JFrame("");
    frame.setSize(1400, 825);
    p1 = new GraphicsPanel();
    p2 = new GUIPanel();
    frame.add(p1);
    frame.add(p2);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
}

This is my instance data and main method. I feel I shouldn't be posting ton of code but since I am unsure of where this problem is, I will post my inner class for my graphics.

public static class GraphicsPanel extends JPanel implements   KeyListener, ActionListener{
    private Brick[][] bricks;
    private Ball ball;
    private Paddle paddle;

    public GraphicsPanel(){
        addKeyListener(this);
        restart();
    }

    public void restart() {
        paddle = new Paddle(40, 1000);
        ball = new Ball(40, 25, 1000, 800);
        //Initialize each brick at proper cords
        bricks = new Brick[5][24];

        for (int x=0; x < 5; x  ){
            for (int y=0; y < 24; y  ){
                bricks[x][y]=new Brick((y 1)*40, (x 1)*10, (6-(x 1))*10);
            }
        }
        //Initialize score to 0 and lives to 3
        score = 0;
        lives = 3;
        //scoreLabel, livesLabel
        scoreLabel = new JLabel("Score: "   score);
        livesLabel = new JLabel("Lives: "   lives);
        //Initialize Timer
        t = new Timer(10, this);

    }
    //KeyListener methods
    public void keyPressed(KeyEvent e){
        if (e.getKeyCode() == KeyEvent.VK_LEFT){
            paddle.change(-2);
        }
        else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            paddle.change(2);
        }
        else if (e.getKeyCode() == KeyEvent.VK_DOWN){
            paddle.change(0);
        }
    }
    public void keyReleased(KeyEvent e){}
    public void keyTyped(KeyEvent e){}
    //Action Performed
    public void actionPerformed(ActionEvent e){
        //Timer ActionEvent
        if (e.getSource() == t){
            ball.move();
            paddle.move();
            if (ball.getY() > 800){ //Has the ball gone to the lowerborder?
                lives--;
                livesLabel.setText("Lives: "   lives);
                if (lives == 0)
                    t.stop();
                ball = new Ball(40, 25, 1000, 800);
            }
            //Test for collision from both paddle and bricks
            paddle.collide(ball);
            for (int x=0; x<5; x  ){
                for (int y=0; y<24; y  ){
                    score  = bricks[x][y].collides(ball);
                    scoreLabel.setText("Score: "   score);
                }
            }
            repaint();
        }
    }

Author
Account Strength
100%
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
8,864
Link Karma
399
Comment Karma
8,066
Profile updated: 2 days ago
Posts updated: 8 months ago

Subreddit

Post Details

Location
We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
10 years ago