[英]Why is my Java Applet flickering?
我正在研究一个小的Java Applet,它在屏幕上有3个球反弹。 当它们与墙壁碰撞时,它们发出一种声音,而当彼此碰撞时,它们发出另一种声音。
我的问题是球由于某种原因而闪烁,我不确定为什么。 如果有人可以解释为什么他们在忽悠,我将如何解决该问题,将不胜感激。
这是我的代码:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class ass3 extends Applet implements Runnable {
int[] positionX = {0, 0, 0};
int[] positionY = {0, 0, 0};
int[] incrementX = {3, 7, 4};
int[] incrementY = {8, 4, 5};
Thread t;
AudioClip sound;
AudioClip collide;
public void init(){
sound = getAudioClip(getDocumentBase(), "Audio1.au");
collide = getAudioClip(getDocumentBase(), "Audio2.au");
}
public void start() {
t = new Thread(this);
t.start();
}
public void paint(Graphics g) {
g.fillOval(getWidth()-50-positionX[0], getHeight()-50-positionY[0], 50, 50);
g.fillOval(getWidth()-450-positionX[1], getHeight()-450-positionY[1], 50, 50);
g.fillOval(getWidth()-450-positionX[2], getHeight()-50-positionY[2], 50, 50);
}
public void run() {
while (true) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {}
for (int i=0; i < 3; i++) {
positionX[i] += incrementX[i];
positionY[i] += incrementY[i];
}
// ball 1
if (positionX[0] > 500 || positionX[0] < 0) {
incrementX[0] = -incrementX[0];
sound.play();
}
repaint();
if (positionY[0] > 500 || positionY[0] < 0) {
incrementY[0] = -incrementY[0];
sound.play();
}
repaint();
// ball 2
if (positionX[1] > 100 || positionX[1] < -400) {
incrementX[1] = -incrementX[1];
sound.play();
}
repaint();
if (positionY[1] > 100 || positionY[1] < -400) {
incrementY[1] = -incrementY[1];
sound.play();
}
repaint();
// ball 3
if (positionX[2] > 100 || positionX[2] < -400) {
incrementX[2] = -incrementX[2];
sound.play();
}
repaint();
if (positionY[2] > 500 || positionY[2] < 0) {
incrementY[2] = -incrementY[2];
sound.play();
}
repaint();
// collision
if (Math.sqrt(((getWidth()-50-positionX[0])-(getWidth()-450-positionX[1]))*((getWidth()-50-positionX[0])-(getWidth()-450-positionX[1])) + ((getHeight()-50-positionY[0])-(getHeight()-450-positionY[1]))*((getHeight()-50-positionY[0])-(getHeight()-450-positionY[1]))) <= 50) {
incrementX[0] = -incrementX[0];
incrementY[0] = -incrementY[0];
incrementX[1] = -incrementX[1];
incrementY[1] = -incrementY[1];
collide.play();
}
repaint();
if (Math.sqrt(((getWidth()-50-positionX[0])-(getWidth()-450-positionX[2]))*((getWidth()-50-positionX[0])-(getWidth()-450-positionX[2])) + ((getHeight()-50-positionY[0])-(getHeight()-50-positionY[2]))*((getHeight()-50-positionY[0])-(getHeight()-50-positionY[2]))) <= 50) {
incrementX[0] = -incrementX[0];
incrementY[0] = -incrementY[0];
incrementX[2] = -incrementX[2];
incrementY[2] = -incrementY[2];
collide.play();
}
repaint();
if (Math.sqrt(((getWidth()-450-positionX[2])-(getWidth()-450-positionX[1]))*((getWidth()-450-positionX[2])-(getWidth()-450-positionX[1])) + ((getHeight()-50-positionY[2])-(getHeight()-450-positionY[1]))*((getHeight()-50-positionY[2])-(getHeight()-450-positionY[1]))) <= 50) {
incrementX[2] = -incrementX[2];
incrementY[2] = -incrementY[2];
incrementX[1] = -incrementX[1];
incrementY[1] = -incrementY[1];
collide.play();
}
repaint();
}
}
}
很简单,停止多次调用重绘。 (您在while循环中多次调用它)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.