82.游戏项目-椭圆轨迹的实现

 1 package test;
 2 
 3 import java.awt.Frame;
 4 import java.awt.event.WindowAdapter;
 5 import java.awt.event.WindowEvent;
 6 
 7 public class MyFrame extends Frame {
 8     
 9     public void launchFrame(){
10         setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT); 
11         setLocation(300,150);
12         setVisible(true);
13         
14         new PaintThread().start();
15         
16         addWindowListener(new WindowAdapter(){
17             public void windowClosing(WindowEvent e) {
18                 System.exit(0);
19             }    
20         });
21     }
22     
23     class PaintThread extends Thread{
24         public void run(){
25             while(true){
26                 repaint();
27                 try {
28                     Thread.sleep(40);
29                 } catch (InterruptedException e) {
30                     e.printStackTrace();
31                 }
32             }
33         }
34     }
35 
36 }

--------------继承 MyFrame的类 减少代码量

 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 GameFrame4 extends MyFrame {
15     
16     Image img = GameUtil.getImage("picture/test3.jpg");
17     
18     private double x = 100, y = 100;
19     private double degree = 3.14/3;// [0,2pi];
20     
21     public void paint(Graphics g) {
22         g.drawImage(img, (int)x, (int)y, null);
23         x = 250 + 100* Math.cos(degree);
24         y = 200 + 50* Math.sin(degree);
25         degree += 0.1;
26     }
27     
28     public static void main(String[] args) {
29         GameFrame4 gf=new GameFrame4();
30         gf.launchFrame();
31     }    
32 }

补:

经常用到的常量可以用一个类封装存储起来

 1 package test;
 2 /**
 3  * 游戏项目中用到的常量
 4  * @author Nicholas
 5  *
 6  */
 7 public class Constant {
 8     
 9     public static final int GAME_WIDTH=500;
10     public static final int GAME_HEIGHT=500;
11     
12 }
原文地址:https://www.cnblogs.com/shixinzei/p/8007012.html