Java小案例(行星移動)

來源:互聯網
上載者:User

標籤:座標   getc   while   .net   sleep   ada   java.net   dmi   str   

Java小案例

行星移動:參考:三百集

使用軟體:idea2017,java

1,圖片集:這裡  (idea圖片源放在target目錄下,才能訪問到),建議從小往上看。。。

2,定義MyFrame

package my.university;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;public class MyFrame extends Frame {    public void launchFram(){        setSize(Content.Game_With,Content.Game_High);        setLocation(100,100);        setVisible(true);        new PaintThread().start();        addWindowListener(new WindowAdapter() {            @Override            public void windowClosing(WindowEvent e) {                System.exit(0);            }        });    }   private class  PaintThread extends Thread{        public void run(){            while(true){                repaint();                try {                    Thread.sleep(50);                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }   }}

2,定義ImageUtil類:

package my.university;import javax.imageio.ImageIO;import java.awt.*;import java.awt.image.BufferedImage;import java.io.IOException;import java.net.URL;public class ImageUtil {    private ImageUtil(){}//工具類構成私人的,?    public static Image 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();        }        return image;    }}

3,定義常量類,相當於設定檔

package my.university;public class Content {    public static final int Game_With=500;    public static final int Game_High=500;}

4,定義Start類

package my.university;import java.awt.*;public class Start {    public double x;    public double y;    public double with;    public double height;    Image image;    public Start(){}//預設構造起,用於子類繼承    public Start(Image image){        this.image=image;        this.with=image.getWidth(null);        this.height=image.getHeight(null);    }    public Start(Image image,double x,double y){        this(image);        this.x=x;        this.y=y;    }    public Start(String path,double x,double y){        this(ImageUtil.getImage(path),x,y);    }    public void draw(Graphics g){        g.drawImage(image,(int)x,(int)y,null);    }}

5,定義Plant類

package my.university;import java.awt.*;/** * Created by Administrator on 2017/8/12. */public class Plant extends Start {    double longAxis;  //橢圓的長軸    double shortAxis;  //橢圓的短軸    double speed;     //飛行的速度    double degree;    Start center;    boolean statellite;    public Plant(){}    public Plant(Start center,String path,double longAxis,double shortAxis,double speed){        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;;    }    public Plant(Start center,String path,double longAxis,double shortAxis,double speed,Boolean statellite){       this(center, path, longAxis, shortAxis, speed);       this.statellite=statellite;    }    public Plant(Image image){        super(image);    }    public Plant(Image image,double x,double y){        super(image, x, y);    }    //畫軌跡    public void drawTrace(Graphics g){        double ovalX,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,(int)ovalHeight);//定義矩形的左上方座標及寬和高,在矩形中畫橢圓 g.setColor(c); } //移動 public void move(){ x=center.x+center.with/2+longAxis*Math.cos(degree); y=center.y+center.height/2+shortAxis*Math.sin(degree); degree+=speed; } public void draw(Graphics g){ super.draw(g); move(); if(!statellite){ drawTrace(g); } }}

6,定以調用類

package my.university;import java.awt.*;public class SolarFrame extends MyFrame {    Image bg=ImageUtil.getImage("images/bg.jpg");    Start sun=new Start("images/sun.jpg",Content.Game_With/2,Content.Game_High/2);    Plant mercurys = new Plant(sun, "images/Mercury.jpg", 50, 30, 0.2);    Plant venus = new Plant(sun, "images/Venus.jpg", 60, 40, 0.3);    Plant earth=new Plant(sun,"images/Earth.jpg",100,50,0.4);    Plant mars = new Plant(sun, "images/Mars.jpg", 120, 60, 0.5);    Plant jupiter = new Plant(sun, "images/jupiter.jpg", 140, 70, 0.6);    Plant saturn =new Plant(earth,"images/Saturn.jpg",160,80,0.7,true);    Plant uranus = new Plant(sun, "images/Uranus.jpg", 180, 90, 0.8);    Plant neptune = new Plant(sun, "images/Neptune.jpg", 200, 100, 0.9);    Plant moon = new Plant(earth, "images/moon.jpg", 30, 20, 0.3,true);    public void paint(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 void main(String[] args){        new SolarFrame().launchFram();    }}

最後附上代碼

 

Java小案例(行星移動)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.