Java中的一个小问题

这个是Animate.java
import java.awt.*;
public class Animate extends javax.swing.JApplet
implements Runnable {

Image[] picture = new Image[6];
int totalPictures = 0;
int current = 0;
Thread runner;
int pause = 500;

public void init() {
for (int i = 0; i < 6; i++) {
String imageText = null;
imageText = getParameter("image"+i);
if (imageText != null) {
totalPictures++;
picture[i] = getImage(getCodeBase(), imageText);
} else {
break;
}
}
String pauseText = null;
pauseText = getParameter("pause");
if (pauseText != null) {
pause = Integer.parseInt(pauseText);
}
}

public void paint(Graphics screen) {
super.paint(screen);
Graphics2D screen2D = (Graphics2D) screen;
if (picture[current] != null) {
screen2D.drawImage(picture[current], 0, 0, this);
}
}

public void start() {
if (runner == null) {
runner = new Thread(this);
runner.start();
}
}

public void run() {
Thread thisThread = Thread.currentThread();
while (runner == thisThread) {

if (current >= totalPictures) {
current = 0;
}
try {
Thread.sleep(pause);
} catch (InterruptedException e) {
// do nothing
}
repaint();
current++;
}
}

public void stop() {
if (runner != null) {
runner = null;
}
}
}
这个是Animate.html
<applet code="Animate.class" width="215" height="298">
<param name="image0" value="image/lh0.gif">
<param name="image1" value="image/lh1.gif">
<param name="image2" value="image/lh2.gif">
<param name="image3" value="image/lh3.gif">
<param name="pause" value="800">
</applet>
我的问题是为什么run()方法改成这样运行的效果不同
public void run() {
Thread thisThread = Thread.currentThread();
while (runner == thisThread) {

if (current >= totalPictures) {
current = 0;
}
repaint();
current++;
try {
Thread.sleep(pause);
} catch (InterruptedException e) {
// do nothing
}
}
}
就是把Repaint和current++的位置调到sleep之前,显示的图片会少一个。只能传一张图片还有3张类似的。

因为你在thread.sleep 之前 repaint 了能一样么大哥 那是在线程休眠前做的 和之后做的当然不一样了追问

while (runner == thisThread) {
current++;
if (current {
curre=0;
}
repaint();
try {
Thread.sleep(pause);
原始run代码是这么写的,我删掉了一部分,写不下,你应该能看懂,这么写有一个缺点就是从第二幅图片开始显示,你看这里的repaint()也是在sleep之前啊。但是我给的那个不完善的run(),运行后会有一个图片不显示,我想知道为什么

追答

你把repaint放到休眠后头你 看了么

温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-11-25
repaint(); 重画
第一张显示了,但是没sleep就重画换掉了
相似回答