Java Solar System Small game analysis and source code

Source: Internet
Author: User
Tags thread class

Java Solar System Small game analysis and source code

-20150809


Recently looked at some object-oriented knowledge. Then follow the teacher's commentary made 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


Let's take a look at the source code structure diagram:


Now analyze the functions of each class progressively:

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. Parent class for each panel

------did so. The purpose is to encapsulate the data. Facilitates the expansion of programs


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 c2/>//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 window initial position frame.setdefaultcloseoperation (jframe.exit_on_close); frame.setvisible (true); new Paintthread (). Start () ;} /** * Defines a thread class for redrawing a window.  is an inner class * @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) Basic event handling Class---solar bag

--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 need to explain


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 are executed along the ellipse: Long axis, short axis, movement speed, rotation angle.

Executes 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 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 = Shortaxis;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 package of code to deal with better, easy to expand the program. Embodies the powerful object-oriented, different functions encapsulated in different classes and methods. The common part of the class is encapsulated in the parent class, which improves the reusability of the code. There will be a variety of small problems and details in the early stages of writing, but after processing these, it is easier to expand the number of planets at a later stage, and a new planetary object and then a panel can be painted. Object-oriented water is too deep, this is only a preliminary small, still need to continue efforts to research.!!





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.