Java Small case (planetary movement)

Source: Internet
Author: User

Java Small Case

Planetary Movement: Reference: 300 episodes

Using the software: Idea2017,java

1, Photo gallery: Here (idea image source placed in the target directory, to access to), it is recommended to look up from small ...

2, define MyFrame

 Packagemy.university;Importjava.awt.*;ImportJava.awt.event.WindowAdapter;Importjava.awt.event.WindowEvent; Public classMyFrameextendsFrame { Public voidLaunchfram () {setSize (Content.game_with,content.game_high); SetLocation (100,100); SetVisible (true); NewPaintthread (). Start (); Addwindowlistener (NewWindowadapter () {@Override Public voidwindowclosing (windowevent e) {system.exit (0);    }        }); }   Private classPaintthreadextendsthread{ Public voidrun () { while(true) {repaint (); Try{Thread.Sleep (50); } Catch(interruptedexception e) {e.printstacktrace (); }            }        }   }}

2, define the Imageutil class:

 Packagemy.university;ImportJavax.imageio.ImageIO;Importjava.awt.*;ImportJava.awt.image.BufferedImage;Importjava.io.IOException;ImportJava.net.URL; Public classImageutil {PrivateImageutil () {}//does the tool class constitute a private,?     Public StaticImage getImage (String path) {URL URL=imageutil.class. getresource (path); /*System.out.println (ImageUtil.class.getClassLoader (). GetResource ("")); Result is file:/d:/xxxxx/sparkdemo/sparksql/target/classes/*/bufferedimage Image=NULL; Try{image=imageio.read (URL); } Catch(IOException e) {e.printstacktrace (); }        returnimage; }}

3, define the constant class, equivalent to the configuration file

 Package my.university;  Public class Content {    publicstaticfinalint game_with=500;      Public Static Final int game_high=500;}

4, defining the Start class

 Packagemy.university;Importjava.awt.*; Public classStart { Public Doublex;  Public Doubley;  Public DoubleWith ;  Public Doubleheight;    Image Image;  PublicStart () {}//default constructs, for subclass inheritance     PublicStart (image image) { This. image=image;  This. With=image.getwidth (NULL);  This. Height=image.getheight (NULL); }     PublicStart (image Image,DoubleXDoubley) {         This(image);  This. x=x;  This. y=y; }     PublicStart (String Path,DoubleXDoubley) {         This(Imageutil.getimage (path), x, y); }     Public voidDraw (Graphics g) {g.drawimage (image, (int) x, (int) Y,NULL); }}

5, defining the plant class

 Packagemy.university;Importjava.awt.*;/*** Created by Administrator on 2017/8/12.*/ Public classPlantextendsStart {DoubleLongaxis;//The long axis of an ellipse    DoubleShortaxis;//the short axis of an ellipse    DoubleSpeed//Speed of Flight    Doubledegree;    Start Center; BooleanStatellite;  PublicPlant () {} PublicPlant (Start center,string Path,DoubleLongaxis,DoubleShortaxis,DoubleSpeed ) {        Super(Imageutil.getimage (path));  This. center=Center;  This. x=center.x+Longaxis;  This. y=Center.y;  This. longaxis=Longaxis;  This. shortaxis=Shortaxis;  This. speed=Speed ;; }     PublicPlant (Start center,string Path,DoubleLongaxis,DoubleShortaxis,DoubleSpeed,boolean Statellite) {        This(center, Path, Longaxis, Shortaxis, speed);  This. statellite=Statellite; }     PublicPlant (image image) {Super(image); }     PublicPlant (image Image,DoubleXDoubley) {        Super(image, X, y); }    //Draw Track     Public voidDrawtrace (Graphics g) {DoubleOvalx,ovaly,ovalwidth,ovalheight; Ovalwidth= Longaxis*2; Ovalheight= Shortaxis*2; Ovalx= (CENTER.X+CENTER.WITH/2)-Longaxis; Ovaly= (CENTER.Y+CENTER.HEIGHT/2)-Shortaxis; Color C=G.getcolor (); G.setcolor (Color.Blue);
G.drawoval ((int) Ovalx, (int) Ovaly, (int) Ovalwidth, (intovalheight);//define the upper-left coordinate of the rectangle and the width and height, and draw an ellipse in the rectangle G.setcolor (c); } //Moving Public voidMove () {x=center.x+center.with/2+longaxis*Math.Cos (degree); Y=center.y+center.height/2+shortaxis*Math.sin (degree); Degree+=Speed ; } Public voidDraw (Graphics g) {Super. Draw (g); Move (); if(!statellite) {Drawtrace (g); } }}

6, set to invoke class

 Packagemy.university;Importjava.awt.*; Public classSolarframeextendsmyframe {Image bg=imageutil.getimage ("Images/bg.jpg"); Start Sun=NewStart ("Images/sun.jpg", CONTENT.GAME_WITH/2,CONTENT.GAME_HIGH/2); Plant Mercurys=NewPlant (Sun, "images/mercury.jpg", 50, 30, 0.2); Plant Venus=NewPlant (Sun, "images/venus.jpg", 60, 40, 0.3); Plant Earth=NewPlant (Sun, "images/earth.jpg", 100,50,0.4); Plant Mars=NewPlant (Sun, "images/mars.jpg", 120, 60, 0.5); Plant Jupiter=NewPlant (Sun, "images/jupiter.jpg", 140, 70, 0.6); Plant Saturn=NewPlant (Earth, "images/saturn.jpg", 160,80,0.7,true); Plant Uranus=NewPlant (Sun, "images/uranus.jpg", 180, 90, 0.8); Plant Neptune=NewPlant (Sun, "images/neptune.jpg", 200, 100, 0.9); Plant Moon=NewPlant (Earth, "images/moon.jpg", 30, 20, 0.3,true);  Public voidPaint (Graphics g) {g.drawimage (bg,0,0,NULL);        Sun.draw (g);        Mercurys.draw (g);        Venus.draw (g);        Earth.draw (g);        Mars.draw (g);        Jupiter.draw (g);        Saturn.draw (g);        Uranus.draw (g);        Neptune.draw (g);    Moon.draw (g); }     Public Static voidMain (string[] args) {Newsolarframe (). Launchfram (); }}

Finally, attach the code

Java Small case (planetary movement)

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.