79_游戏项目_动画的实现

来源:互联网 发布:天翼飞young客户端mac 编辑:程序博客网 时间:2024/06/16 15:52
package com.zhushen.Test;import java.awt.Frame;import java.awt.Graphics;import java.awt.Image;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/** * 游戏窗口类 * @author zhushen * */public class GameFrame extends Frame {    private static final long serialVersionUID = 1L;    Image img=GameUtil.getImage("images/mylove.png");    /**     * 加载游戏窗口     * @param x     * @param y     * @param width     * @param height     */    public void launchFrame(int x,int y,int width,int height){        setSize(width,height);        setLocation(x,y);        setVisible(true);        new PaintThread().start();//启动重画线程        addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e){                System.exit(0);            }        }) ;        }    /**     * 在窗口里面画元素     */    private double x=100,y=100;    public void paint(Graphics g){        g.drawImage(img, (int)x, (int)y, null);    //  x+=3;        y+=10;    }    /**     * 定义一个重画窗口的线程类,内部类     * @author zhushen     *     */    class PaintThread extends Thread{        public void run(){            while(true){                repaint();                try {                    Thread.sleep(40);//1s画1000/40次                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }    public static void main(String[] args) {        GameFrame gf=new GameFrame();        gf.launchFrame(100,100,500,500);    }}