저는 그래픽과 japplet을 처음 사용하고 있으며, 화면을 가로 지르는 직사각형을 만들었습니다. 그러나 어떤 이유로 인해 사각형이 이동하면 사각형의 이전 인스턴스를 제거하지 않고 선을 그립니다.Java 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));
}
}
}
가 왜 기존의 그래픽을 제거하지 않는 이유는 무엇입니까? 고맙습니다!
public void paint(Graphics g) {
super.paint(g);
//....
가 오프
까지 더 나은 아니에요, 그런 말로 미루어 보아 :
@MCMastery : 관련하여, "이것은 JApplet이지만 고맙습니다."- 저는 이것을 알고 있으며 이것은 제가 말하고있는 것과 정확히 같습니다. ** 다시 ** 당신은 톱 레벨 윈도우의 paint 메소드를 JApplet, JFrame 등으로 그려서는 안됩니다. –
그럼 어떻게 업데이트합니까? – MCMastery
@MCMastery : 관련해서,''어떻게 업데이트합니까? ''- 현재 코드가 위치를 복사하고 다시 사용합니다. 약간 변경하는 것을 고려하십시오. 당신이하는 일은 당신에게 달려 있습니다. –