简单的太阳系模型

很早前学视频的一个行星绕太阳转的模型,没事做着玩的不错
这里写图片描述
主窗口


public class MyFrame extends Frame{



public void lanchFrame(){

        setSize(constant.GAME_WIDTH,constant.GAME_HEIGHT);
        setLocation(50, 50);
        setVisible(true);





    addWindowListener(new WindowAdapter(){

        @Override
        public void windowClosing(WindowEvent e) {  //窗口关闭
        System.exit(0);
        }

    });


    new PaintThread().start();
}

  /*
   * 线程类,控制元素
   */

class PaintThread extends Thread{

       public void run(){
           while(true){
               repaint();
               try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
           }
       }
   }
}

主要天体及行星的布置

public class Planes extends Star{

    double longAxis;        //长轴
    double shortAxis;      //短轴
    double speed;
    double degree;
    Star center;

    boolean satlittle;


    public void draw(Graphics g){
        super.draw(g);
        drawTrace(g);
        move();
        if(!satlittle){
            drawTrace(g);
        }
    }   

    public  void move(){
        x=center.x+center.width/2+longAxis*Math.cos(degree);
        y=center.y+center.heigth/2+shortAxis*Math.sin(degree);

        degree+=speed;

}

    public void drawTrace(Graphics g){

        int _x,_y,_width,_height;
        _width=(int) (longAxis*2);
        _height=(int) (shortAxis*2);
        _x=(int) ((center.x+center.width/2)-longAxis);
        _y=(int) ((center.y+center.heigth/2)-shortAxis);
        Color c=g.getColor();
        g.setColor(Color.blue);
        g.drawOval((int)_x, (int)_y, (int)_width, (int)_height);

        g.setColor(c);
    }





    public Planes(Star center,String imgpath,  double longAxis,
            double shortAxis, double speed) {

        this.center=center;
        this.x=center.x+longAxis;
        this.y=center.y;
        this.img=Gameutil.getImage(imgpath);

        this.width=img.getWidth(null);
        this.heigth=img.getHeight(null);

        this.longAxis = longAxis;
        this.shortAxis = shortAxis;
        this.speed = speed;

    }

    public Planes(Star center,String imgpath,  double longAxis,
            double shortAxis, double speed,boolean satlittle) {

          this(center, imgpath, longAxis, shortAxis, speed);
          this.satlittle=satlittle;
    }


    public Planes(Image img,double x,double y){
        super(img,x,y);
    }   

    public Planes(String imgpath,double x,double y){
        super(imgpath, x, y);

    }

}
package cn.hfz.uil;

import java.awt.Graphics;
import java.awt.Image;

import cn.hfz.solor.Gameutil;

public class Star {

    Image img;
    double x,y;
    int width,heigth;

    public void draw(Graphics g){
        g.drawImage(img, (int)x, (int)y,null);
    }


    public Star(){}

    public Star(Image img,double x,double y){
        this.img=img;
        this.x=x;
        this.y=y;
        this.width=img.getWidth(null);
        this.heigth=img.getHeight(null);
    }
    //封装
    public Star(String imgpath,double x,double y){
         this(Gameutil.getImage(imgpath),x,y);
    }

}

方位,图片

package cn.hfz.uil;

import java.awt.Graphics;
import java.awt.Image;

import cn.hfz.solor.Gameutil;
import cn.hfz.solor.MyFrame;

public class GameSolor extends MyFrame {

    Image backg=Gameutil.getImage("img/sky.gif");
    Star sun=new Star("img/sun.gif", 350, 300);
    Planes earth=new Planes(sun, "img/earth.gif", 150, 100,0.06);
    Planes moon=new Planes(earth, "img/moon.gif", 30, 20,0.05,true);
    Planes Mars=new Planes(sun, "img/Mars.gif", 200, 130,0.05);
    Planes shuixin=new Planes(sun, "img/水星.gif",60 ,50 , 0.08);
    Planes jinxin=new Planes(sun, "img/金星.gif", 100, 60,0.07);
    Planes Muxin=new Planes(sun, "img/木星.gif",220 , 155, 0.04);
    Planes Tuxin=new Planes(sun, "img/土星.gif", 250, 170, 0.035);
    Planes Tianwx=new Planes(sun, "img/天王星.gif", 280, 190, 0.03);
    Planes Haiwx=new Planes(sun, "img/海王星.gif", 310, 210, 0.025);

    public void paint(Graphics g){

        g.drawImage(backg,0, 0, null);
        sun.draw(g);
        earth.draw(g);
        Mars.draw(g);
        moon.draw(g);
        shuixin.draw(g);
        jinxin.draw(g);
        Muxin.draw(g);
        Tuxin.draw(g);
        Tianwx.draw(g);
        Haiwx.draw(g);



    }

    public static void main(String[] args){
           new GameSolor().lanchFrame();
        }


}

程序打包已上传,如下:

http://download.csdn.net/detail/t1012665655/9749752

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值