Java_太陽系_行星模型_小游戲練習_詳細注釋


 1 //實現MyFrame--實現繪制窗口,和實現重寫 重畫窗口線程類
 2 
 3 package cn.xiaocangtian.Test;
 4 
 5 import java.awt.Frame;
 6 import java.awt.event.WindowAdapter;
 7 import java.awt.event.WindowEvent;
 8 
 9 
10 public class MyFrame extends Frame {
11 
12         //加載窗口
13         public void launchFrame() {
14             setSize(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);       //設置窗口大小
15             setLocation(100, 100);   //設置左上角坐標,開始位置, 也就是窗口開始位置
16             setVisible(true);        //設置為可見(默認為不可見)
17             
18             //啟動重畫線程
19             new PaintThread().start();
20             
21             //匿名內部類---用來關閉窗口
22             addWindowListener(new WindowAdapter() {
23                 @Override
24                 public void windowClosing(WindowEvent e) {
25                     System.exit(0);
26                 }
27             });
28             
29         }
30         
31         /**
32          * 定義一個重畫窗口的線程類
33          * 是一個內部類(方便訪問外部類屬性)
34          */
35         class PaintThread extends Thread {
36             public void run() {
37                 while (true) {
38                     repaint();             //重畫
39                     try {
40                         Thread.sleep(40);  //1s = 1000ms
41                     } catch (InterruptedException e) {
42                         e.printStackTrace();
43                     }  
44                 }
45             }
46         }
47         
48 }
 1 package cn.xiaocangtian.Util;
 2 
 3 import java.awt.Image;
 4 import java.awt.Toolkit;
 5 import java.awt.image.BufferedImage;
 6 import java.io.IOException;
 7 import java.net.URL;
 8 
 9 import javax.imageio.ImageIO;
10 import javax.swing.ImageIcon;
11 
12 /**
13  * 游戲開發中常用的工具類(比如:加載圖片等方法)
14  * @author admin
15  *
16  */
17 public class GameUtil {
18     
19     private GameUtil () {} //工具類通常將構造方法私有
20     
21     public static Image getImage(String path) {
22 //        URL u = GameUtil.class.getClassLoader().getResource(path);
23 //        BufferedImage img = null;
24 //        try {
25 //            img = ImageIO.read(u);
26 //        } catch (IOException e) {
27 //            e.printStackTrace();
28 //        }
29 //        
30 //        return img;  //BufferedImage是Image子類,也算正確返回
31         return Toolkit.getDefaultToolkit().getImage(GameUtil.class.getClassLoader().getResource(path));
32     }
33 }
 1 package cn.xiaocangtian.Solar;
 2 
 3 import java.awt.Graphics;
 4 import java.awt.Image;
 5 
 6 import cn.xiaocangtian.Util.GameUtil;
 7 
 8 //封裝成類
 9 //導入圖片
