import java.awt.*;
import java.awt.event.*;import javax.swing.*;public class TankClient extends JFrame { //此处改为Frame就可以重画
private int x = 50; private int y = 50; public TankClient() {
super("Tank War");
this.setSize(400, 400);
this.setLocation(200, 200);
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
new Thread(new PaintTank()).start();
this.setVisible(true);
} public void paint(Graphics g) {
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
x += 25;
y += 25;
} public static void main(String[] args) {
TankClient tc = new TankClient();
} private class PaintTank implements Runnable {
public void run() {
while (true) {
repaint(); //时不时此处的repaint因为是属PaintTank的缘故?
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}为何改为用awt就可以重画了,现在是原来的还在,新的也能画出,真不知是为什么
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
x += 25;
y += 25;
}改成public void paintComponent(Graphics g) {
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
x += 25;
y += 25;
}
你要是只要求画新的,就加一句
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
x += 25;
y += 25;
}
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
x += 25;
y += 25;
}