一个吃豆豆动画

import java.awt.*;
import java.awt.event.*;         //事件响应需要
import java.util.*;              //定时器需要
public class app20_8 extends Frame implements ActionListener {

	/**
	 * @param args
	 * 吃豆豆动画
	 * Delmore
	 */
	
	static app20_8 frm = new app20_8();
	static Button btn = new Button("begin game");
	int sx = 20;
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		frm.setTitle("吃豆豆游戏");
		frm.setBounds(400,200,450,250);
		frm.setLayout(null);
		btn.setBounds(100,200,100,30);
		frm.add(btn);
		btn.addActionListener(frm);
		frm.setVisible(true);
		
		frm.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		}); 
	}
	public void actionPerformed(ActionEvent e){
	   
			Timer t = new Timer( true);
			TimerTask aa = new TimerTask(){
				public void run(){
					Graphics g = getGraphics();
					g.setColor(Color.white);                    //擦除小精灵
					g.fillArc(sx, 40, 50, 50, 20, 320);	
					sx = sx + 20;                              //移动小精灵
					if (sx>400){
						sx = 20;                                //回到原来位置
					}		
					paint(g);		                           //调用paint函数
				}

			};
			t.schedule(aa,1000,500);
	}
	
	public void paint(Graphics g){
		g.setColor(Color.red);
		for(int x = 20;x<=sx;x=x+20)
			g.fillOval(x+5, 62, 5, 5);
		for(int x=sx+25;x<=420;x=x+20){
			g.fillOval(x, 60, 10, 10);
		}
		g.setColor(Color.pink);	
		g.fillArc(sx, 40, 50, 50, 40, 280);	
		for(int i=0;i<50000000;i++);
		g.fillArc(sx, 40, 50, 50, 20, 320);		
	}

}

实现精灵吃豆豆的动画,吃完后,变成小点

你可能感兴趣的:(一个吃豆豆动画)