Java太陽系小游戲剖析和源碼詳解。本站提示廣大學習愛好者:(Java太陽系小游戲剖析和源碼詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是Java太陽系小游戲剖析和源碼詳解正文
比來看了面向對象的一些常識,然後隨著先生的講授做了一個太陽系各行星繞太陽轉的小游戲,來演習穩固一下比來學的常識:
用到常識點:類的繼續、辦法的重載與重寫、多態、封裝等
剖析:
1.須要加載圖片、繪圖
2.建一個面板,主頁面
3.行星類
後果圖:
先看一下源碼構造圖:
如今慢慢剖析各個類的功效:
1)對象類-----util包中
--Constant類 封裝了游戲頂用到的常量
--GameUtil類 封裝了游戲的圖片加載功效
--MyFrame類 封裝了游戲面板的結構,用於各面板的父類
------之所以如許做,目標是為了封裝數據,便於法式的擴大
Constant.java
package util; public class Constant { public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600; }
GameUtil.java
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(){ } //對象類平日將結構辦法公有 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
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); // 設置其總在最上 frame.setLocationRelativeTo(null); // 設置窗體初始地位 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); new PaintThread().start(); } /** * 界說一個重畫窗口的線程類,是一個外部類 * @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
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{ //除圖片、坐標,行星沿著橢圓運轉:長軸、短軸、挪動速度、扭轉角度。繞著某個star運轉 double longAxis; //橢圓長軸 double shortAxis; //橢圓短軸 double speed; //飛翔速度 double degree; //扭轉角度 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
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
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); } }
總結:該小游戲對代碼的封裝處置的比擬好,便於法式的擴大,表現了面向對象的壯大,分歧的功效封裝在分歧的類與辦法中,把類的公共的部門封裝在父類中,進步代碼的重用性。後期各個類寫的進程中會有各類小成績與細節,但處置完這些後,前期想擴大行星的個數就比擬簡略了,new一個行星對象,然後畫的面板上便可。面向對象水太深,這只是初步小浏覽,仍需持續盡力專研!!!
以上就是Java太陽系小游戲剖析和源碼詳解,願望對年夜家進修java說話有所贊助。