Java太阳系模型小项目

首先定义一个窗口类

package cn.hxd.util;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
* 游戏项目中用到的常用方法
* @author HXD
*
*/

public class MyFrame extends Frame{

/**
* 加载窗口
*/
public void launchFrame(){
setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);
setLocation(200,200);
setVisible(true);

new PaintThread().start();//启动重画线程

addWindowListener(new WindowAdapter() {

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

/**
* 定义一个重画窗口的线程类,是一个内部类
* @author HXD
*
*/
class PaintThread extends Thread{
public void run(){
while(true){
repaint();
try {
Thread.sleep(80);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

}

加载图片的工具类

package cn.hxd.util;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;
/**
* 游戏开发中常用的工具类
* @author HXD
*
*/

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;//BufferedImage是Image子类,也算正确返回
}

}

将项目中要用到的常量放在一个常量类中

package cn.hxd.util;
/**
* 游戏项目中用到的常量
* @author HXD
*
*/
public class Constant {
public static final int GAME_WIDTH=1360;
public static final int GAME_HEIGHT=760;
}

星球类

package cn.hxd.SolarSystem;

import java.awt.Graphics;
import java.awt.Image;
import cn.hxd.util.GameUtil;

/**
* 星球类
* @author HXD
*
*/
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(){ //子类要调用父类的默认构造函数
}
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.y=y;

}
//导入
public Star(String imgpath,double x,double y){
this(GameUtil.GetImage(imgpath),x,y);
}
}

行星类

package cn.hxd.SolarSystem;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import cn.hxd.util.GameUtil;

/**
* 行星类,继承星球类
* @author HXD
*
*/
public class Planet extends Star {
//除了图片,坐标,行星沿着某个椭圆运行:长轴,短轴,速度, 角度,绕着某个Star飞
double longAxis;
double shortAxis;
double speed;
double degree;
Star center;
boolean satellite; //看是否是卫星


//需要调用父类的空构造器
public Planet(String imgpath, double longAxis, double shortAxis, double speed, Star center) {
super(GameUtil.GetImage(imgpath));

this.longAxis = longAxis; //当前行星的长轴
this.shortAxis = shortAxis;

this.speed = speed;
this.center=center;
this.x=center.x+longAxis; //行星的初始位置
this.y=center.y;

this.width=img.getWidth(null);
this.height=img.getHeight(null);
}

public Planet(String imgpath, double longAxis, double shortAxis, double speed,Star center, boolean satellite) {
this(imgpath ,longAxis,shortAxis,speed,center);
this.satellite=satellite;

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

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

public void draw(Graphics g){
super.draw(g);
if(!satellite){ //不是卫星才画出轨迹
drawTrance(g);
}
move();
}
//画出行星的轨迹
public void drawTrance(Graphics g){
double oval_x, oval_y, oval_width, oval_height;
oval_width=longAxis*2;
oval_height=shortAxis*2;
oval_x=(center.x+center.width/2)-longAxis;//左上顶点为(中心.x + x.width/2) - 长轴
oval_y=(center.y+center.height/2)-shortAxis;

Color c=g.getColor();
g.setColor(Color.blue); //设置轨迹颜色
g.drawOval((int)oval_x, (int)oval_y, (int)oval_width, (int)oval_height);
g.setColor(c);
}

public void move(){
//沿着椭圆轨迹飞行
x=(center.x+center.width/2)+longAxis*Math.cos(degree);
y=(center.y+center.height/2)+shortAxis*Math.sin(degree);
degree+=speed;
}
}

太阳系主窗口

package cn.hxd.SolarSystem;

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

import cn.hxd.util.Constant;
import cn.hxd.util.GameUtil;
import cn.hxd.util.MyFrame;

/**
* 太阳系的主窗口
* @author HXD
*
*/
public class SolarFrame extends MyFrame {

public SolarFrame(){}

private Image iBuffer;
private Graphics gBuffer;
// SolarFrame(String s) {
// super(s);
// }
//导入背景
Image bg = GameUtil.GetImage("images/bg.jpg");
//这里是利用封装的类,导入图片
Star sun = new Star("images/ty.png",Constant.GAME_WIDTH/2,Constant.GAME_HEIGHT/2);
Planet earth = new Planet("images/dq.png", 200, 150, 0.1, sun);
Planet moon= new Planet("images/moon.jpg", 70, 50, 0.2, earth,true);
Planet mars = new Planet("images/mars.jpg", 300, 230,0.07, sun);
Planet mx = new Planet("images/mx.jpg", 400, 300, 0.05, sun);
/**
25 * 可以继续添加 其他 行星,只需一行代码(已经封装好)
26 * ......
27 * ......
28 * ......
29 */



/**
* 重写重绘函数,此为回调函数,只需要实现,然后由系统自动调用
*/
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
sun.draw(g); //这里使用的是封装的方法
earth.draw(g);
mars.draw(g);
mx.draw(g);
moon.draw(g);
}
//JAVA双缓冲 ,防止屏幕闪烁
public void update(Graphics scr)
{
if(iBuffer==null)
{
iBuffer=createImage(this.getSize().width,this.getSize().height);
gBuffer=iBuffer.getGraphics();
}
gBuffer.setColor(getBackground());
gBuffer.fillRect(0,0,this.getSize().width,this.getSize().height);
paint(gBuffer);
scr.drawImage(iBuffer,0,0,this);
}


public static void main(String[] args){
new SolarFrame().launchFrame();
}
}

原文地址:https://www.cnblogs.com/houxudong/p/6946681.html