import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class cricle extends JFrame {    int x=80,y=80;
    
    public void launch(){
        //setLocation(400, 300);
        setSize(700,800);
        setLocationRelativeTo(null);  //jframe居中显示
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);  //jframe不可改变大小
        setBackground(Color.YELLOW);
        setVisible(true);
        Thread t=new Thread(new RunTank());
        t.start();
    }
    
    //画物体        
                          public void paint(Graphics g){
            Color c=g.getColor();
            g.setColor(Color.red);
            g.fillOval(x,y, 40, 40);
            g.setColor(c);
            y+=50;
        }
        
        //物体运动
        class RunTank implements Runnable{            public void run() {
                while(true) {
                    repaint();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
    }
    
    
    public static void main(String[] args) {        cricle tan= new cricle();
        tan.launch();    }
    }

我是想实现一个物体的运动,模拟游戏人物的走动效果。
在这段代码中为什么这个物体之前的运动轨迹都会留下来?

解决方案 »

  1.   

    (运行效果)图片地址:http://hi.csdn.net/space-6758047-do-album-picid-1058949.html 
      

  2.   

    重写paint方法的时候记得添加  super.paint(g);
    public void paint(Graphics g){
          super.paint(g);
          .........................  
     }
    就ok了。
      

  3.   


    这样不行啊。。
    有个疑问:1.出现这种现象的原因是不是背景没有重刷,更新的原因?
    2.如果这样的话,我在线程中调用了repaint()方法,而repaint()方法在jvm内部不是会自动update()更新操作,然后在paint()?
      

  4.   

    import java.awt.*;
    import java.awt.event.*;public class TankClient extends Frame {

    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 lauchFrame() {
    this.setLocation(400, 300);
    this.setSize(800, 600);
    this.setTitle("TankWar");
    this.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    this.setResizable(false);
    this.setBackground(Color.GREEN);
    setVisible(true);

    new Thread(new PaintThread()).start();
    } public static void main(String[] args) {
    TankClient tc = new TankClient();
    tc.lauchFrame();
    }

    private class PaintThread implements Runnable { public void run() {
    while(true) {
    repaint();
    try {
    Thread.sleep(100);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    }}
    这是马士兵老师写的,这段代码可以实现想要的效果我写的和这段有什么区别吗?
      

  5.   

    其实 你所看到的界面 都是通过一直重绘的。。
    你所能用到
    public void paint(Graphics g){
    }
    方法 是Compent自带的。。
    你对界面所进行拖到 放大 缩小 等等操作等要触发paint这个方法
    super.paint(g);就是相当于每次都重绘界面。。
      

  6.   

    额。还是不明白为什么要super.paint(g);
    repaint()方法不是会自动调用paint()方法吗?
      

  7.   

    你现在只是重写paint方法。。 下层的paint你肯定就调用不到了。。你的repaint()也只会调用到你自己的paint方法 super.paint(g);就可以调用到被你重写的paint方法了。。
      

  8.   

    每次重绘画面前,要用背景色,涂抹整个FRAME.
      

  9.   

    都不是!那是因为Frame是属于awt的容器组件,第一他不适合用来做绘图,因为它是从Component这个祖先派生而来,做了许多改进。第二是因为awt包的组件都没有实现双缓冲,要实现动画,必须覆盖update方法,将这个方法体重写成:
    public void update(Graphics g){
        g.setColor(getBackground());
        g.fillRect(getSize().getWidth(),getSize().getHeight());
        g.setColor(getForeground());
        paint(g);
    }但有种更方便的做法,即使用swing包中的组件,因为这个包中的组件都实现了双缓冲。即改刷屏为贴图,可小小防止画面闪烁现象。
    但面板就不要使用JFrame了,而是使用JComponent,这个组件虽然很原始,但希望你多研究研究,很有来头的!搞懂了这个组件,Java中的所有组件是怎么来的,他们的设计思想怎样的,都会明朗。
      

  10.   

    super.paint()以后,自己设置的背景颜色都被刷成父类frame的背景颜色了,在覆盖的paint()方法中怎么设置成自己想要的背景颜色啊?