第一个JAVA小项目(太阳系行星运动模型)


1. 窗体的构建

对于游戏来说,需要构建一个桌面窗体,建立MyFrame类,继承Frame类,专用用于游戏窗体的构建。在MyFrame类中建立launchFrame()方法,用于实现窗口的生成。

这三行代码分别用于设置窗体大小,位置和可见的,这三个方法继承自Frame类。

            setSize(Constant.GAME_HEIGHT,Constant.GAME_WIDTH);
            setLocation(100, 100);
            setVisible(true);

定义内部类PaintThread继承Thread,重写Runnable接口方法run(),一直执行 repaint()函数,Thread.sleep(40)是让线程停止40ms。PaintThread().start()让线程的run()方法执行,可以实现窗口的动态更新。

         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();
                     }
                 }
             }
         }

在addWindowListener方法中定义内部类WindowAdapter,内部内重写了windowClosing()方法,用于窗体的退出(如果没有这个方法,窗体将无法退出)

addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
         }

窗体构建类MyFrame 的完整代码如下:


package com.zhong.util;

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



public class MyFrame extends Frame {

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

            new PaintThread().start();
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
         }

         class PaintThread extends Thread{
             public void run(){
                 while(true){
                     repaint();
                     try {
                        Thread.sleep(40);
                     } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                     }
                 }
             }
         }


}

2. 工具类

定义常量类Constant,用于保存项目中的所有常量。代码如下:

package com.zhong.util;
/**
 * 常量
 * @author lenovo
 *
 */
public class Constant {
     public static final int GAME_WIDTH=800;
     public static final int GAME_HEIGHT=800;
}

定义 GameUtil类,用于一般工具的实现。里面的 getImage(String path)方法,用于从指定地址读取图片。完整代码如下:

package com.zhong.util;

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

import javax.imageio.ImageIO;

/**
 * 
 * 常用工具类
 * @author Kobe Zhong
 *
 */
public class GameUtil {
     private GameUtil(){

     }
     public static Image getImage(String path){
         URL  url=GameUtil.class.getClassLoader().getResource(path);
         BufferedImage img=null;
         try {
            img=ImageIO.read(url);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return img;
     }
}

3. 星球类

星球类Star是所有的星球所共有的属性。

定义星球的属性有图片(代表每种星球),坐标,宽度和长度(图片的)。代码如下:

    Image img;
    double x,y;
    int width,height;

定义星球类的重载构造函数,代码如下:

    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);
    }

定义星球类的draw()方法,可以在窗体上画出星球的图片。代码如下:

    public void draw(Graphics g){
        g.drawImage(img,(int)x,(int)y,null);
    }

星球类Star的完整代码如下:

package com.zhong.solar;

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

import com.zhong.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);
    }
}

4. 行星类

行星类Planet继承星球类Star,还拥有自己所特有的属性。

行星类包含长轴、短轴、运动速度、角度、中心恒星以及是否为卫星。属性定义代码如下:

    double longAxis;//长轴
    double shortAxis;//短轴
    double speed;
    double degree;//角度
    Star center;

    boolean isSattilite;

行星Planet的重载构造函数的代码如下:

    public Planet(Star center,String imgpath, double longAxis,double shortAxis, double speed) {

        super(GameUtil.getImage(imgpath));
        this.x=center.x+longAxis;
        this.y=center.y;
//      this.img=GameUtil.getImage(imgpath);
        this.shortAxis = shortAxis;
        this.longAxis = longAxis;
        this.speed = speed;
        this.center = center;
    }

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

    }
    public Planet(Image img, double x, double y) {
        super(img, x, y);
        // TODO Auto-generated constructor stub
    }
    public Planet(String imgpath, double x, double y) {
        super(imgpath, x, y);
        // TODO Auto-generated constructor stub
    }

行星的运动是绕着恒星中心,做椭圆运动,其坐标变化的代码如下:

    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);
    }

行星在窗体上的绘制重写父类Star的draw()方法,并调用move()坐标变化方法,还有是否为卫星的判断,不是卫星就画运动轨迹。具体代码如下:

    public void draw(Graphics g){
        super.draw(g);
        move();
        if(!isSattilite){
             drawTrace(g);
        }
    }

行星类Planet的完整代码如下所示:

package com.zhong.solar;

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

import com.zhong.util.GameUtil;

public class Planet extends Star{
    double longAxis;//长轴
    double shortAxis;//短轴
    double speed;
    double degree;//角度
    Star center;

    boolean isSattilite;
    public void draw(Graphics g){
        super.draw(g);
        move();
        if(!isSattilite){
             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);
    }

    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.x=center.x+longAxis;
        this.y=center.y;
//      this.img=GameUtil.getImage(imgpath);
        this.shortAxis = shortAxis;
        this.longAxis = longAxis;
        this.speed = speed;
        this.center = center;
    }

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

    }
    public Planet(Image img, double x, double y) {
        super(img, x, y);
        // TODO Auto-generated constructor stub
    }
    public Planet(String imgpath, double x, double y) {
        super(imgpath, x, y);
        // TODO Auto-generated constructor stub
    }


}

5. 太阳系类(主方法)

定义SolarFrame为主类,继承MyFrame。

加载背景图片,并定义太阳,地球,火星,月球的对象,并初始化。代码如下:

         Image bg=GameUtil.getImage("images/bg.jpg");
         Star sun=new Star("images/sun.jpg", Constant.GAME_HEIGHT/2, Constant.GAME_WIDTH/2);
         Planet earth =new Planet(sun, "images/earth.jpg",100,50,0.1 ) ;
         Planet mars =new Planet(sun, "images/Mars.jpg",200,130,0.2 ) ;
         Planet moon=new Planet(earth,    "images/moon.jpg",20,13,0.01,true);

重写paint()方法,将背景图片,太阳,地球,火星和月球的图片绘制。(注意:paint()方法为回调方法,不需要在主函数调用它,在其父类的构造方法中已经调用,根据其所在对象重写的功能具体实现)

         public void paint(Graphics g){
             g.drawImage(bg,0,0,null);
             sun.draw(g);
             earth.draw(g);
             mars.draw(g);
             moon.draw(g);
         }

主方法中,定义SolarFrame对象,并调用它的launchFrame()方法(实现窗体的更新)。代码如下:

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

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

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

public class SolarFrame extends MyFrame{
         Image bg=GameUtil.getImage("images/bg.jpg");
         Star sun=new Star("images/sun.jpg", Constant.GAME_HEIGHT/2, Constant.GAME_WIDTH/2);
         Planet earth =new Planet(sun, "images/earth.jpg",100,50,0.1 ) ;
         Planet mars =new Planet(sun, "images/Mars.jpg",200,130,0.2 ) ;
         Planet moon=new Planet(earth,  "images/moon.jpg",20,13,0.01,true);
         public void paint(Graphics g){
             g.drawImage(bg,0,0,null);
             sun.draw(g);
             earth.draw(g);
             mars.draw(g);
             moon.draw(g);
         }

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

第一个JAVA小项目(太阳系行星运动模型)_第1张图片

你可能感兴趣的:(Java学习)