import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;public class Test {    public static void main(String[] args) {
        TankWar tankWar = new TankWar();    }
}class TankWar extends Frame {//
                              //主要问题为:把这里的Frame改成JFrame运行时不会擦除背景
                              //请运行一下看看区别,然后帮忙解释一下吧~~
                              //别人问我的问题,搞不清了,看API也看的眼花缭乱的~
                              //
                              //另外感觉这段代码~~有点~~那个,在repaint()里调用paint()会产生递归吧
                              //我觉的这不是很好的设计方式,请大家评价一下
                              //
                              //又:谁有关于画图或paint()方法的详细点的资料,分享一下吧~谢谢啦
    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;
        try {
            Thread.sleep(500);
        } catch (InterruptedException ex) {
            Logger.getLogger(TankWar.class.getName()).log(Level.SEVERE, null, ex);
        }
        repaint();
    }    public TankWar() {
        super("测试");
        this.setLocation(100, 100);
        this.setSize(800, 600);
        this.setResizable(false);
        this.setBackground(Color.GREEN);
        setVisible(true);
    }
}   问题在注释里~~

解决方案 »

  1.   

    在JFrame中update方法的文档说明是Just   call   paint()。每次repaint重绘时,背景不重绘。要重绘背景要么在paint里调用super.paint();要么自己在paint里写重绘背景代码。
    详细出处参考:http://bbs.firnow.com/dview16t125538.html
      

  2.   

    就应该用JFRAME啊,FRAME把整个背景色都变了
      

  3.   

    repaint不是马上让系统去重绘界面,只是发个消息通知界面要重绘。
    不过在paint里调用repaint是不好,同样效果可以这样做。    public void paint(Graphics g) {
            //--by blazingfire 用JFrame不能重绘,要加上如下代码
            //你还要重绘背景
            super.paint(g);
            
            Color c = g.getColor();
            g.setColor(Color.RED);
            g.fillOval(x, y, 30, 30);
            g.setColor(c);
        }    public TankWar() {
            super("测试");
            this.setLocation(100, 100);
            this.setSize(800, 600);
            this.setResizable(false);
            this.setBackground(Color.GREEN);
            setVisible(true);
            
            //--by blazingfire 用定时器重绘, paint方法只要绘图就好了
            new Timer().schedule(new TimerTask() {
                
                @Override
                public void run() {
                    y += 5;
                    TankWar.this.repaint();
                }
                
            }, 500, 500); 

        }
      

  4.   

    楼主为什么要使用一个线程?为了动态改变paint?