简体   繁体   中英

Java: Repaint() method not calling paintComponent

I'm making a brick breaker game and I'm stuck on getting the repaint() method working. I've used the debugger and it's not calling the paintComponent method. I am trying to get the paddle to move left or right when the left/right arrow keys are pressed. Hence I am trying to repaint the graphics, but have no luck in it working and can't figure out what I'm doing wrong.

My main class:

public class BrickBreakerGameApp {
    public static void main(String[] args) {
        int pw = 500;
        int ph = 900;
        GamePanel gPanel = new GamePanel(pw,ph);

        GameFrame gFrame = new GameFrame();
       
        gFrame.getContentPane().add(gPanel); //add game panel to frame
        
        gFrame.addKeyListener(new GamePanel(pw,ph)); //adds the key listener for the game panel frame
        
        gFrame.pack();
        gFrame.setVisible(true); //make frame visible
    }
}

My frame class:

import java.awt.Dimension;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class GameFrame extends JFrame {
    //Global Variables
    public int frameWidth = 500;
    public int frameHeight = 800;
    
    //create constructor
    GameFrame () {
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //so app closes properly
        this.setPreferredSize(new Dimension(frameWidth, frameHeight)); //set frame size
        this.setTitle("Brick Breaker Game");

        ImageIcon icon = new ImageIcon("res/brick-breaker-logo.jpg"); //create image icon
        this.setIconImage(icon.getImage()); //update frame icon

        this.pack();
    }
}


My panel class:

import java.awt.Color;
import java.awt.event.*;
import javax.swing.JPanel;
import java.awt.*;

/**
 * This class handles the game panel as a whole
 */

public class GamePanel extends JPanel implements ActionListener, KeyListener {

    // Global Variables
    private Ball ball;
    private Paddle paddle;
    private GameFrame gameFrame;
    private int width;
    private int height;

    // create a constructor
    GamePanel (int gamePanelWidth, int gamePanelHeight) {
    

        this.setWidth(gamePanelWidth);
        this.setHeight(gamePanelHeight);
        
        
        
        initialiseGame();
        this.isVisible();
        
    }


    private void initialiseGame() {
        ball = new Ball(10, 520, 30, 30); //create the ball object
        paddle = new Paddle(this, 50, 550, 100, 10); //creates paddle object
        
    }

    
    // paint all the elements in
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        // background
        g.setColor(Color.black);
        g.fillRect(0, 0, getWidth(), getHeight());

        // paddle
        g.setColor(Color.CYAN);
        g.fillRect(paddle.getPaddleX(), paddle.getPaddleY(), paddle.getPaddleWidth(), paddle.getPaddleHeight());

        // ball
        g.setColor(Color.MAGENTA);
        g.fillOval(ball.getBallX(), ball.getBallY(), ball.getBallWidth(), ball.getBallHeight());

    }

    //add any unimplemented methods because we are using an interface
    @Override
    public void actionPerformed(ActionEvent e) {
        
    }

    @Override
    public void keyPressed(KeyEvent e) {
        

        if(e.getKeyCode() == KeyEvent.VK_LEFT) {
            paddle.moveLeft();
            
    
        }
        if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
            paddle.moveRight();
            
        }
        this.repaint();
        
        
    }

 
    
    


    @Override
    public void keyTyped(KeyEvent e) {  
    }
    
    @Override
    public void keyReleased(KeyEvent e) {   
    }
    
    //create get and set methods for all paddle attributes
    public int getWidth() {
        return width;
    }

    public void setWidth(int pWidth) {
        this.width = pWidth;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int pHeight) {
        this.height = pHeight;
    }

    
}


My Paddle class:

public class Paddle {
    //Global Variables
    private int paddleWidth;
    private int paddleHeight;
    private int paddleX; //paddle x position
    private int paddleY;
    private GamePanel gamePanel;
    
    //create paddle constructor
    Paddle() {

    }

    //create a paddle constructor based off parameters
    Paddle(GamePanel gPanel, int pX, int pY, int pWidth, int pHeight) {
        //set the values
        this.setPaddleWidth(pWidth);
        this.setPaddleHeight(pHeight);
        this.setPaddleX(pX);
        this.setPaddleY(pY);
        this.setGamePanel(gPanel);

    }

    //create get and set methods for all paddle attributes
    public int getPaddleWidth() {
        return paddleWidth;
    }

    public void setPaddleWidth(int pWidth) {
        this.paddleWidth = pWidth;
    }

    public int getPaddleHeight() {
        return paddleHeight;
    }

    public void setPaddleHeight(int pHeight) {
        this.paddleHeight = pHeight;
    }

    public int getPaddleX() {
        return paddleX;
    }

    public void setPaddleX(int pX) {
        this.paddleX = pX;
    }

    public int getPaddleY() {
        return paddleY;
    }

    public void setPaddleY(int pY) {
        this.paddleY = pY;
    }

    
    public GamePanel getGamePanel() {
        return gamePanel;
    }

    public void setGamePanel(GamePanel gPanel) {
        this.gamePanel = gPanel;
    }

    //move the paddle left if it is not already positoned at 0 (far left)
    public void moveLeft() {
        try {
            if(getPaddleX() <= 0) {
                setPaddleX(0);
                System.out.println("less than 0, x: " + getPaddleX());
                
            } 
            else if (getPaddleX() > 0) {
                setPaddleX(getPaddleX()-10); //to move paddle left -10
          //      gamePanel.repaint(getPaddleX()+10, getPaddleY(), getPaddleWidth(), getPaddleHeight()); //repaint old position
          //      gamePanel.repaint(getPaddleX(), getPaddleY(), getPaddleWidth(), getPaddleHeight()); //repaint new position
                System.out.println("left, x: " + getPaddleX());
              
            }
            
            
        }
        catch (Exception e) {
            
         }
        

    }

    

    //move the paddle right if it is not already positioned to the far right of the panel
    public void moveRight() {
        
        if(getPaddleX() >= gamePanel.getWidth() - getPaddleWidth()) { //dont move the paddle if it is on the right edge of the panel
            setPaddleX(gamePanel.getWidth() - getPaddleWidth());
            System.out.println("right1, x:" + getPaddleX());
            
        }
        else if ((getPaddleX()+getPaddleWidth()) <= gamePanel.getWidth()){ //if the paddle is within the panel bounds
            setPaddleX(getPaddleX() + 10); //to move paddle right +10
            System.out.println("right, x:" + getPaddleX());
        }
        
    }
}

After a little bite of debugging/testing I found out that in your main method:

public class BrickBreakerGameApp {
    public static void main(String[] args) {
        int pw = 500;
        int ph = 900;
        GamePanel gPanel = new GamePanel(pw,ph);

        GameFrame gFrame = new GameFrame();
       
        gFrame.getContentPane().add(gPanel); //add game panel to frame
        
        gFrame.addKeyListener(new GamePanel(pw,ph)); //adds the key listener for the game panel frame
        
        gFrame.pack();
        gFrame.setVisible(true); //make frame visible
    }
}

to the gFrame.addKeyListener method call do not add a new GamePanel but the one that you created already, namely:

gFrame.addKeyListener(gPanel); //adds the key listener for the game panel frame

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM