第一个JAVA小项目(太阳系行星运动模型)
来源:互联网 发布:mac删除bootcamp文件 编辑:程序博客网 时间:2024/04/29 08:55
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小项目(太阳系行星运动模型)
- 太阳系模型小项目(JAVA)
- Java上手小项目(一)-太阳系模型笔记
- PV3D的小练习~太阳系八大行星
- java实现椭圆运动(太阳系)
- java太阳系模型
- 第一个小项目
- 第一个小项目
- 第一个小项目
- 第一个小项目
- Unity 小项目:模拟太阳系
- Java程序设计——模拟行星运动
- 太阳系、行星中文普及读物
- 太阳及太阳系行星数据资料
- C++ 实现太阳系行星系统
- 消息 | 谷歌AI帮助NASA发现第二个"太阳系":也有八颗行星
- tp5第一个小项目
- 第一个ssm小项目
- 股市回暖 手机管家:荐股诈骗短信猛增
- 2通过nvm安装部署node模块包管理工具npm
- 互联网高可用架构技术实践
- iOS开发畅销书作者Erica Sadun的书籍作品
- 获取access_token中参数问题
- 第一个JAVA小项目(太阳系行星运动模型)
- 画子图(add_subplot & subplot)
- 使用Struts,spring和HibernateXML方式进行整合
- 论钓鱼和互动营销的相似之处
- 揭秘C可变参数原理
- GitForWindows的基本安装和配置
- 原生Js实现图片轮播
- Fiori 代码规范0---导读
- 数据结构学习记录-链式队列