Java Solar System Small game analysis and source code

Source: Internet
Author: User

Java Solar System Small game analysis and source code

-20150809


Recently looked at some object-oriented knowledge, and then followed the teacher's explanation to do a solar system of the planets around the sun to the game, to practice consolidating the knowledge of recent studies:

Knowledge points: Inheritance of classes, overloading and rewriting of methods, polymorphism, encapsulation, etc.

Analysis:

1. Need to load pictures, drawings

2. Build a panel, main page

3. Planetary class

。。。


First look at the source code structure diagram:


Now gradually analyze the functions of each class:

1) Tool class-----Util package

The--constant class encapsulates the constants used in the game

The--gameutil class encapsulates the game's picture loading function

The--myframe class encapsulates the construction of the game panel for each panel's parent class

------is doing so in order to encapsulate the data and facilitate the expansion of the program


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;/** * Tool class (load picture) * @author long * */public class Gameutil {private Gameutil () {}< The c1/>//tool class will typically construct a method private 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;/** * Game Panel Parent class * @author long * */public class MyFrame Ext Ends jpanel{/** * Method of loading frame */public void Launchframe () {JFrame frame = new JFrame ("Mygame"); Frame.add (this); Frame.setsize (constant.game_width,constant.game_height); Frame.setalwaysontop (true); Set its total at the top frame.setlocationrelativeto (null); Set the initial position of the form frame.setdefaultcloseoperation (jframe.exit_on_close); frame.setvisible (true); new Paintthread (). Start () ;}  /** * Defines a redraw window for the thread class, is an inner class * @author Dell * */class Paintthread extends thread {public void run () {while (true) {repaint (); try {Thread.Sleep (+);//1s = 1000ms} catch (Interruptedexception e) {e.printstacktrace ();}}}   } public static void Main (string[] args) {new MyFrame (). Launchframe ();}}

2) Main Event handling Class---Solar package

--planet class of planets inherit to star class

--solarframe class game main panel class inherits to MyFrame class

--star Planet Class, the parent of each planet

--test class test class, no description required


Planet.java

Package Solar;import java.awt.color;import java.awt.graphics;import java.awt.image;import util. gameutil;/** * Planetary class, inheriting to Star class * @author long * */public class Planet extends star{//In addition to pictures, coordinates, the planets run along the ellipse: Long axis, short axis, movement speed, rotation angle.   Run a double longaxis around a star;  Oval long axis double Shortaxis;      Elliptic short axis double speed;     Flight speed double degree;       Rotation Angle Star Center; Round the planet public void Draw (Graphics g) {//g.drawimage (img, (int) x, (int) y, null); Super.draw (g);d rawtrace (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 () {//Fly along elliptical trajectory x = center.x + Longaxis * Math.Cos (degree); y = center.y + Shortaxis * Math.sin (degree);d Egree + = 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 = ShortA Xis;this.speed = Speed;this.center = center;} Public Planet (Star center,string imgpath,double longaxis, double shortaxis,double speed) {This (center,gameutil.getimag E (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;i Mage 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), @Overridepublic 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);}}


-----------------------------------------------------------------------------------------------


Summary: The small game on the packaging of the code is better, easy to expand the program, embodies the powerful object-oriented, different functions encapsulated in different classes and methods, the common parts of the class encapsulated in the parent class, improve code reusability. There will be a variety of small problems and details in the early stages of writing, but after processing these, the number of planets to be expanded later is relatively simple, new a planetary object, and then the panel can be painted. Object-oriented water is too deep, this is only a preliminary small, still need to continue efforts to research!!!





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Solar System Small game analysis and source code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.