【发布时间】:2014-07-11 21:26:38
【问题描述】:
我是图形和 japplet 的新手,我制作了一个穿过屏幕的矩形。但是由于某种原因,它只是画了一条线,而不是在移动后删除矩形的旧实例。
主要:
public class Main extends JApplet implements Runnable {
private static final long serialVersionUID = 1L;
private static int width = 900;
private static int height = 600;
public static int fps = 60;
public Thread thread = new Thread(this);
public static Ailoid ailoid = new Ailoid();
public void init() {
setSize(width, height);
setBackground(Color.white);
ailoid.setLocation(new Location(100, 100));
AlienManager.registerAlien(ailoid);
}
public void paint(Graphics g) {
g.setColor(Color.green);
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
int x = loc.getX();
int y = loc.getY();
g.fillRect(x, y, 10, 20);
}
}
// Thread start
@Override
public void start() {
thread.start();
}
// Thread stop
@SuppressWarnings("deprecation")
@Override
public void destroy() {
thread.stop();
}
@Override
public void run() {
while (true) {
Updater.run();
repaint();
try {
// 1000 divided by fps to get frames per millisecond
Thread.sleep(1000 / fps);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
更新者:
public class Updater {
public static void run() {
for (Alien alien : AlienManager.getAliens()) {
Location loc = alien.getLocation();
int x = loc.getX();
int y = loc.getY();
alien.setLocation(new Location(x, y));
}
}
}
为什么不删除旧图形?谢谢!
【问题讨论】:
标签: java swing graphics japplet