项目三要求:
代码如下:
public class TankClient extends Frame{
int x = 50;
int y = 50 ;
public void lanuchFrame(){
this.setLocation(300,200);
this.setSize(800,600);
this.setResizable(false);
this.setBackground(Color.GREEN);
setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
Frame f =(Frame)e.getSource();
f.setVisible(false);
}
});
new Thread(new PaintThread()).start();
}
public void paint(Graphics g){
Color c =g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y+=5;
}
public static void main(String[] args) {
new TankClient().lanuchFrame();
}
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();
//就是来自于Frame的repaint方法,这样就可以
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
//需要注意就是paint方法就是当窗口刚出现、覆盖重现、扯动窗口大小的paint()