Java_太阳系_行星模型_小游戏练习_详细注释
2016-12-08 22:21
423 查看
//实现MyFrame--实现绘制窗口,和实现重写 重画窗口线程类 package cn.xiaocangtian.Test; 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_WIDTH, Constant.GAME_HEIGHT); //设置窗口大小 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); //1s = 1000ms } catch (InterruptedException e) { e.printStackTrace(); } } } } }
package cn.xiaocangtian.Util; import java.awt.Image; import java.awt.Toolkit; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; import javax.swing.ImageIcon; /** * 游戏开发中常用的工具类(比如:加载图片等方法) * @author admin * */ 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子类,也算正确返回 return Toolkit.getDefaultToolkit().getImage(GameUtil.class.getClassLoader().getResource(path)); } }
package cn.xiaocangtian.Solar; import java.awt.Graphics; import java.awt.Image; import cn.xiaocangtian.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); } }
package cn.xiaocangtian.Util; /** * 游戏项目中用到的常量 * 单独负责常量 * @author admin */ public class Constant { public static final int GAME_WIDTH = 750; public static final int GAME_HEIGHT = 600; }
package cn.xiaocangtian.Solar; import java.awt.Color; import java.awt.Graphics; import java.awt.Image; import cn.xiaocangtian.Util.GameUtil; public class Planet extends Star { //除了图片,坐标,行星沿着某个椭圆运行:长轴,短轴,速度, 角度,绕着某个Star飞 double longAxis; //椭圆的长轴 double shortAxis; //椭圆的短轴 double speed; //飞行的速度 double degree; //角度 Star center; //中心 boolean satillite; //标志是否是卫星 public void draw(Graphics g) { super.draw(g); move(); if (!satillite) { //不是卫星再画出轨迹 drawTrace(g); } } 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; ovalHeight = longAxis * 2; //长度即为长轴*2 ovalWidth = shortAxis * 2; ovalX = (center.x + center.width/2) - longAxis; //左上顶点为(中心.x + x.width/2) - 长轴 ovalY = (center.y + center.height/2) - shortAxis; Color oldColor = g.getColor(); g.setColor(Color.blue); //设置轨迹颜色 g.drawOval((int)ovalX, (int)ovalY, (int)ovalHeight, (int)ovalWidth); g.setColor(oldColor); } //需要调用父类的空构造器 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.satillite = 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 cn.xiaocangtian.Solar; import java.awt.Graphics; import java.awt.Image; import cn.xiaocangtian.Util.Constant; import cn.xiaocangtian.Util.GameUtil; import cn.xiaocangtian.Util.MyFrame; /** * 太阳系主窗口 * @author admin * */ public class SolarFrame extends MyFrame { //导入背景 Image bg = GameUtil.getImage("images/yuzhou.png"); //这里是利用封装的类,导入图片 Star sun = new Star("images/sun.png", Constant.GAME_WIDTH / 2, Constant.GAME_HEIGHT / 2); Planet earth = new Planet(sun, "images/polar.png", 150, 100, 0.1); Planet moon = new Planet(earth, "images/moon.png", 30, 20, 0.3, true); Planet Mars = new Planet(sun, "images/Mars.png", 200, 130, 0.2); /** * 可以继续添加 其他 行星,只需一行代码(已经封装好) * ...... * ...... * ...... */ /** * 重写重绘函数,此为回调函数,只需要实现,然后由系统自动调用 */ public void paint(Graphics g) { g.drawImage(bg, 0, 0, null); sun.draw(g); //这里使用的是封装的方法 earth.draw(g); moon.draw(g); Mars.draw(g); /*** * 还可以继续添加其他行星并绘制 * .......... * .......... * .......... */ } public static void main(String[] args) { new SolarFrame().launchFrame(); } }
//本程序只添加了太阳,地球,月球,火星,其余可以自行添加,使用封装好的方法,只用十分简洁的代码即可
相关文章推荐
- java连接池一例(注释非常详细到位)
- 发布一个Java写的俄罗斯方块源码 算法简单(300行) 注释详细
- 可视化日历【Java,有详细的注释】
- 太阳系模型_飞机游戏JAVA084-097
- 十一用java写的俄罗斯方块,并有详细的注释.
- javadoc-Java注释详细介绍
- 大数相乘问题--算法思想及Java实现解析(附详细注释)
- Java系列(七)__模型案例练习
- java文件下载(有非常详细的注释).
- socket select 模型(详细注释版本)
- (转)发布一个Java写的俄罗斯方块源码 算法简单(300行) 注释详细
- 今天看到了一个用java实现复制,剪切,删除文件或文件夹的示例,感觉不错,欢迎学习(带详细注释)
- java小练习(已知学生有学号,姓名,性别,年龄,身高,体重六项基本信息,依次从键盘录入该学生信息,完成后输出该生详细信息。)
- 用java写的俄罗斯方块,并有详细的注释.
- 从无到有的Java小游戏开发练习(一)---推箱子
- 【技术收藏】enpaodelvzi编写:一个Java写的俄罗斯方块源码 算法简单(300行) 注释详细
- JAVA连接Oracle数据库实代码+详细注释
- JAVA连接Oracle数据库实代码+详细注释
- java中JVM虚拟机内存模型详细说明
- EventBus源码解读详细注释(2)MainThread线程模型分析