Recently, I read some of the knowledge -oriented knowledge, and then followed the teacher's explanation to make a mini -game around the sun around the sun to practice and consolidate the recent learning knowledge:
Using knowledge points: inheritance of class, loading and rewriting of methods, polymorphism, packaging, etc.
analyze:
1. Need to load pictures and drawings
2. Build a panel, home page
3. Planet
Effect map:
First look at the source code structure diagram:
Now gradually analyze the functions of each class:
1) Tools ----- Util package
--Constant class encapsulated the constant used in the game
--Gameutil class encapsulates the picture loading function of the game
--Myframe classes the structure of the game panel for the parent class of each panel
------ The reason for this is to encapsulate data to 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_heigh = 600;} Gameutil.java
Package Util; Import Java.awt.image; Import Java.awt.image.buffEredImage; Import Java.io.ioException; Import Java.net.url; Import Imageio.imageio; /*** Tool class (loaded picture) * @AUTHOR Long * */Public Class Gametil {Private Gametil () {} // The tool class usually constructs the constructor private Public Static Getimage (String Path) {URL U = Gameuti. l.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; /*** The parent class of the gaming panel* @Author Long** /Public Class Myframe Extends Jpanel { /** * Method of loading frame */ public void launchframe () {jframe frame = new jframe ("mygame"); frame.add (this); frame.setsize (constant.game_width, constant.game_height); me.setalwaySontop (true); // Set its best Frame.setLocationRelationoTo (null); // Set the initial position of the window Frame.setDefaultCloseoperation (jframe.exit_on_Close); ad (). Start ();} /*** Define a replaceable window The thread class is an internal class * @Author Dell * */Class PainThreads Threads Thread {Public Void Run () {While (TRUE) {repain (); TRY {Thread.sleep (40); // 1S = 1000M s} catch (InterruptedException E) {e.printstacktrace ();}}} public static void main (string [] args) {new myframe (). Launchframe ();}} 2) Main event handling class---in the solar package
-Planet class planets inherit to Star class
-Solarframe game main panels inherit to MyFrame class
-Star-class planet, the father 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; /*** Planets, inherited to Star OR Long * */ Public Class Planet Extends Star {// Except for pictures and coordinates, the planet runs along the ellipse: long shaft, short shaft, movement speed, rotation angle. Run Double Longaxis around a star; // The ellipse long shaft double shortaxis; // The elliptical short shaft Double SPEED; // Flying speed double deadree; // Rotate angle; // raphics g) {//g.DrawImage (ING, (int) x, (int) y, null); super.draw (g); drawtrace (g); move ();} Public void drawtrace (Graphics G) {diety ,, Tracey, TraceWidth, Traceheight; Tracex = (Center.x+Center.w/2) -longaxis; Tracey = (Center.y+Center.h/2) -Shortaxis; is; Traceheight = 2*Shortaxis; Color C = g.getColor (); g.SetColor (color.blue); g.Drawoval ((int) tracex, (int) tracey, (int) traceWidth, (int) traceHeight); ;; } PUBLIC VOID MOVE () {// Flying along the ellipse trajectory x = center.x + longaxis * math.cos (deGree); y = center.y + shortaxis * math.sin (deGree); } Public Planet (Image Img, Double X, Double Y) {Super (IMG, X, Y);} Public Planet (String IMGPATH, Double X, Double Y) {Super (IMGPATH, X, Y);} Bil 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 /h /h /h /h /h/ 2) + shortaxis; this.img = img; this.longaxis = longaxis; this.shortaxis = shortaxis; this.Speed = speed; this.Center = center; ET (Star Center, String Imgpath, Double Longaxis, Double Shortaxis , DOUBLE SPEED) {this (center, Gametil.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; Lass Solarframe Extends MyFrame {int Width = Constant.game_width/2; int Height = Constant.game_height/2; Image BG = Gametil.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) { WIMAGE (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 , Y; int W, H;; Public void Draw .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, D OUBLE X ,double y){ this(GameUtil.getImage(imgPath),x,y); } }Summary: This mini -game is better handled by the encapsulation of the code, which facilitates the expansion of the program, and reflects the strength of the object -oriented. Different functions are encapsulated in different classes and methods. Improve the reuse of code. There will be various small problems and details in the process of various categories in the early stage, but after dealing with these, the number of planets in the later period is relatively simple. One planetary object is available, and then the panel drawn can be used. Object -oriented water is too deep, this is just a preliminary involvement, and still needs to continue to work hard! Intersection Intersection
The above is the analysis of the Java solar game analysis and source code. I hope it will be helpful for everyone to learn Java language.