package com.test;
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
public class Tank extends JFrame{
int x=50,y=50;
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 void tank(){
this.setLocation(200,100);
this.setTitle("坦克大战");
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.white);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
Tank t=new Tank();
t.tank(); }
public class PaintThread implements Runnable{
public void run() {
while(true){
repaint();
try{
Thread.sleep(50);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}}运行后始终有坦克的原始的移动路线,怎样消除之前的移动路线。
import java.awt.*;
import java.awt.event.*;import javax.swing.*;
public class Tank extends JFrame{
int x=50,y=50;
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 void tank(){
this.setLocation(200,100);
this.setTitle("坦克大战");
this.setSize(800,600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
this.setBackground(Color.white);
setVisible(true);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
Tank t=new Tank();
t.tank(); }
public class PaintThread implements Runnable{
public void run() {
while(true){
repaint();
try{
Thread.sleep(50);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
}}运行后始终有坦克的原始的移动路线,怎样消除之前的移动路线。
g.setColor(Color.WHITE);
g.fillRect(0, 0, this.getWidth(), this.getHeight()); Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 30, 30);
g.setColor(c);
y += 5;
}