很早前学视频的一个行星绕太阳转的模型,没事做着玩的不错
主窗口
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();
}
}
程序打包已上传,如下: