java:repaint()方法未调用paintcomponent

ntjbwcob  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(376)

我正在做一个破砖游戏,我一直在努力让repaint()方法工作。我使用了调试器,它没有调用paintcomponent方法。当按下左/右箭头键时,我正试图使拨杆向左或向右移动。因此,我试图重新绘制的图形,但没有运气在它的工作,不能找出我做错了什么。
我的主要课程:

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
    }
}

我的框架类:

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();
    }
}

我的小组课:

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;
    }

}

我的划桨课:

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());
        }

    }
}
snvhrwxg

snvhrwxg1#

经过一点调试/测试,我发现在您的主要方法中:

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
    }
}

gFrame.addKeyListener 方法调用不添加 new GamePanel 但你已经创造了一个,即:

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

相关问题