博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

paint repaint实现动画

Posted on 2012-08-09 11:11  紫冰龙  阅读(279)  评论(0编辑  收藏  举报
import java.applet.*;
import java.awt.*;

import javax.swing.JFrame;
public class Example7_3 extends Applet {
    int i = 1;
    public void init(){
        setBackground(Color.YELLOW);
    }
    public void paint(Graphics g) {
        i = i + 8;
        if (i>200) i=1;
        g.setColor(Color.RED);
        g.fillRect(i, 10, 20, 20);
        g.drawString("我正在学习update ", 100,100);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        
        repaint();
    }
    public void update(Graphics g) {
        g.clearRect(i, 10, 20, 20);
        paint(this.getGraphics());
        
    }
    public Example7_3(){
        //setBackground(Color.YELLOW);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        JFrame f = new JFrame("Test T");
        f.setSize(500,500);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new Example7_3());
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

}