对象和计时器(For 循环)

Objects and Timers (For Loops)

这是一个学校项目。我正在尝试进行碰撞游戏,目标是躲避球。我目前正在努力尝试每 30 秒左右添加一个球,但遇到了问题。我已经尝试了 for 循环和计时器来解决这部分(在我有 for 循环的代码中)。我反复得到相同的结果:球仍然在那里但看不见。如果有人知道如何解决此问题或可能有效的其他方法,请提供帮助。谢谢!

import java.awt.*;
import java.lang.*;
import java.awt.event.KeyEvent;
import java.util.Formatter;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Polygon;
import java.awt.geom.Area;
public class  Man extends JComponent implements KeyListener {

private static final int BOX_WIDTH = 640;
private static final int BOX_HEIGHT = 480;
private float ballSpeedX3 = 7;
private float ballSpeedY3 = 7;
private double ball3Radius = 20;
private double ball3X = 320 ;
private double ball3Y = 120 ;
private float ballSpeedX4 = -10;
private float ballSpeedY4 = 10;
private double ball4Radius = 15;
private double ball4X = 600 ;
private double ball4Y = 300 ;
private float ballSpeedX = 0;
private float ballSpeedY = 0;
private double ballRadius = 20;
private double ballX = 120;
private double ballY = 140;
private float ballSpeed1X = 10;
private float ballSpeed1Y = -10;
private double ballRadius1 = 20;
private double ball1X = 320;
private double ball1Y = 340;
private float ballSpeed2X = -3;
private float ballSpeed2Y = -3;
private double ballRadius2 = 50;
private double ball2X = 50;
private double ball2Y = 400;

private static final int UPDATE_RATE = 30;

public Man() {
    this.setPreferredSize(new Dimension(BOX_WIDTH, BOX_HEIGHT));
    Thread gameThread = new Thread() {
        public void run() {
while(true){             

              if ( Math.sqrt(    (Math.pow((ballX- ball1X), 2))    +    Math.pow((ballY-ball1Y), 2)) <= (ballRadius1 + ballRadius)) {
    System.exit(0);}
              if ( Math.sqrt(    (Math.pow((ball4X- ballX), 2))    +    Math.pow((ball4Y-ballY), 2)) <= (ball4Radius + ballRadius)) {
                    System.exit(0);}
              if ( Math.sqrt(    (Math.pow((ball2X- ballX), 2))    +    Math.pow((ball2Y-ballY), 2)) <= (ballRadius2 + ballRadius)) {
                System.exit(0);}        

                  ball4X += ballSpeedX4;
                  ball4Y += ballSpeedY4;
                  if (ball4X - ball4Radius < 0) {
                      ballSpeedX4 = -ballSpeedX4;
                      ball4X = ball4Radius;
                  } else if (ball4X + ball4Radius > BOX_WIDTH) {
                      ballSpeedX4 = -ballSpeedX4;
                      ball4X = BOX_WIDTH - ball4Radius;
                  }
                  if (ball4Y - ball4Radius < 0) {
                      ballSpeedY4 = -ballSpeedY4;
                      ball4Y = ball4Radius;
                  } else if (ball4Y + ball4Radius > BOX_HEIGHT) {
                      ballSpeedY4 = -ballSpeedY4;
                      ball4Y = BOX_HEIGHT - ball4Radius;
                  }

              if ( Math.sqrt(    (Math.pow((ball3X- ballX), 2))    +    Math.pow((ball3Y-ballY), 2)) <= (ball3Radius + ballRadius)) {
                System.exit(0);}


                ball3X += ballSpeedX3;
                ball3Y += ballSpeedY3;
                if (ball3X - ball3Radius < 0) {
                    ballSpeedX3 = -ballSpeedX3;
                    ball3X = ball3Radius;
                } else if (ball3X + ball3Radius > BOX_WIDTH) {
                    ballSpeedX3 = -ballSpeedX3;
                    ball3X = BOX_WIDTH - ball3Radius;
                }
                if (ball3Y - ball3Radius < 0) {
                    ballSpeedY3 = -ballSpeedY3;
                    ball3Y = ball3Radius;
                } else if (ball3Y + ball3Radius > BOX_HEIGHT) {
                    ballSpeedY3 = -ballSpeedY3;
                    ball3Y = BOX_HEIGHT - ball3Radius;
                }

                ballX += ballSpeedX;

                ballY += ballSpeedY;


                if (ballX - ballRadius < 0) {

                    ballX = ballRadius;
                } else if (ballX + ballRadius > BOX_WIDTH) {

                    ballX = BOX_WIDTH - ballRadius;
                }

                if (ballY - ballRadius < 0) {

                    ballY = ballRadius;
                } else if (ballY + ballRadius > BOX_HEIGHT) {

                    ballY = BOX_HEIGHT - ballRadius;
                }

                ball1X += ballSpeed1X;
                ball1Y += ballSpeed1Y;
                if (ball1X - ballRadius1 < 0) {
                    ballSpeed1X = -ballSpeed1X;
                    ball1X = ballRadius1;
                } else if (ball1X + ballRadius1 > BOX_WIDTH) {
                    ballSpeed1X = -ballSpeed1X;
                    ball1X = BOX_WIDTH - ballRadius1;
                }

                if (ball1Y - ballRadius1 < 0) {
                    ballSpeed1Y = -ballSpeed1Y;
                    ball1Y = ballRadius1;
                } else if (ball1Y + ballRadius1 > BOX_HEIGHT) {
                    ballSpeed1Y = -ballSpeed1Y;
                    ball1Y = BOX_HEIGHT - ballRadius1;
                }
                ball2X += ballSpeed2X;
                ball2Y += ballSpeed2Y;
                if (ball2X - ballRadius2 < 0) {
                    ballSpeed2X = -ballSpeed2X;
                    ball2X = ballRadius2;
                } else if (ball2X + ballRadius2 > BOX_WIDTH) {
                    ballSpeed2X = -ballSpeed2X;
                    ball2X = BOX_WIDTH - ballRadius2;
                }

                if (ball2Y - ballRadius2 < 0) {
                    ballSpeed2Y = -ballSpeed2Y;
                    ball2Y = ballRadius2;
                } else if (ball2Y + ballRadius2 > BOX_HEIGHT) {
                    ballSpeed2Y = -ballSpeed2Y;
                    ball2Y = BOX_HEIGHT - ballRadius2;
                }

                repaint();
                try {
                    Thread.sleep(1000 / UPDATE_RATE);

                } catch (InterruptedException ex) { }
            }
        }  
    };
    gameThread.start();

}
@Override
public void paintComponent(Graphics g) {
    super.paintComponent(g);
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, BOX_WIDTH, BOX_HEIGHT);
    g.setColor(Color.BLUE);
    g.fillOval((int) (ballX - ballRadius), (int) (ballY - ballRadius),
               (int)(2 * ballRadius), (int)(2 * ballRadius));
  for(int i=100 ; i < 5 ; i++){   g.setColor(Color.RED);
    g.fillOval((int) (ball1X - ballRadius1), (int) (ball1Y - ballRadius1),

            (int)(2 * ballRadius1), (int)(2 * ballRadius1));  } 
    g.setColor(Color.PINK);
    g.fillOval((int) (ball2X - ballRadius2), (int) (ball2Y - ballRadius2),
               (int)(2 * ballRadius2), (int)(2 * ballRadius2));   

