Java行星移动的小程序

太阳系行星的运动

在这里插入图片描述
**package com.wang.util;

/**
 * 游戏项目中用到的常量
 * @author dell
 */
public class Constant {
	public static final int GAME_WIDTH = 800;
	public static final int GAME_HEIGHT = 800;	
}**

package com.wang.util;
/**

  • 游戏开发中常用的工具类(比如:加载图片等方法)
  • */
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) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return img;
}
}

package com.wang.util;

import javax.swing.JFrame;

public class MyFrame extends JFrame{
	/**
	 * 加载窗口
	 * */
		public void init() {
			this.setSize(500,500);
			this.setLocation(100,100);
			this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
			this.setTitle("游戏");
			this.setVisible(true);
		    new PaintThread().start();//启动重画线程
		}
		/**
		 * 定义一个重画窗口的线程类,是一个内部类;
		 * */
		class PaintThread extends Thread{
			public void run() {
				while(true) {
					repaint();
					try {
						Thread.sleep(40);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
}

以上三个类在一个包中;

package com.wang.solar;

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

import com.wang.util.GameUtil;

public class Planet extends Star {
	//除了图片,坐标。 行星沿着某个椭圆运行:长轴、短轴、速度、角度。 绕着某个Star飞。
	double longAxis;  //椭圆的长轴
	double shortAxis;  //椭圆的短轴
	double speed;     //飞行的速度
	double degree;    
	Star center;
	
boolean satellite;
	public void draw(Graphics g){
		super.draw(g);
		move();
		if(!satellite){
			drawTrace(g);
		}
	}
	public void drawTrace(Graphics g){
		double ovalX,ovalY,ovalWidth,ovalHeight;
		ovalWidth = longAxis*2;
		ovalHeight = shortAxis*2;
		ovalX = (center.x+center.width/2)-longAxis;
		ovalY = (center.y+center.height/2)-shortAxis;
		
Color c =g.getColor();
		g.setColor(Color.blue);
		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);
		y = (center.y+center.height/2)+ shortAxis*Math.sin(degree);
			degree += speed;
	}
		public Planet(Star center,String imgpath, double longAxis,
			double shortAxis, double speed) {
		super(GameUtil.getImage(imgpath));
		
this.center = center;
		this.x = center.x + longAxis;
		this.y = center.y;
			this.longAxis = longAxis;
this.shortAxis = shortAxis;
		this.speed = speed;
		
this.width = img.getWidth(null);
		this.height = img.getHeight(null);
	}
public Planet(Star center,String imgpath, double longAxis,
			double shortAxis, double speed,boolean satellite) {
		this(center, imgpath, longAxis, shortAxis, speed);
		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);
	}
}

package com.wang.solar;

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

import com.wang.util.Constant;
import com.wang.util.GameUtil;
import com.wang.util.MyFrame;

/**
 * 
 * 太阳系的主窗口
 * 
 * */
public class SolarFrame extends MyFrame{
   Image bg=GameUtil.getImage("images/bg.jpg");
                          /*  Star sun=new Star("images/sun.jpg",Constant.GAME_WIDTH/2,Constant.GAME_HEIGHT/2);*/
   Star sun=new Star("images/sun.jpg",Constant.GAME_WIDTH/2,Constant.GAME_HEIGHT/2);
   Planet earth =new Planet(sun,"images/earth.jpg",80,50,0.06);
   Planet moon = new Planet(earth, "images/moon.jpg",20, 15, 0.08,true);
	Planet mars = new Planet(sun, "images/Mars.jpg", 120, 90, 0.02);
	Planet Jupiter = new Planet(sun, "images/Jupiter.jpg",200, 150, 0.04);
	Planet Mercury = new Planet(sun, "images/Mercury.jpg",  160, 120, 0.05);
	Planet Neptune = new Planet(sun, "images/Neptune.jpg",  240, 180, 0.05);
	public void paint(Graphics g){
		g.drawImage(bg, 0, 0, null);
		sun.draw(g);
		earth.draw(g);
		mars.draw(g);
		
moon.draw(g);
		Jupiter.draw(g);
		Mercury.draw(g);
		Neptune.draw(g);
	}
	
public static void main(String[] args) {
		new SolarFrame().init();
	}
}

package com.wang.solar;

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

import com.wang.util.GameUtil;

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);//通过this调用另一个构造方法
}
}

这三个在一个包中

猜你喜欢

转载自blog.csdn.net/qq_43615815/article/details/83929137
0条评论
添加一条新回复