我已經創建了Java彈跳球應用程序。一切都如假想般運作,但球在移動時凍結。當我將光標移動到窗口時,球順利移動。當我停止移動光標時,它又一次凍結。JAVA彈跳球凍結
OS:Fedora的23 JDK:8
public class Main {
public static void main(String[] args) {
Game game = new Game();
}
}
class Game extends JFrame {
public Game(){
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
Ball ball1 = new Ball();
add(ball1);
setSize(400,300);
setTitle("Bubbles");
setVisible(true);
}
}
class Ball extends JPanel implements Runnable{
int radius = 50;
int x = 5 ;
int y = 5;
int dx = 2;
int dy = 2;
public Ball(){
Thread thread = new Thread(this);
thread.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.red);
g.fillOval(x,y,radius,radius);
}
public void moveBall(){
if (x + dx > 400 - radius){
x = 400 - radius;
dx = -dx;
} if (x + dx < 0){
x = 0;
dx = -dx;
}
else {
x += dx;
}
if (y + dy > this.getHeight() - radius){
y = this.getHeight() - radius;
dy = -dy;
} if (y + dy < 0){
y = 0;
dy = -dy;
}
else {
y += dy;
}
}
@Override
public void run() {
while (true){
moveBall();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
您的代碼爲我工作。 –
它也適用於我:/ –
我測試了代碼,沒有任何東西凍結。 (操作系統:WIN10 JDK:8),所以它必須是Fedora的東西。 – osanger