简易太阳系模型
此简易太阳系模型实现了日地月的二维运行轨迹,运行结果截图如下:
代码:
1.solar包
(1)星系类:
import java.awt.Graphics;
import java.awt.Image;
import util.Constant;
import util.GameUtil;
import util.MyFrame;
public class MySolarSystem extends MyFrame {
Image bg = GameUtil.getImage("images/bg.png");
Star sun = new Star("images/s.png",Constant.WIDTH/2,Constant.HEIGHT/2);
Planet earth = new Planet("images/e.png",sun,200,150,0.05);
Planet moon = new Planet("images/m.png",earth,50,40,0.15,true);
public void paint(Graphics g){
g.drawImage(bg,0,0,null);
sun.draw(g);
earth.draw(g);
moon.draw(g);
}
public static void main(String[] args){
new MySolarSystem().launchFrame();
}
}
(2)恒星类:
import java.awt.Image;
import util.GameUtil;
import java.awt.Graphics;;
public class Star{
Image img;
double x,y;
int width,height;
public void draw(Graphics g){
g.drawImage(img, (int)x, (int)y, null);
}
public Star(Image img){
this.img = img;
this.width = img.getWidth(null );
this.height = img.getHeight(null);
}
public Star(Image img, double x, double y){
this(img);
this.x = x - this.width/2;
this.y = y - this.height/2;
}
public Star(String imgpath, double x, double y){
this(GameUtil.getImage(imgpath), x, y);
}
}
(3)行星类:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import util.GameUtil;
public class Planet extends Star {
Star center;
double longAxis;
double shortAxis;
double speed;
double degree;
boolean stal;
public void draw(Graphics g) {
super.draw(g);
if(!stal){
drawTrace(g);
}
move();
}
public void drawTrace(Graphics g){
double ovalX,ovalY,ovalWidth,ovalHeight;
ovalX = center.x + center.width /2 - longAxis;
ovalY = center.y + center.height /2 - shortAxis;
ovalWidth = longAxis*2;
ovalHeight = shortAxis*2;
Color c= g.getColor();
g.setColor(Color.yellow);
g.drawOval((int)ovalX, (int)ovalY, (int)ovalWidth, (int)ovalHeight);
g.setColor(c);
}
public void move() {
x = center.x + center.width/2 + longAxis*Math.cos(degree)- this.width/2;
y = center.y + center.width/2 + shortAxis*Math.sin(degree) - this.height/2;
degree += speed;
}
public Planet(String imgpath, Star center, double longAxis,
double shortAxis, double speed) {
super(GameUtil.getImage(imgpath));
this.center = center;
this.longAxis = longAxis;
this.shortAxis = shortAxis;
this.x = center.x + center.width/2 + longAxis - this.width/2;
this.y = center.y + center.width/2 - this.height/2;
this.speed = speed;
}
public Planet(String imgpath, Star center, double longAxis,
double shortAxis, double speed, boolean stal){
this(imgpath, center, longAxis, shortAxis, speed);
this.stal = stal;
}
public Planet(Image img, double x, double y) {
super(img, x, y);
}
public Planet(String imgpath, double x, double y) {
super(imgpath, x, y);
}
}
2.工具包
(1)工具类:
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class GameUtil {
private GameUtil(){}
public static Image getImage(String path){
URL u = GameUtil.class.getClassLoader().getResource(path);
BufferedImage img = null;
try {
img = ImageIO.read(u);
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
}
(2)框架类:
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyFrame extends Frame {
public void launchFrame(){
setSize(Constant.WIDTH, Constant.HEIGHT);
setLocation(100, 100);
setVisible(true);
new PaintThread().start();
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
class PaintThread extends Thread{
public void run(){
while(true){
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private Image offScreenImage = null;
public void update(Graphics g){
if(offScreenImage == null){
offScreenImage = this.createImage(Constant.WIDTH,Constant.HEIGHT);
}
Graphics gOff = offScreenImage.getGraphics();
paint(gOff);
g.drawImage(offScreenImage, 0, 0, null);
}
}
(3)常量类:
public class Constant {
public static final int WIDTH = 800;
public static final int HEIGHT = 800;
}
3.素材包
(1)背景:
(2)太阳:
(3)地球:
(4)月球:
此程序在高淇老师教学视频下完成