Java模拟八大行星运动效果

  1 import java.awt.*;
  2 import java.awt.event.WindowAdapter;
  3 import java.awt.event.WindowEvent;
  4 
  5 public class SolorSystem extends Frame{
  6     
  7     double x , x1 = 200,x2 = 200,x3 = 200,x4 = 200,x5 = 200,x6 = 200,x7 = 200,x8 = 200;
  8     double y , y1 = 200,y2 = 200,y3 = 200,y4 = 200,y5 = 200,y6 = 200,y7 = 200,y8 = 200;
  9     double t = 0,t1 = 0,t2 = 0,t3 = 0,t4 = 0,t5 = 0,t6 = 0,t7 = 0,t8 = 0;
 10     private Image offScreenImage = null;
 11     void loadFrame(){
 12         this.setSize(900,900);
 13         this.setLocation(50,50);
 14         this.setTitle("火星十一郎");
 15         myEvent();
 16         this.setVisible(true);
 17 //        this.setBackground(Color.black);
 18         new PaintThread().start();
 19     }
 20     public void update(Graphics g) {
 21         if(offScreenImage == null)
 22             offScreenImage = this.createImage(900, 900);
 23         
 24         Graphics gOff = offScreenImage.getGraphics();
 25         Color oldColor = gOff.getColor();
 26         gOff.setColor(Color.GREEN);
 27         gOff.fillRect(0, 0, 900, 900);
 28         gOff.setColor(oldColor);
 29         paint(gOff);
 30         g.drawImage(offScreenImage, 0, 0, null);
 31     }
 32     
 33     Image sun = Toolkit.getDefaultToolkit().getImage("images/sun.jpg");
 34     Image bg = Toolkit.getDefaultToolkit().getImage("images/bg.jpg");
 35     Image mercury = Toolkit.getDefaultToolkit().getImage("images/Mercury.jpg");
 36     Image venus = Toolkit.getDefaultToolkit().getImage("images/Venus.jpg");
 37     Image earth = Toolkit.getDefaultToolkit().getImage("images/Earth.jpg");
 38     Image mars = Toolkit.getDefaultToolkit().getImage("images/Mars.jpg");
 39     Image jupiter = Toolkit.getDefaultToolkit().getImage("images/Jupiter.jpg");
 40     Image saturn = Toolkit.getDefaultToolkit().getImage("images/Saturn.jpg");
 41     Image uranus = Toolkit.getDefaultToolkit().getImage("images/Uranus.jpg");
 42     Image neptune = Toolkit.getDefaultToolkit().getImage("images/Neptune.jpg");
 43     Image moon = Toolkit.getDefaultToolkit().getImage("images/moon.jpg");
 44     
 45     public void paint(Graphics g){
 46         g.setColor(Color.blue);
 47         g.drawImage(bg , 0 , 0 , null);
 48         g.drawImage(sun , 435 , 435 , null);
 49         g.drawOval(401, 415, 88, 60);
 50         g.drawOval(375, 395, 140, 100);
 51         g.drawOval(335, 365, 220, 160);
 52         g.drawOval(285, 335, 320, 220);
 53         g.drawOval(155, 275, 580, 340);
 54         g.drawOval(120, 245, 650, 400);
 55         g.drawOval(80, 210, 730, 470);
 56         g.drawOval(10, 165, 870, 560);
 57         
 58         t1 = t1 + Math.PI / 8.8;         //公转周期为88天
 59         t2 = t2 + Math.PI / 22.5;
 60         t3 = t3 + Math.PI / 36.5;
 61         t = t + Math.PI / 0.1;
 62         t4 = t4 + Math.PI / 68.7;
 63         t5 = t5 + Math.PI / 432.9;
 64         t6 = t6 + Math.PI / 1076.8;
 65         t7 = t7 + Math.PI / 3066.0;
 66         t8 = t8 + Math.PI / 6015.2;
 67         
 68         g.drawImage(mercury , (int)x1 ,(int)y1, null);
 69         x1 = 440 + 44 * Math.cos(t1);
 70         y1 = 440 + 30 * Math.sin(t1);
 71         g.drawImage(venus , (int)x2 ,(int)y2, null);
 72         x2 = 440 + 70 * Math.cos(t2);
 73         y2 = 440 + 50 * Math.sin(t2);
 74         g.drawImage(earth , (int)x3 ,(int)y3, null);
 75         x3 = 440 + 110 * Math.cos(t3);
 76         y3 = 440 + 80 * Math.sin(t3);
 77         g.drawImage(moon , (int)x ,(int)y, null);
 78         x = x3 + 15 * Math.cos(t1);
 79         y = y3 + 15 * Math.sin(t1);
 80         g.drawImage(mars , (int)x4 ,(int)y4, null);
 81         x4 = 440 + 160 * Math.cos(t4);
 82         y4 = 440 + 110 * Math.sin(t4);
 83         g.drawImage(jupiter , (int)x5 ,(int)y5, null);
 84         x5 = 435 + 290 * Math.cos(t5);
 85         y5 = 435 + 170 * Math.sin(t5);
 86         g.drawImage(saturn , (int)x6 ,(int)y6, null);
 87         x6 = 428 + 325 * Math.cos(t6);
 88         y6 = 428 + 200 * Math.sin(t6);
 89         g.drawImage(uranus , (int)x7 ,(int)y7, null);
 90         x7 = 435 + 365 * Math.cos(t7);
 91         y7 = 435 + 235 * Math.sin(t7);
 92         g.drawImage(neptune , (int)x8 ,(int)y8, null);
 93         x8 = 435 + 435 * Math.cos(t8);
 94         y8 = 435 + 280 * Math.sin(t8);
 95     }
 96 
 97     public static void main(String[] args){
 98         SolorSystem ball = new SolorSystem();
 99         ball.loadFrame();
100     }
101     
102     private class PaintThread extends Thread{
103         public void run(){
104             while(true){
105                 repaint();
106                 try {
107                     Thread.sleep(40);
108                 } catch (InterruptedException e) {
109                     e.printStackTrace();
110                 }
111             }
112         }
113     }
114     
115     private void myEvent()
116     {
117         this.addWindowListener(new WindowAdapter()//窗口监听
118         {
119             public void windowClosing(WindowEvent e)
120             {
121                 //System.out.println("窗体执行关闭!");
122                 System.exit(0);
123             }
124         });
125     }
126 
127 }

 

posted @ 2013-03-29 20:23  加拿大小哥哥  阅读(811)  评论(1编辑  收藏  举报