javajframe缓慢更新屏幕直到我按下一个键

lf5gs5x2  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(362)

我正在写一个简单的游戏,我正在jframe中使用java。我的输出看起来非常糟糕,直到我移动底部的平台,它看起来没有足够的更新率,直到我按下按钮更新,当我释放键,它做同样的事情。这是包含两个类的完整代码:

public class BrickBreaker {
    private static final String TITLE = "break ball";
    private static final int X = 200, Y = 200, WIDTH = 700, HEIGHT = 600;

    public static void main(String[] arg){
        JFrame window = new JFrame();
        GamePlay gp = new GamePlay();
        window.setBounds(X,Y,WIDTH,HEIGHT);
        window.setTitle(TITLE);
        window.setResizable(false);
        window.setVisible(true);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.add(gp);

    }
}

public class GamePlay extends JPanel implements KeyListener, ActionListener {

    private boolean play = false;
    private int score = 0, totalBricks = 21, delay = 10;
    private int playerX = 310;
    private int ballX = 120, ballY = 350, ballDirX = -1, ballDirY = -2;
    private final Timer TIMER;

    public GamePlay(){
        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,700,592);

        //Borders
        g.setColor(Color.YELLOW);
        g.fillRect(0,0,3,592);
        g.fillRect(697,0,3,592);
        g.fillRect(0,0,697,3);

        //Paddle
        g.setColor(Color.GREEN);
        g.fillRect(playerX, 550, 100, 8);

        //Ball
        g.setColor(Color.YELLOW);
        g.fillOval(ballX, ballY, 20, 20);

        g.dispose();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        TIMER.start();
        if (play){
            TIMER.start();
            ballX += ballDirX;
            ballY += ballDirY;

            if(ballX <= 0 || ballX >= 700){
                ballDirX = - ballDirX;
            }

            if(ballY <= 0 || ballY >= 600){
                ballDirY = -ballDirY;
            }
        }

        repaint();
    }

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT){
            if(playerX >= 590){
                playerX = 590;
            }else{
                moveRight();
            }
        }else if(e.getKeyCode() == KeyEvent.VK_LEFT){
            if(playerX <= 10){
                playerX = 10;
            }else{
                moveLeft();
            }
        }
    }

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

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

}

我想知道如何使球的运动始终处于平稳的按键状态。

d5vmydt9

d5vmydt91#

首先,没有必要在actionlistener中启动计时器。您已经在构造函数中启动了计时器。
jframe更新我的屏幕慢,直到我按下一个键
运行代码时根本没有动画。
让我们做一些基本的调试:

public void actionPerformed(ActionEvent e) {
    System.out.println(Play); // basic debugging

你看到了什么?
当你按箭头键时会发生什么?
为什么只在moveright/moveleft方法中设置play变量?

相关问题