封装成MyFrame父类

package theChildOfGod.test;

/**
 *游戏项目中的常量 
 */
public class Constant {
    public static final int GAME_WIDTH = 500;
    public static final int GAME_HEIGHT = 500;
}
package theChildOfGod.test;

import java.awt.Frame;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import org.omg.Messaging.SyncScopeHelper;

public class MyFrame extends Frame {


    /**
     * 加载窗口
     */
    public void launchFrame() {
        setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
        setLocation(100, 100);
        setVisible(true);

        new PaintThread().start(); // 启动重画线程

        addWindowListener(new WindowAdapter() { // 通过这个可以直接关闭窗口
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }

    private double x = 100, y = 100;
    private double degree = 3.14 / 3; // [0, 2pi]
    private double speed = 100;

    @Override
    public void paint(Graphics g) {

        if (speed > 0) {
            speed -= 0.5;
        }
        x += speed * Math.cos(degree);
        y += speed * Math.sin(degree);

        if (y > 500 - 50 || y < 30) {
            degree = -degree;
        }

        if (x < 0 || x > 500 - 30) {
            degree = Math.PI - degree;
        }

    }

    /**
     * 定义一个重画窗口的线程类,是一个内部类。
     * 
     * @author theChildOfGod
     *
     */
    class PaintThread extends Thread {

        public void run() {
            while (true) {
                try {
                    repaint();
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package theChildOfGod.test;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import theChildOfGod.test.GameFrame.PaintThread;

public class GameFrame03 extends MyFrame{
    Image img = GameUtil.getImage("images/timg.jpg");


    private double x = 100, y = 100;

    private double degree = 3.14 / 3; //[0, 2pi]
    private double speed = 100;
    @Override
    public void paint(Graphics g) {

        g.drawImage(img, (int)x, (int)y, 50, 50, null);

        degree += 0.1;

    }
    public static void main(String[] args) {
        GameFrame02 gf = new GameFrame02();
        gf.launchFrame();

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值