package test;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
* 测试窗口物体沿着任意角度飞行
* 最后,实现一个小的台球游戏
* @author Administrator
*
*/
public class GameFrame03 extends Frame{
//GUI编程:AWT,swing等。一般工作用不到
Image img = GameUtil.getImage("images/sun.jpg");
/**
* 加载窗口
*/
public void launchFrame(){
setSize(500,500); //窗口的大小设置
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; //[0.2pi](0-360度) //碰撞角度,反弹方向
private double speed = 10; //移动速度
@Override
public void paint(Graphics g) {
g.drawImage(img, (int)x, (int)y,null);
x += speed*Math.cos(degree);
y += speed*Math.sin(degree);
if(speed>0){ //速度的渐减
speed -= 0.05;
}else{ //直到0,停下
speed = 0;
}
if(y>500-30||y<0){ //这里碰到边框反弹
degree = -degree;
}
if(x<0||x>500-30){
degree = Math.PI-degree;
}
}
/**
* 定义一个重画窗口的线程类,是一个内部类
* @author Administrator
*
*/
class PaintThread extends Thread{
public void run(){
while(true){ //死循环
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
} //1s = 1000ms
}
}
}
public static void main(String[] args) {
GameFrame03 gf = new GameFrame03();
gf.launchFrame();
}
}