Java Solar System Model small Project

Source: Internet
Author: User
Tags thread class

First define a window class

Package cn.hxd.util;

Import Java.awt.Frame;
Import Java.awt.event.WindowAdapter;
Import java.awt.event.WindowEvent;

/**
* Common methods used in game projects
* @author HXD
*
*/

public class MyFrame extends frame{

/**
* Load Window
*/
public void Launchframe () {
SetSize (Constant.game_width,constant.game_height);
SetLocation (200,200);
SetVisible (TRUE);

New Paintthread (). Start ();//start Redraw thread

Addwindowlistener (New Windowadapter () {

@Override
public void windowclosing (WindowEvent e) {//Click Close window
System.exit (0);
}
});
}

/**
* A thread class that defines a redraw window, is an inner class
* @author HXD
*
*/
Class Paintthread extends thread{
public void Run () {
while (true) {
Repaint ();
try {
Thread.Sleep (80);
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
}
}

}

Tool class for loading pictures

Package cn.hxd.util;

Import Java.awt.Image;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Java.net.URL;

Import Javax.imageio.ImageIO;
/**
* Tools commonly used in game development
* @author HXD
*
*/

public class Gameutil {

Private Gameutil () {}//Tool class sets the constructor to 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;//bufferedimage is the image subclass, which is also correctly returned
}

}

Put the constants in your project in a constant class

Package cn.hxd.util;
/**
* Constants used in the game project
* @author HXD
*
*/
public class Constant {
public static final int game_width=1360;
public static final int game_height=760;
}

Planet Class

Package Cn.hxd.SolarSystem;

Import Java.awt.Graphics;
Import Java.awt.Image;
Import Cn.hxd.util.GameUtil;

/**
* Planet Class
* @author HXD
*
*/
public class Star {
Image img;
Double x, y;
int width,height;

public void Draw (Graphics g) {
G.drawimage (IMG, (int) x, (int) y, null);
}

Public Star () {//subclass to invoke the default constructor of the parent class
}
Public Star (Image img) {
this.img=img;
This.width=img.getwidth (NULL);
This.height=img.getheight (NULL);
}

Public Star (Image img,double x,double y) {
This (IMG);
This.x=x;
This.y=y;

}
Import
Public Star (String imgpath,double x,double y) {
This (Gameutil.getimage (Imgpath), x, y);
}
}

Planetary class

Package Cn.hxd.SolarSystem;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Image;
Import Cn.hxd.util.GameUtil;

/**
* Planetary class, Inheriting planet class
* @author HXD
*
*/
public class Planet extends Star {
In addition to pictures, coordinates, planets run along an ellipse: a long axis, a short axis, a speed, an angle, a star flying around
Double Longaxis;
Double Shortaxis;
Double speed;
Double degree;
Star Center;
Boolean satellite; See if it's a satellite.


You need to call the empty constructor of the parent class
Public Planet (String Imgpath, double Longaxis, double shortaxis, double speed, Star center) {
Super (Gameutil.getimage (Imgpath));

This.longaxis = Longaxis; The long axis of the current planet
This.shortaxis = Shortaxis;

This.speed = speed;
This.center=center;
This.x=center.x+longaxis; The initial position of the planet
THIS.Y=CENTER.Y;

This.width=img.getwidth (NULL);
This.height=img.getheight (NULL);
}

Public Planet (String Imgpath, double Longaxis, double Shortaxis, double Speed,star Center, Boolean satellite) {
This (Imgpath, longaxis,shortaxis,speed,center);
This.satellite=satellite;

}
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 void Draw (Graphics g) {
Super.draw (g);
if (!satellite) {//Not a satellite to draw a trajectory
Drawtrance (g);
}
Move ();
}
Draw the trajectory of a planet
public void Drawtrance (Graphics g) {
Double oval_x, oval_y, Oval_width, oval_height;
oval_width=longaxis*2;
oval_height=shortaxis*2;
oval_x= (CENTER.X+CENTER.WIDTH/2)-longaxis;//left upper vertex (center. x + X.WIDTH/2)-long axis
Oval_y= (CENTER.Y+CENTER.HEIGHT/2)-shortaxis;

Color C=g.getcolor ();
G.setcolor (Color.Blue); Set Track color
G.drawoval ((int) oval_x, (int) oval_y, (int) oval_width, (int) oval_height);
G.setcolor (c);
}

public void Move () {
Fly along an elliptical trajectory
x= (CENTER.X+CENTER.WIDTH/2) +longaxis*math.cos (degree);
Y= (CENTER.Y+CENTER.HEIGHT/2) +shortaxis*math.sin (degree);
Degree+=speed;
}
}

Solar System main Window

Package Cn.hxd.SolarSystem;

Import Java.awt.Graphics;
Import Java.awt.Image;

Import cn.hxd.util.Constant;
Import Cn.hxd.util.GameUtil;
Import Cn.hxd.util.MyFrame;

/**
* Main window of the solar system
* @author HXD
*
*/
public class Solarframe extends MyFrame {

Public Solarframe () {}

Private Image IBuffer;
Private Graphics Gbuffer;
Solarframe (String s) {
Super (s);
// }
Import background
Image bg = gameutil.getimage ("images/bg.jpg");
Here is the use of encapsulated classes to import pictures
Star Sun = new Star ("Images/ty.png", CONSTANT.GAME_WIDTH/2,CONSTANT.GAME_HEIGHT/2);
Planet earth = new Planet ("Images/dq.png", Max, 0.1, Sun);
Planet moon= New Planet ("images/moon.jpg", +, 0.2, earth,true);
Planet mars = new Planet ("Images/mars.jpg", 230,0.07, Sun);
Planet mx = new Planet ("images/mx.jpg", +, 0.05, Sun);
/**
25 * Can continue to add other planets, just one line of code (already packaged)
26 * ...
27 * ...
28 * ...
29 */



/**
* Rewrite the redraw function, which is a callback function that needs to be implemented and then automatically called by the system
*/
public void Paint (Graphics g) {
G.drawimage (BG, 0, 0, NULL);
Sun.draw (g); The encapsulation method is used here.
Earth.draw (g);
Mars.draw (g);
Mx.draw (g);
Moon.draw (g);
}
Java double buffering to prevent screen flicker
public void update (Graphics SCR)
{
if (ibuffer==null)
{
Ibuffer=createimage (This.getsize (). Width,this.getsize (). height);
Gbuffer=ibuffer.getgraphics ();
}
Gbuffer.setcolor (Getbackground ());
Gbuffer.fillrect (0,0,this.getsize (). Width,this.getsize (). height);
Paint (Gbuffer);
Scr.drawimage (Ibuffer,0,0,this);
}


public static void Main (string[] args) {
New Solarframe (). Launchframe ();
}
}

Java Solar System Model small Project

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.