下面的java代码每次重绘的时候,之前的红色的圆没有被擦除掉,是怎么回事?
importjava.awt.Color;importjava.awt.Graphics;importjava.awt.event.WindowAdapter;impor...
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TankClient extends JFrame{
private JPanel mainPanel;
private int x=50,y=50;
@Override
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 TankClient(){
lauchFrame();
}
public void lauchFrame(){
mainPanel = new JPanel();
mainPanel.setBackground(Color.green);
this.add(mainPanel);
this.setBounds(300,100,800,600);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
new TankClient();
}
/**
* 创建一个Tread每隔相同时间进行重画,从而改变坦克位置
* @author Administrator
*/
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();//重画组件
try {
Thread.sleep(100);//睡100ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
} 展开
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TankClient extends JFrame{
private JPanel mainPanel;
private int x=50,y=50;
@Override
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 TankClient(){
lauchFrame();
}
public void lauchFrame(){
mainPanel = new JPanel();
mainPanel.setBackground(Color.green);
this.add(mainPanel);
this.setBounds(300,100,800,600);
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
this.setResizable(false);
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
new TankClient();
}
/**
* 创建一个Tread每隔相同时间进行重画,从而改变坦克位置
* @author Administrator
*/
private class PaintThread implements Runnable{
public void run() {
while(true){
repaint();//重画组件
try {
Thread.sleep(100);//睡100ms
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询