首先定義一個窗口類
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();
}
}