80.游戏项目-物体的移动

 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 GameFrame2 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 boolean left;
33     private boolean up;
34     
35     public void paint(Graphics g) {
36         g.drawImage(img, (int)x, (int)y, null);
37     
38         if(left){
39             x-=3;
40         }
41         else {
42             x+=3;
43         }
44         if(x>500-50){
45             left=true;
46         }
47         if(x<0){
48             left=false;
49         }
50         if(up){
51             y-=5;
52         }
53         else {
54             y+=5;
55         }
56         if(y>500-50){
57             up=true;
58         }
59         if(y<20){
60             up=false;
61         }
62     }
63     
64     class PaintThread extends Thread{
65         public void run(){
66             while(true){
67                 repaint();
68                 try {
69                     Thread.sleep(10);
70                 } catch (InterruptedException e) {
71                     e.printStackTrace();
72                 }
73             }
74         }
75     }
76     public static void main(String[] args) {
77         GameFrame2 gf=new GameFrame2();
78         gf.launchFrame();
79     }    
80 }

原文地址:https://www.cnblogs.com/shixinzei/p/8006871.html