【发布时间】:2015-11-12 13:01:13
【问题描述】:
所以这就是我卡住的地方......我让它水平走到窗口的尽头,然后下降,但我不知道如何让它在到达窗口底部后向左移动并且然后当它到达屏幕的左侧时上升。 谢谢,
import javax.swing.*;
import java.awt.*;
public class AnimatedImageApplet extends JApplet implements Runnable {
private static final long serialVersionUID = 1L;
private Thread t = null;
private Image image;
private int x = 0;
private int y = 0;
private static final int vx = 1;
private static final int vy= 1;
private boolean horizontal = true;
private boolean vertical = true;
public void init() {
image = getImage(getDocumentBase(), "face.png");
}
public void start() {
if (t == null) {
t = new Thread(this);
t.start();
}
}
public void paint(Graphics canvas) {
canvas.fillRect(0,0,getWidth(),getHeight());
synchronized (this) {
canvas.drawImage(image, x, y, this);
}
}
@Override
public void run() {
int direction = 1;
while (true) {
synchronized (this) {
x += vx * direction;
y += vy * (horizontal ? 0 : 1);
if (x + image.getWidth(this) == getWidth()) {
horizontal = false;
direction = 0;
}
}
repaint();
try {
Thread.sleep(15);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
【问题讨论】:
-
有一个 x/y 增量,当不需要时,只需将其设置为
0,当您到达其中一个边界(垂直/水平)时,将另一个增量设置为非 0 值跨度> -
我一直盯着这段代码太久了。迷路了,不过还是谢谢。
标签: java animation applet japplet