Java JFrame 更新我的屏幕很慢,直到我按下一个键
Java JFrame updates my screen slow untill I press a key
我正在编写一个简单的游戏,并且在 JFrame 中使用 java。在我将平台移动到底部之前,我的输出看起来非常糟糕,看起来它没有足够的更新速率,直到我按下按钮更新并且当我释放键时它做同样的事情。所以这是包含 2 类:
的完整代码
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;
}
}
我想知道如何让小球的运动始终保持在按键状态下的速度。
首先不需要在ActionListener中启动Timer。您已经在构造函数中启动了 Timer。
JFrame updates my screen slow untill I press a key
我运行代码的时候完全没有动画。
让我们做一些基本的调试:
public void actionPerformed(ActionEvent e) {
System.out.println(Play); // basic debugging
你看到了什么?
那么当你按下箭头键时会发生什么?
为什么只在 moveRight/moveLeft 方法中设置 play 变量?
我正在编写一个简单的游戏,并且在 JFrame 中使用 java。在我将平台移动到底部之前,我的输出看起来非常糟糕,看起来它没有足够的更新速率,直到我按下按钮更新并且当我释放键时它做同样的事情。所以这是包含 2 类:
的完整代码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;
}
}
我想知道如何让小球的运动始终保持在按键状态下的速度。
首先不需要在ActionListener中启动Timer。您已经在构造函数中启动了 Timer。
JFrame updates my screen slow untill I press a key
我运行代码的时候完全没有动画。
让我们做一些基本的调试:
public void actionPerformed(ActionEvent e) {
System.out.println(Play); // basic debugging
你看到了什么?
那么当你按下箭头键时会发生什么?
为什么只在 moveRight/moveLeft 方法中设置 play 变量?