Java编写天体运动模型

天体界面

import java.awt.Graphics;
import java.awt.Image;
import until.Constant;
import until.GameUtil;
import until.Myframe;
public class SolarFrame extends Myframe{
Image bg=GameUtil.getImage("image/14.jpg");
Start sun=new Start("image/13.jpg", 250, 250);
Planet earth=new Planet("image/15.jpg", 100, 80, 0.1, sun);
Planet marth=new Planet("image/20.jpg", 200, 130, 0.05, sun);
Planet mar=new Planet("image/15.jpg", 20, 13, 0.2, earth);
Planet boll=new Planet("image/21.jpg", 50, 30, 0.2, sun);
public void paint(Graphics g) {
g.drawImage(bg, 0, 0, null);
sun.draw(g);
earth.draw(g);
marth.draw(g);
mar.draw(g);
boll.draw(g);
}
private Image offScreenImage=null;
public void update(Graphics g) {
if(offScreenImage==null) 
offScreenImage=this.createImage(Constant.GAME_HIGH,Constant.GAME_WIGHT);
Graphics goff=offScreenImage.getGraphics();
paint(goff);
g.drawImage(offScreenImage, 0, 0, null);
}
public static void main(String[] args) {
new SolarFrame().launchFrame();
}
}

绘制行星

package lss.solarSystem;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import until.GameUtil;
public class Planet extends Start{
double longAixt;
double shortAixt;
double speed;
double degree;
Start center;
public void draw(Graphics g) {
super.draw(g);
drawTrace(g);
move();
}
public void drawTrace(Graphics g) {
double tracex,tracey,tracewidth,traceheight;
tracewidth=longAixt*2;
traceheight=shortAixt*2;
tracex=(center.x center.width/2)-longAixt;
tracey=(center.y center.hight/2)-shortAixt;
Color c=g.getColor();
g.setColor(Color.blue);
g.drawOval((int)tracex,(int)tracey, (int)tracewidth,(int) traceheight);
g.setColor(c);
}
public void move() {
x=(center.x center.width/2) longAixt*Math.cos(degree);
y=(center.y center.hight/2) shortAixt*Math.sin(degree);
degree =speed;
}
public Planet(String imgpath, double longAixt, double shortAixt, double speed, Start center) {
super(GameUtil.getImage(imgpath));
this.center=center;
this.x=center.x longAixt;
this.y=center.y;
this.img=GameUtil.getImage(imgpath);
this.longAixt = longAixt;
this.shortAixt = shortAixt;
this.speed = speed;
this.width=img.getWidth(null);
this.hight=img.getHeight(null);
}
public Planet(Image img, double x, double y) {
super(img, x, y);
}
}

绘制中心天体

package lss.solarSystem;
import java.awt.Graphics;
import java.awt.Image;
import until.GameUtil;
public class Start {
Image img;
double x,y;
int width,hight;
public void draw(Graphics g) {
g.drawImage(img,(int) x,(int) y, null);
}
public Start() {
}
public Start(Image img) {
this.img=img;
this.width=img.getWidth(null);
this.hight=img.getHeight(null);
}
public Start(Image img,double x,double y) {
this(img);
this.x=x;
this.y=y;
}
public Start(String imgpath,double x,double y) {
this(GameUtil.getImage(imgpath),x,y);
}
}

工具包—常量

public class Constant {
	public static final int GAME_HIGH=500;
	public static final int GAME_WIGHT=500;
}

游戏工具

public class GameUtil {
	private GameUtil() {}
	public static Image getImage(String path) {
		return Toolkit.getDefaultToolkit().getImage(GameUtil.class.getClassLoader().getResource(path));
}
}

窗口

public class Myframe extends Frame {
	public void launchFrame() {
		setSize(Constant.GAME_HIGH,Constant.GAME_WIGHT);
		setLocation(100,100);
		setVisible(true);
		new PaintThread().start();
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}	
		});
	}
	private Image offScreenImage=null;
	public void update(Graphics g) {
		if(offScreenImage==null) 
		offScreenImage=this.createImage(Constant.GAME_HIGH,Constant.GAME_WIGHT);
			Graphics goff=offScreenImage.getGraphics();
			paint(goff);
			g.drawImage(offScreenImage, 0, 0, null);
	}
	class PaintThread extends Thread{
		public void run() {
			while(true) {
				repaint();
				try {
					Thread.sleep(20);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值