paint repaint实现动画

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);
    }

}
原文地址:https://www.cnblogs.com/qqjue/p/2629892.html