Java Solar System Small game analysis and source code detailed _java

Source: Internet
Author: User
Tags thread class

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

Use of knowledge points: Class inheritance, method overload and rewrite, polymorphism, encapsulation, etc.

Analysis:

1. Need to load pictures, paint

2. Build a panel, home page

3. Planetary class

Effect Chart:

Let's take a look at the source structure chart:

Now step through the functionality 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 structure of the game panel and is used for the parent class of each panel

------do this in order to encapsulate the data and to facilitate the expansion of the program

Constant.java

Package util; 
 
public class Constant {public 
  static final int game_width =; 
  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 tool class typically constructs the 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's parent class * @author long * */public class MyFrame extends jpanel{/** * Loading Frame method * * * Publ 
    IC 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); 
    Sets the initial position of the form frame.setdefaultcloseoperation (Jframe.exit_on_close); 
     
    Frame.setvisible (TRUE); 
  New Paintthread (). Start (); /** * Defines a thread class for a redraw window, is an internal class * @author Dell * * */class Paintthread extends thread {PU 
        Blic 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 The main Event processing class---solar package
--planet class planets inherit to star class

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

The--star 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; /** * Planet Class, inherited to Star class * @author long * */public class Planet extends star{//except for pictures, coordinates, the planets run along the ellipse: Long axis, short axis, moving speed, rotation angle.  Running a double longaxis around a star; Elliptic long axis double shortaxis;   Elliptic short axis double speed;   Flying speed double degree;    Rotation angle 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 () {//flying along an elliptical trajectory 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; 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); 
  } 
   
 

Summary: the small game of code encapsulation processing is better, easy to expand the program, embodies the powerful object-oriented, different functions encapsulated in different classes and methods, the public parts of the class encapsulated in the parent class, improve the reusability of the code. In the early stages of writing the process will have a variety of small problems and details, but after processing these, the latter want to expand the number of planets is relatively simple, new a planetary object, and then draw the Panel on it. Object-oriented water too deep, this is only a preliminary small dabble, still need to continue efforts to study!!!

The above is the Java Solar System games analysis and source code detailed, I hope to learn the Java language Help.

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.