首页 > 代码库 > Java小案例(行星移动)
Java小案例(行星移动)
Java小案例
行星移动:参考:三百集
使用软件:idea2017,java
1,图片集:这里 (idea图片源放在target目录下,才能访问到),建议从小往上看。。。
2,定义MyFrame
package my.university; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class MyFrame extends Frame { public void launchFram(){ setSize(Content.Game_With,Content.Game_High); setLocation(100,100); setVisible(true); new PaintThread().start(); addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } private class PaintThread extends Thread{ public void run(){ while(true){ repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
2,定义ImageUtil类:
package my.university; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; public class ImageUtil { private ImageUtil(){}//工具类构成私有的,? public static Image getImage(String path){ URL url=ImageUtil.class.getResource(path); /*System.out.println(ImageUtil.class.getClassLoader().getResource("")); result is file:/d:/xxxxx/sparkDemo/sparksql/target/classes/*/ BufferedImage image =null; try { image=ImageIO.read(url); } catch (IOException e) { e.printStackTrace(); } return image; } }
3,定义常量类,相当于配置文件
package my.university; public class Content { public static final int Game_With=500; public static final int Game_High=500; }
4,定义Start类
package my.university; import java.awt.*; public class Start { public double x; public double y; public double with; public double height; Image image; public Start(){}//默认构造起,用于子类继承 public Start(Image image){ this.image=image; this.with=image.getWidth(null); this.height=image.getHeight(null); } public Start(Image image,double x,double y){ this(image); this.x=x; this.y=y; } public Start(String path,double x,double y){ this(ImageUtil.getImage(path),x,y); } public void draw(Graphics g){ g.drawImage(image,(int)x,(int)y,null); } }
5,定义Plant类
package my.university; import java.awt.*; /** * Created by Administrator on 2017/8/12. */ public class Plant extends Start { double longAxis; //椭圆的长轴 double shortAxis; //椭圆的短轴 double speed; //飞行的速度 double degree; Start center; boolean statellite; public Plant(){} public Plant(Start center,String path,double longAxis,double shortAxis,double speed){ super(ImageUtil.getImage(path)); this.center=center; this.x=center.x+longAxis; this.y=center.y; this.longAxis=longAxis; this.shortAxis=shortAxis; this.speed=speed;; } public Plant(Start center,String path,double longAxis,double shortAxis,double speed,Boolean statellite){ this(center, path, longAxis, shortAxis, speed); this.statellite=statellite; } public Plant(Image image){ super(image); } public Plant(Image image,double x,double y){ super(image, x, y); } //画轨迹 public void drawTrace(Graphics g){ double ovalX,ovalY,ovalWidth,ovalHeight; ovalWidth = longAxis*2; ovalHeight = shortAxis*2; ovalX = (center.x+center.with/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);//定义矩形的左上角坐标及宽和高,在矩形中画椭圆 g.setColor(c); } //移动 public void move(){ x=center.x+center.with/2+longAxis*Math.cos(degree); y=center.y+center.height/2+shortAxis*Math.sin(degree); degree+=speed; } public void draw(Graphics g){ super.draw(g); move(); if(!statellite){ drawTrace(g); } } }
6,定以调用类
package my.university; import java.awt.*; public class SolarFrame extends MyFrame { Image bg=ImageUtil.getImage("images/bg.jpg"); Start sun=new Start("images/sun.jpg",Content.Game_With/2,Content.Game_High/2); Plant mercurys = new Plant(sun, "images/Mercury.jpg", 50, 30, 0.2); Plant venus = new Plant(sun, "images/Venus.jpg", 60, 40, 0.3); Plant earth=new Plant(sun,"images/Earth.jpg",100,50,0.4); Plant mars = new Plant(sun, "images/Mars.jpg", 120, 60, 0.5); Plant jupiter = new Plant(sun, "images/jupiter.jpg", 140, 70, 0.6); Plant saturn =new Plant(earth,"images/Saturn.jpg",160,80,0.7,true); Plant uranus = new Plant(sun, "images/Uranus.jpg", 180, 90, 0.8); Plant neptune = new Plant(sun, "images/Neptune.jpg", 200, 100, 0.9); Plant moon = new Plant(earth, "images/moon.jpg", 30, 20, 0.3,true); public void paint(Graphics g){ g.drawImage(bg,0,0,null); sun.draw(g); mercurys.draw(g); venus.draw(g); earth.draw(g); mars.draw(g); jupiter.draw(g); saturn.draw(g); uranus.draw(g); neptune.draw(g); moon.draw(g); } public static void main(String[] args){ new SolarFrame().launchFram(); } }
最后附上代码
Java小案例(行星移动)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。