10 public class Star {
11     Image img;                //用於導入圖片
12     double x, y;              //圖片位置
13     int width, height;        //圖片長寬
14     
15     public void draw(Graphics g) {
16         g.drawImage(img, (int)x, (int)y, null);
17     }
18     
19     public Star() {          //子類要調用父類的默認造函數
20         
21     }
22     
23     public Star(Image img) {
24         this.img = img;
25         this.width = img.getWidth(null);
26         this.height = img.getHeight(null);
27         
28     }
29     
30     public Star(Image img, double x, double y) {
31         this(img);
32         this.x = x;
33         this.y = y;
34         
35     }
36     
37     //導入
38     public Star(String imgpath, double x, double y) {
39         this(GameUtil.getImage(imgpath), x, y);
40     }
41 }
 1 package cn.xiaocangtian.Util;
 2 
 3 /**
 4  * 游戲項目中用到的常量
 5  * 單獨負責常量
 6  * @author admin
 7  */
 8 public class Constant {
 9     
10     public static final int GAME_WIDTH = 750;
11     public static final int GAME_HEIGHT = 600;
12     
13 }
 1 package cn.xiaocangtian.Solar;
 2 
 3 import java.awt.Color;
 4 import java.awt.Graphics;
 5 import java.awt.Image;
 6 
 7 import cn.xiaocangtian.Util.GameUtil;
 8 
 9 public class Planet extends Star {
10     
11     //除了圖片,坐標,行星沿着某個橢圓運行:長軸,短軸,速度, 角度,繞着某個Star飛
12     double longAxis;        //橢圓的長軸
13     double shortAxis;       //橢圓的短軸
14     double speed;           //飛行的速度
15     double degree;          //角度
16     Star center;            //中心
17     boolean satillite;      //標志是否是衛星
18     
19     public void draw(Graphics g) {
20         super.draw(g);
21         move();
22         if (!satillite) {   //不是衛星再畫出軌跡
23             drawTrace(g);
24         }
25     }
26     
27     public void move() {
28         //沿着橢圓飛
29         x = (center.x + center.width/2) + longAxis * Math.cos(degree);
30         y = (center.y + center.height/2) + shortAxis * Math.sin(degree);                
31         //速度不一樣,所以增量也不同
32         degree += speed;
33     }
34     
35     //畫出行星的軌跡
36     public void drawTrace(Graphics g) {
37         double ovalX, ovalY, ovalWidth, ovalHeight;
38         ovalHeight = longAxis * 2;                         //長度即為長軸*2
39         ovalWidth = shortAxis * 2;
40         ovalX = (center.x + center.width/2) - longAxis;    //左上頂點為(中心.x + x.width/2) - 長軸
41         ovalY = (center.y + center.height/2) - shortAxis;
42         
43         Color oldColor = g.getColor();
44         g.setColor(Color.blue);                            //設置軌跡顏色
45         g.drawOval((int)ovalX, (int)ovalY, (int)ovalHeight, (int)ovalWidth);
46         g.setColor(oldColor);
47     }
48     
49     
50     //需要調用父類的空構造器
51     public Planet(Star center, String imgpath, double longAxis,
52             double shortAxis, double speed) {
53         super(GameUtil.getImage(imgpath));
54         
55         this.center = center;
56         this.x = center.x + longAxis;         //行星的位置
57         this.y = center.y;
58         
59         this.longAxis = longAxis;             //當前行星的長軸
60         this.shortAxis = shortAxis;
61         this.speed = speed;
62         
63         this.width = img.getWidth(null);
64         this.height = img.getHeight(null);
65     }
66 
67     public Planet(Star center, String imgpath, double longAxis,
68             double shortAxis, double speed, boolean satellite) {
69         this(center, imgpath, longAxis, shortAxis, speed);
70         this.satillite = satellite;
71     }
72     
73     public Planet(Image img, double x, double y) {
74         super(img, x, y);
75     }
76     
77     public Planet(String imgpath, double x, double y) {
78         super(imgpath, x, y);
79     }
80     
81 }
 1 package cn.xiaocangtian.Solar;
 2 
 3 import java.awt.Graphics;
 4 import java.awt.Image;
 5 
 6 import cn.xiaocangtian.Util.Constant;
 7 import cn.xiaocangtian.Util.GameUtil;
 8 import cn.xiaocangtian.Util.MyFrame;
 9 
10 /**
11  * 太陽系主窗口
12  * @author admin
13  *
14  */
15 public class SolarFrame extends MyFrame {
16     //導入背景
17     Image bg = GameUtil.getImage("images/yuzhou.png");
18     //這里是利用封裝的類,導入圖片
19     Star sun = new Star("images/sun.png", Constant.GAME_WIDTH / 2, Constant.GAME_HEIGHT / 2);
20     Planet earth = new Planet(sun, "images/polar.png", 150, 100, 0.1);
21     Planet moon = new Planet(earth, "images/moon.png", 30, 20, 0.3, true);
22     Planet Mars = new Planet(sun, "images/Mars.png", 200, 130, 0.2);
23     
24     /**
25      * 可以繼續添加 其他 行星,只需一行代碼(已經封裝好)
26      * ......
27      * ......
28      * ......
29      */
30     
31     /**
32      * 重寫重繪函數,此為回調函數,只需要實現,然后由系統自動調用
33      */
34     public void paint(Graphics g) {
35         g.drawImage(bg, 0, 0, null);
36         sun.draw(g);       //這里使用的是封裝的方法
37         earth.draw(g);
38         moon.draw(g);
39         Mars.draw(g);
40         
41         /***
42          * 還可以繼續添加其他行星並繪制
43          * ..........
44          * ..........
45          * ..........
46          */
47     }
48     
49     public static void main(String[] args) {
50         new SolarFrame().launchFrame();
51     }
52 }

//本程序只添加了太陽,地球,月球,火星,其余可以自行添加,使用封裝好的方法,只用十分簡潔的代碼即可


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM