所以我在 Java2D 中移动一个图像,它也会反弹。出于某种原因,它总是会留下旧图像的痕迹。我该如何解决这个问题?
主类:
package org.main.graphics;
import java.awt.Graphics;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import org.main.entity.Entity;
import org.main.entity.Loael;
@SuppressWarnings("serial")
public class GameWindow extends JFrame implements Runnable {
private List<Entity> entities = new ArrayList<Entity>();
private Thread animator;
public GameWindow() throws IOException {
super("Game");
setSize(640, 480);
setVisible(true);
setResizable(false);
setLocationRelativeTo(null);
revalidate();
entities.add(new Loael(500, 400));
animator = new Thread(this);
animator.start();
}
public void paint(Graphics g) {
for (Entity entity : entities) {
try {
g.drawImage(entity.getImage(), entity.getX(), entity.getY(),
this);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
while (true) {
for (Entity entity : entities) {
entity.animate(getBounds());
repaint();
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
}
}
}
错误示例: