java实现椭圆运动(太阳系)
2017-11-25 11:34
218 查看
package com.xasmall.test; import java.awt.Frame; import java.awt.Graphics; import java.awt.Image; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; import javax.imageio.ImageIO; /* * 游戏窗口类 * @author xasmall */ public class MyGameFrame extends Frame{//AWT,swing private static final long serialVersionUID = 1L; static double centerx; static double centery; static double sunx; static double suny; static double width; static double height; static int x; static int y; Image imagesun=getimage("images/sun.png"); Image imageEarth=getimage("images/Earth.png"); Image imageMercury=getimage("images/Mercury.png"); Image imageVenus=getimage("images/Venus.png"); Image imageUranus=getimage("images/Uranus.png"); Image imageNeptune=getimage("images/Neptune.png"); Image imageJupiter=getimage("images/Jupiter.png"); Image imageSaturn=getimage("images/Saturn.png"); Image imageMars=getimage("images/Mars.png"); static Plent Earth; static Plent Mercury;//水星 static Plent Mars;//火星 static Plent Neptune;//海王星 static Plent Venus;//金星 static Plent Uranus;//天王星 static Plent Jupiter;//木星 static Plent Saturn;//土星 public MyGameFrame() { } public MyGameFrame(double width,double height,double sunx,double suny) { this(); MyGameFrame.sunx=sunx; MyGameFrame.suny=suny; MyGameFrame.centerx=sunx+35; MyGameFrame.centery=suny+35; MyGameFrame.width=width; MyGameFrame.height=height; } /* * * 加载窗口 */ public void lanuchFrame() { setSize((int)width, (int)height); setLocation(20, 20); setVisible(true); new FrameThread().start(); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } @Override public void paint(Graphics g) { g.drawImage(imagesun, (int)sunx, (int)suny, null); plentxy(Earth); g.drawImage(imageEarth, (int)Earth.x, (int)Earth.y, null); plentxy(Mercury); g.drawImage(imageMercury, (int)Mercury.x, (int)Mercury.y, null); plentxy(Venus); g.drawImage(imageVenus, (int)Venus.x, (int)Venus.y, null); plentxy(Mars); g.drawImage(imageMars, (int)Mars.x, (int)Mars.y, null); plentxy(Jupiter); g.drawImage(imageJupiter, (int)Jupiter.x, (int)Jupiter.y, null); plentxy(Saturn); g.drawImage(imageSaturn, (int)Saturn.x, (int)Saturn.y, null); plentxy(Uranus); g.drawImage(imageUranus, (int)Uranus.x, (int)Uranus.y, null); plentxy(Neptune); g.drawImage(imageNeptune, (int)Neptune.x, (int)Neptune.y, null); } public void plentxy(Plent plent) { plent.setDrgee(); plent.setX(centerx); plent.setY(centery); } class FrameThread extends Thread{ @Override public void run() { while(true) { repaint(); try { Thread.sleep(40);//25帧 } catch (InterruptedException e) { e.printStackTrace(); } } } } public Image getimage(String path) { URL url=MyGameFrame.class.getClassLoader().getResource(path); BufferedImage img=null; try { img=ImageIO.read(url); } catch (IOException e) { e.printStackTrace(); } return img; } public static void main(String[] args) { MyGameFrame mygame=new MyGameFrame(1500,1000,700,450); Earth=new Plent(10, 400, 300); Mercury=new Plent(4,150,120); Venus=new Plent(6,250,200); Mars=new Plent(12, 550, 450); Jupiter=new Plent(15,750,540); Saturn=new Plent(18,920,700); Uranus=new Plent(21,1100,820); Neptune=new Plent(24, 1300, 900); mygame.lanuchFrame(); } } class Plent{ double speed; double drgee=0; double x; double y; double width; double height; public Plent() { } public Plent(double speed,double width,double height) { this(); this.speed=speed/100; this.height=height; this.width=width; } public double setDrgee() { return this.drgee+=this.speed; } public double setX(double centerx) { return this.x=(int)(Math.sin(this.drgee)*(width/2)+centerx); } public double setY(double centery) { return this.y=(int)(Math.cos(this.drgee)*(height/2)+centery); } }
图片自取:
密码: ksmu
相关文章推荐
- java实现类的封装(物体实现椭圆运动)
- Java实现的简易太阳系模型
- Java简单的台球运动轨迹效果实现
- Jecc(java椭圆曲线加密库)学习笔记及ECDH实现
- OpenGL 实现模拟太阳系运动
- 机器人的运动范围(Java实现)
- java编程加载窗口,实现物体变速运动(台球的运动)
- Java实现机器人的运动范围
- javascript实现椭圆运动轨迹
- 椭圆曲线加密算法SM2在Android用Java调用实现
- Java实现画线、矩形、椭圆、字符串功能
- 机器人的运动范围java实现
- JAVA 多线程制作大球吃小球 一、实现球的自动生成及运动 生产消费模型
- 现代应用密码学中椭圆曲线求点集E以及点乘算法的java代码实现
- Java编程实现的模拟行星运动示例
- cocos2d-x2.2实现椭圆运动,参数为中心点坐标和长半轴、短半轴
- 【java】java实现运动的小车
- [置顶] java程序实现小球来回运动
- Java通过数学函数(抛物线、正弦曲线、椭圆等)控制游戏中物体的运动
- 在java中实现图形学中的椭圆算法