Java solar mini-games analysis and source code, java solar mini-games
Java solar system mini-Game Analysis and source code
-20150809
I recently read some object-oriented knowledge, and then followed the teacher's explanation to make a small game about the conversion of various planets in the solar system around the sun to practice and consolidate my recent knowledge:
Knowledge points used: class inheritance, method overloading and rewriting, polymorphism, encapsulation, etc.
Analysis:
1. Images and drawings need to be loaded
2. Create a panel on the home page
3. Planetary
...
:
Let's take a look at the source code structure:
Now we will analyze the functions of each class:
1) tool class ----- util package
-- The Constant class encapsulates constants used in the game
-- GameUtil class encapsulates the game's image loading Function
-- MyFrame class encapsulates the structure of the game panel for the parent class of each panel
------ The purpose of doing so is to encapsulate data and facilitate program Expansion
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 (image loading) * @ author long **/public class GameUtil {private GameUtil () {} // The tool class usually uses the construction 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;/*** parent class of the game panel * @ author long **/public class MyFrame extends JPanel {/*** Method for loading the Frame */public void launchFrame () {JFrame frame = new JFrame ("MyGame"); frame. add (this); frame. setSize (Constant. GAME_WIDTH, Constant. GAME_HEIGHT); frame. setAlwaysOnTop (true); // you can specify the preceding frame. setLocationRelativeTo (null); // sets the initial frame position of the form. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); frame. setVisible (true); new PaintThread (). start () ;}/ *** defines a Thread class for the redraw window. It is an internal class * @ author dell **/class PaintThread extends Thread {public void run () {while (true) {repaint (); try {Thread. sleep (40); // 1 s = 1000 ms} catch (InterruptedException e) {e. printStackTrace () ;}}} public static void main (String [] args) {new MyFrame (). launchFrame ();}}
2) Main Event processing class --- in the solar package
-- The Planet class inherits from the Star class.
-- The main panel class of the SolarFrame game inherits from the MyFrame class.
-- Star planet category, parent category of each planet
-- Test class, not required
Planet. java
Package solar; import java. awt. color; import java. awt. graphics; import java. awt. image; import util. gameUtil;/*** planetary class, inherited to Star class * @ author long **/public class Planet extends Star {// In addition to images and coordinates, the Planet runs along the elliptic: long axis, short axis, moving speed, rotation angle. Run double longAxis around a star; // double shortAxis on the long axis of the elliptic model; // double speed on the short axis of the elliptic model; // double degree on the flight speed; // The Rotation Angle of the Star center; // around the planet 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 () {// fly x = center along the elliptical trajectory. 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);@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);}}
Bytes -----------------------------------------------------------------------------------------------
Summary: this mini-game is better at code encapsulation and convenient for program expansion, reflecting the powerful object-oriented model. Different functions are encapsulated in different classes and methods, encapsulate the public part of the class in the parent class to improve code reusability. There are various small issues and details in the early writing process, but after processing these issues, it is easier to expand the number of planets in the later stage. A new planetary object, then you can draw the panel. The object-oriented water is too deep. This is just a preliminary research, and we still need to work hard !!!
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.