82-83_游戏项目_使用继承封装MyFrame作为以后窗口类共同父类

来源:互联网 发布:文明6 简体中文 mac版 编辑:程序博客网 时间:2024/05/21 20:37

根据游戏先前版本的特点封装一个父类MyFrame代码如下

package com.zhushen.Test;import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/** * 简单窗口类 * @author zhushen * */public class MyFrame extends Frame {    private static final long serialVersionUID = 1L;    /**     * 加载窗口     */    public void launchFrame(){        setSize(Constant.FRAME_WIDTH,Constant.FRAME_HEIGHT);        setLocation(Constant.FRAME_X,Constant.FRAME_Y);        setVisible(true);        new PaintThread().start();//启动重画线程        addWindowListener(new WindowAdapter() {            public void windowClosing(WindowEvent e){                System.exit(0);            }        }) ;        }    /**     * 定义一个重画窗口的线程类,内部类     * @author zhushen     *     */    class PaintThread extends Thread{        public void run(){            while(true){                repaint();                try {                    Thread.sleep(40);//1s画1000/40次                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    }}

由于游戏的窗口的一般是固定的,所有的常量也可以封装一个常量类来处理。

package com.zhushen.Test;/** * 项目的一些配置常量 * @author zhushen * */public class Constant {    public static final int FRAME_X =100;//窗口初始位置的横坐标    public static final int FRAME_Y =100;//窗口初始位置的纵坐标    public static final int FRAME_WIDTH =500;//窗口的宽度    public static final int FRAME_HEIGHT =500;//窗口的高度}

GameFrame

package com.zhushen.Test;import java.awt.Graphics;import java.awt.Image;/** * 游戏窗口类 * @author zhushen * */public class GameFrame extends MyFrame {    private static final long serialVersionUID = 1L;    Image img=GameUtil.getImage("images/mylove.png");    /**     * 在窗口里面画元素     */    private double x=250,y=250;    private double degree=Math.PI/3;//[0,2PI]    private double speed=10;    public void paint(Graphics g){        g.drawImage(img, (int)x, (int)y, null);        if(speed>0){            speed-=0.05;        }else{            speed=0;        }        x+=speed*Math.cos(degree);        y+=speed*Math.sin(degree);        if(y<50||y>Constant.FRAME_HEIGHT-30){            degree=-degree;        }        if(x>Constant.FRAME_WIDTH-30||x<0){            degree=Math.PI-degree;          }    }    public static void main(String[] args) {        GameFrame gf=new GameFrame();        gf.launchFrame();    }}

GameUtil

package com.zhushen.Test;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.IOException;import java.net.URL;import javax.imageio.ImageIO;public class GameUtil {    private GameUtil(){//工具类的一般构造方法私有    }    /**     * 根据path加载一张图片,返回Image     * @param path     * @return     */    public static Image getImage(String path){        URL u=GameUtil.class.getClassLoader().getResource(path);        BufferedImage img=null;        try {            img=ImageIO.read(u);        } catch (IOException e) {            e.printStackTrace();        }        return img;    }}
阅读全文
0 0
原创粉丝点击