Java太阳系小游戏分析和源码详解
最近看了面向对象的一些知识,然后跟着老师的讲解做了一个太阳系各行星绕太阳转的小游戏,来练习巩固一下最近学的知识:
用到知识点:类的继承、方法的重载与重写、多态、封装等
分析:
1.需要加载图片、画图
2.建一个面板,主页面
3.行星类
效果图:
先看一下源码结构图:
现在逐步分析各个类的功能:
1)工具类-----util包中
--Constant类 封装了游戏中用到的常量
--GameUtil类 封装了游戏的图片加载功能
--MyFrame类 封装了游戏面板的构造,用于各面板的父类
------之所以这样做,目的是为了封装数据,便于程序的扩充
Constant.java
package util; public class Constant { public static final int GAME_WIDTH = 800; public static final int GAME_HEIGHT = 600; }
GameUtil.java
package util; import java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; /** * 工具类(加载图片) * @author long * */ 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; } }
MyFrame.java
package util; import javax.swing.JFrame; import javax.swing.JPanel; /** * 游戏面板的父类 * @author long * */ public class MyFrame extends JPanel{ /** * 加载Frame的方法 */ public void launchFrame(){ JFrame frame = new JFrame("MyGame"); frame.add(this); frame.setSize(Constant.GAME_WIDTH,Constant.GAME_HEIGHT); frame.setAlwaysOnTop(true); // 设置其总在最上 frame.setLocationRelativeTo(null); // 设置窗体初始位置 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); new PaintThread().start(); } /** * 定义一个重画窗口的线程类,是一个内部类 * @author dell * */ class PaintThread extends Thread { public void run(){ while(true){ repaint(); try { Thread.sleep(40); //1s = 1000ms } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { new MyFrame().launchFrame(); } }
2)主要的事件处理类---solar包中
--Planet类 行星类继承至Star类
--SolarFrame类 游戏主面板类继承至MyFrame类
--Star类 星球类,各个星球的父类
--Test类 测试类,不需要说明
Planet.java
package solar; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import util.GameUtil; /** * 行星类,继承至Star类 * @author long * */ public class Planet extends Star{ //除了图片、坐标,行星沿着椭圆运行:长轴、短轴、移动速度、旋转角度。绕着某个star运行 double longAxis; //椭圆长轴 double shortAxis; //椭圆短轴 double speed; //飞行速度 double degree; //旋转角度 Star center; //围绕行星 public void draw(Graphics g){ //g.drawImage(img, (int)x, (int)y, null); super.draw(g); drawTrace(g); move(); } public void drawTrace(Graphics g){ double traceX,traceY,traceWidth,traceHeight; traceX = (center.x+center.w/2)-longAxis; traceY = (center.y+center.h/2)-shortAxis; traceWidth = 2*longAxis; traceHeight = 2*shortAxis; Color c = g.getColor(); g.setColor(Color.blue); g.drawOval((int)traceX, (int)traceY, (int)traceWidth, (int)traceHeight); g.setColor(c); } public void move(){ //沿着椭圆轨迹飞行 x = center.x + longAxis * Math.cos(degree); y = center.y + shortAxis * Math.sin(degree); degree += speed; } 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 Planet( Star center,Image img,double longAxis, double shortAxis,double speed) { super(); this.x = (center.x+center.w/2) + longAxis; this.y = (center.y+center.h/2) + shortAxis; this.img = img; this.longAxis = longAxis; this.shortAxis = shortAxis; this.speed = speed; this.center = center; } public Planet( Star center,String imgPath,double longAxis, double shortAxis,double speed) { this(center,GameUtil.getImage(imgPath),longAxis,shortAxis,speed); } }
SolarFrame.java
package solar; import java.awt.Graphics; import java.awt.Image; import util.Constant; import util.GameUtil; import util.MyFrame; public class SolarFrame extends MyFrame{ int width = Constant.GAME_WIDTH/2; int height = Constant.GAME_HEIGHT/2; Image bg=GameUtil.getImage("images/bg.png"); Star sun = new Star("images/sun.jpg",width,height); Planet earth = new Planet(sun,"images/earth.png",100,60,0.1); Planet mars = new Planet(sun,"images/mars.png",180,100,0.15); @Override public void paint(Graphics g) { g.drawImage(bg, 0, 0, null); sun.draw(g); earth.draw(g); mars.draw(g); } public static void main(String[] args) { new SolarFrame().launchFrame(); } }
Star.java
package solar; import java.awt.Graphics; import java.awt.Image; import util.GameUtil; public class Star { public Image img; public double x,y; int w,h; public void draw(Graphics g){ g.drawImage(img, (int)x, (int)y, null); } public Star(){ } public Star(Image img){ this.img = img; this.w = img.getWidth(null); this.h = 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); } }
总结:该小游戏对代码的封装处理的比较好,便于程序的扩充,体现了面向对象的强大,不同的功能封装在不同的类与方法中,把类的公共的部分封装在父类中,提高代码的重用性。前期各个类写的过程中会有各种小问题与细节,但处理完这些后,后期想扩充行星的个数就比较简单了,new一个行星对象,然后画的面板上即可。面向对象水太深,这只是初步小涉猎,仍需继续努力专研!!!
以上就是Java太阳系小游戏分析和源码详解,希望对大家学习java语言有所帮助。
赞 (0)