81.游戏项目-物体任意角度飞行和停止

 1 package test;
 2 import java.awt.Color;
 3 import java.awt.Font;
 4 import java.awt.Frame;
 5 import java.awt.Graphics;
 6 import java.awt.Image;
 7 import java.awt.event.WindowAdapter;
 8 import java.awt.event.WindowEvent;
 9 /**
10  * 测试物体沿着任意角度飞行 和停止
11  * @author Nicholas
12  * 
13  */
14 public class GameFrame3 extends Frame {
15     Image img = GameUtil.getImage("picture/test3.jpg");
16 
17     public void launchFrame(){
18         setSize(500,500);
19         setLocation(300,150);
20         setVisible(true);
21         
22         new PaintThread().start();
23         
24         addWindowListener(new WindowAdapter(){
25             public void windowClosing(WindowEvent e) {
26                 System.exit(0);
27             }
28         });
29     }
30     
31     private double x = 100, y = 100;
32     private double degree = 3.14/3;// 弧度:[0,2*pi];
33     private double speed = 10;//控制速率
34     
35     public void paint(Graphics g) {
36         g.drawImage(img, (int)x, (int)y, null);
37         x +=speed * Math.cos(degree);
38         y +=speed * Math.sin(degree);
39         if(speed > 0){
40             speed -= 0.05;
41         }else {
42             speed = 0;
43         }
44         if(y > 500-50 || y < 30){
45             degree=-degree;
46         }
47         if(x < 0 || x >= 500-50){
48             degree=Math.PI-degree;
49         }
50     }
51     
52     class PaintThread extends Thread{
53         public void run(){
54             while(true){
55                 repaint();
56                 try {
57                     Thread.sleep(40);
58                 } catch (InterruptedException e) {
59                     e.printStackTrace();
60                 }
61             }
62         }
63     }
64     public static void main(String[] args) {
65         GameFrame3 gf=new GameFrame3();
66         gf.launchFrame();
67     }    
68 }

posted @ 2017-12-08 19:52  wydxry  阅读(298)  评论(0编辑  收藏  举报
Live2D