    g.setColor(Color.GREEN);
    g.fillOval((int) (ball3X - ball3Radius), (int) (ball3Y - ball3Radius),
                           (int)(2 * ball3Radius), (int)(2 * ball3Radius));
    for(int i=100 ; i < 5 ; i++){
    g.setColor(Color.YELLOW);
    g.fillOval((int) (ball4X - ball4Radius), (int) (ball4Y - ball4Radius),
                           (int)(2 * ball4Radius), (int)(2 * ball4Radius));}


}

public void keyPressed(KeyEvent e) {

    if (e.getKeyCode() == KeyEvent.VK_RIGHT ) {
        ballSpeedX = 5;
    }
    else if (e.getKeyCode() == KeyEvent.VK_LEFT ) {
        ballSpeedX = -5;
    }
    else if (e.getKeyCode() == KeyEvent.VK_UP ) {
        ballSpeedY = -5;
    }
    else if (e.getKeyCode() == KeyEvent.VK_DOWN ) {
        ballSpeedY = 5;
    }
}

public void keyReleased(KeyEvent e) {
     if (e.getKeyCode() == KeyEvent.VK_RIGHT ) {
         ballSpeedX = 0;
     }
     else if (e.getKeyCode() == KeyEvent.VK_LEFT ) {
         ballSpeedX = 0;
     }
     else if (e.getKeyCode() == KeyEvent.VK_UP ) {
         ballSpeedY = 0;
     }
     else if (e.getKeyCode() == KeyEvent.VK_DOWN ) {
         ballSpeedY = 0;

     }  
}
public void keyTyped(KeyEvent e) { }

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JFrame frame = new JFrame("Collision");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            Man man = new Man();

            frame.setContentPane(man);
            frame.pack();
            frame.addKeyListener(man);
            frame.setVisible(true);
        }
    });
}

}

你的代码对我来说有点长。不过看到你是新建线程调用Thread.sleep,我大概知道你错在哪里了

在您的新线程代码中,您正在更新球的位置(显示在 UI 上)和其他内容,对吗?这应该是您遇到问题的原因。

你不应该(通常不能)在任何线程中更新 UI,UI 线程除外。

为此,您可以使用 javax.swing.Timerjava.util.Timer。 IMO,前者更容易,更适合使用。

只需创建一个新计时器并调用 start:

// declare a timer variable in class level first!
timer = new Timer(1000 / UPDATE_RATE, new ActionListener() {
    // all your code in the while loop...
    // do not include the "while(true)" part
});
timer.start();