Java编写天体运动模型

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() {@Overridepublic 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();}}}}
}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部