1主界面
2星球类
package com.softeem.solar;
import java.awt.Graphics;
import java.awt.Image;
import com.softeem.uitl.Constant;
import com.softeem.uitl.GameUtil;
import com.softeem.uitl.MyFrame;
/**
* 太阳系的窗口
* @author 魏东
*
*/
public class SolarFrame extends MyFrame {
Image bg = GameUtil.getImage("Images/1.jpg");
Star sun =new Star("Images/sun.jpg", Constant.GAME_WIDTH/2,Constant.GAME_HEIGHT/2);
Planet earth = new Planet("Images/Earth.jpg",150,100,0.02,sun);
Planet moon = new Planet("Images/moon.jpg",20,30,0.01,earth,true);
Planet mars = new Planet("Images/Mars.jpg",200,130,0.01,sun);
Planet venus = new Planet("Images/Venus.jpg",120,80,0.03,sun);
Planet uranus = new Planet("Images/Uranus.jpg",300,200,0.008,sun);
Planet mercuy = new Planet("Images/Mercury.jpg",100,60,0.04,sun);
Planet saturn = new Planet("Images/Saturn.jpg",250,150,0.009,sun);
Planet jupiter = new Planet("Images/Jupiter.jpg",350,250,0.005,sun);
public void paint(Graphics g){
g.drawImage(bg, 0, 0, null);
sun.draw(g);
earth.draw(g);
mars.draw(g);
moon.draw(g);
venus.draw(g);
uranus.draw(g);
mercuy.draw(g);
saturn.draw(g);
jupiter.draw(g);
}
public static void main(String[] args) {
new SolarFrame().launchFrame();
}
}
2星球类
package com.softeem.solar;
import java.awt.Graphics;
import java.awt.Image;
import com.softeem.uitl.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调另一个构造器
}
}
3.运行轨迹
package com.softeem.solar;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import com.softeem.uitl.GameUtil;
public class Planet extends Star{
//除了图片坐标,行星沿着太阳做椭圆运行,长轴,短轴,速度,角度
double longAxis;//椭圆的长轴
double shortAxis;//椭圆的短轴
double speed;//速度
double degree;
Star center;
boolean satellite;
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 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 draw(Graphics g){
super.draw(g);
move();
if(!satellite){
drawTrace(g);
}
}
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(String imgpath, double longAxis,
double shortAxis, double speed, Star center) {
super(GameUtil.getImage(imgpath));
this.x=center.x +longAxis;
this.y=center.y;
this.longAxis = longAxis;
this.shortAxis = shortAxis;
this.speed = speed;
this.center = center;
this.width =img.getWidth(null);
this.height=img.getHeight(null);
}
public Planet(Image img,double x,double y){
super(img,x,y);
}
public Planet(String imgpath,double x,double y){
super(imgpath,x,y);
}
}
4.窗口加载类
package com.softeem.uitl;
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.GAME_WIDTH, Constant.GAME_HEIGHT);//设置窗口大小
setLocation(100, 100);//设置窗口的位置,首坐标为100,100
setVisible(true);
new PaintThread().start();
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
private Image offScreeImage = null;
public void update(Graphics g){
if(offScreeImage==null){
offScreeImage = this.createImage(Constant.GAME_WIDTH, Constant.GAME_HEIGHT);
}
Graphics goff =offScreeImage.getGraphics();
paint(goff);
g.drawImage(offScreeImage, 0, 0, null);
}
/**
* 定义一个重画窗口的线程类,是一个内部类
* @author 魏东
*
*/
class PaintThread extends Thread{
public void run(){
while(true){
repaint();
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
5.工具类
package com.softeem.uitl;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
/**
* 游戏开发中常用的工具类
* @author 魏东
*
*/
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;
}
}
6.常量类
package com.softeem.uitl;
/**
* 存放常量的类
* @author 魏东
*
*/
public class Constant {
public static final int GAME_WIDTH = 800;
public static final int GAME_HEIGHT = 600;
}