最近看了面向?qū)ο蟮囊恍┲R,然后跟著老師的講解做了一個太陽系各行星繞太陽轉(zhuǎn)的小游戲,來練習(xí)鞏固一下最近學(xué)的知識:
用到知識點:類的繼承、方法的重載與重寫、多態(tài)、封裝等
分析:
1.需要加載圖片、畫圖
2.建一個面板,主頁面
3.行星類
效果圖:
先看一下源碼結(jié)構(gòu)圖:
現(xiàn)在逐步分析各個類的功能:
1)工具類-----util包中
--Constant類 封裝了游戲中用到的常量
--GameUtil類 封裝了游戲的圖片加載功能
--MyFrame類 封裝了游戲面板的構(gòu)造,用于各面板的父類
------之所以這樣做,目的是為了封裝數(shù)據(jù),便于程序的擴(kuò)充
Constant.java
1
2
3
4
5
6
7
|
package util; public class Constant { public static final int GAME_WIDTH = 800 ; public static final int GAME_HEIGHT = 600 ; } |
GameUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
package util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; /** * 工具類(加載圖片) * @author long * */ public class GameUtil { private GameUtil(){ } //工具類通常將構(gòu)造方法私有 public static Image getImage(String path){ URL u = GameUtil. class .getClassLoader().getResource(path); BufferedImage img = null ; try { img = ImageIO.read(u); } catch (IOException e) { e.printStackTrace(); } return img; } } |
MyFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package util; import javax.swing.JFrame; import javax.swing.JPanel; /** * 游戲面板的父類 * @author long * */ public class MyFrame extends JPanel{ /** * 加載Frame的方法 */ public void launchFrame(){ JFrame frame = new JFrame( "MyGame" ); frame.add( this ); frame.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT); frame.setAlwaysOnTop( true ); // 設(shè)置其總在最上 frame.setLocationRelativeTo( null ); // 設(shè)置窗體初始位置 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible( true ); new PaintThread().start(); } /** * 定義一個重畫窗口的線程類,是一個內(nèi)部類 * @author dell * */ class PaintThread extends Thread { public void run(){ while ( true ){ repaint(); try { Thread.sleep( 40 ); //1s = 1000ms } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { new MyFrame().launchFrame(); } } |
2)主要的事件處理類---solar包中
--Planet類 行星類繼承至Star類
--SolarFrame類 游戲主面板類繼承至MyFrame類
--Star類 星球類,各個星球的父類
--Test類 測試類,不需要說明
Planet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package solar; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import util.GameUtil; /** * 行星類,繼承至Star類 * @author long * */ public class Planet extends Star{ //除了圖片、坐標(biāo),行星沿著橢圓運(yùn)行:長軸、短軸、移動速度、旋轉(zhuǎn)角度。繞著某個star運(yùn)行 double longAxis; //橢圓長軸 double shortAxis; //橢圓短軸 double speed; //飛行速度 double degree; //旋轉(zhuǎn)角度 Star center; //圍繞行星 public void draw(Graphics g){ //g.drawImage(img, (int)x, (int)y, null); super .draw(g); drawTrace(g); move(); } public void drawTrace(Graphics g){ double traceX,traceY,traceWidth,traceHeight; traceX = (center.x+center.w/ 2 )-longAxis; traceY = (center.y+center.h/ 2 )-shortAxis; traceWidth = 2 *longAxis; traceHeight = 2 *shortAxis; Color c = g.getColor(); g.setColor(Color.blue); g.drawOval(( int )traceX, ( int )traceY, ( int )traceWidth, ( int )traceHeight); g.setColor(c); } public void move(){ //沿著橢圓軌跡飛行 x = center.x + longAxis * Math.cos(degree); y = center.y + shortAxis * Math.sin(degree); degree += speed; } public Planet(Image img, double x, double y){ super (img,x,y); } public Planet(String imgpath, double x, double y){ super (imgpath,x,y); } public Planet( Star center,Image img, double longAxis, double shortAxis, double speed) { super (); this .x = (center.x+center.w/ 2 ) + longAxis; this .y = (center.y+center.h/ 2 ) + shortAxis; this .img = img; this .longAxis = longAxis; this .shortAxis = shortAxis; this .speed = speed; this .center = center; } public Planet( Star center,String imgPath, double longAxis, double shortAxis, double speed) { this (center,GameUtil.getImage(imgPath),longAxis,shortAxis,speed); } } |
SolarFrame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package solar; import java.awt.Graphics; import java.awt.Image; import util.Constant; import util.GameUtil; import util.MyFrame; public class SolarFrame extends MyFrame{ int width = Constant.GAME_WIDTH/ 2 ; int height = Constant.GAME_HEIGHT/ 2 ; Image bg=GameUtil.getImage( "images/bg.png" ); Star sun = new Star( "images/sun.jpg" ,width,height); Planet earth = new Planet(sun, "images/earth.png" , 100 , 60 , 0.1 ); Planet mars = new Planet(sun, "images/mars.png" , 180 , 100 , 0.15 ); @Override public void paint(Graphics g) { g.drawImage(bg, 0 , 0 , null ); sun.draw(g); earth.draw(g); mars.draw(g); } public static void main(String[] args) { new SolarFrame().launchFrame(); } } |
Star.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
package solar; import java.awt.Graphics; import java.awt.Image; import util.GameUtil; public class Star { public Image img; public double x,y; int w,h; public void draw(Graphics g){ g.drawImage(img, ( int )x, ( int )y, null ); } public Star(){ } public Star(Image img){ this .img = img; this .w = img.getWidth( null ); this .h = img.getHeight( null ); } public Star(Image img, double x, double y){ this (img); this .x = x; this .y = y; } public Star(String imgPath, double x, double y){ this (GameUtil.getImage(imgPath),x,y); } } |
總結(jié):該小游戲?qū)Υa的封裝處理的比較好,便于程序的擴(kuò)充,體現(xiàn)了面向?qū)ο蟮膹?qiáng)大,不同的功能封裝在不同的類與方法中,把類的公共的部分封裝在父類中,提高代碼的重用性。前期各個類寫的過程中會有各種小問題與細(xì)節(jié),但處理完這些后,后期想擴(kuò)充行星的個數(shù)就比較簡單了,new一個行星對象,然后畫的面板上即可。面向?qū)ο笏睿@只是初步小涉獵,仍需繼續(xù)努力專研!!!
以上就是Java太陽系小游戲分析和源碼詳解,希望對大家學(xué)習(xí)java語言有所幫助。