小弟最近写了一个9大行星围绕地球的代码  但是我想在创造个卫星围绕地球转 同时地球也围着太阳转 求指导
附上代码import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;public class Test_planet extends Frame {

    public Test_planet(){
     this.setBounds(100, 100, 500, 500);
     this.setVisible(true);
     this.setTitle("9大行星");
     new Thread(new Update()).start();
    }
/**
 * @param args
 */
public static void main(String[] args) {
// TODO Auto-generated method stub
             Test_planet a=new Test_planet();
}
Image img2;
planet earth=new planet(200,200,200,100,0.05,"d:/Program Files/sun.jpg");
public void paint(Graphics g){
img2= Toolkit.getDefaultToolkit().getImage("d:/Program Files/sun.jpg");
g.drawImage(img2,100,100,null);
earth.drawplanet(g);
}
class Update implements Runnable{//线程
public void run() {
while(true){
try {
Thread.sleep(100);
repaint(); //重画
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}    
}
这是封转行星的类代码
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;public class planet {
       private int x;
       private int y;
       private int length;
       private int wide;
       private double e;
       private String imagead;
       Image image;
    public planet(int x, int y, int length, int wide, double e, String imagead) {
super();
this.x = x;
this.y = y;
this.length = length;
this.wide = wide;
this.e = e;
this.imagead = imagead;
image=Toolkit.getDefaultToolkit().getImage(imagead);
}
    double t=0.5;
public void drawplanet(Graphics g){
g.drawImage(image,x,y,null);
x=200+(int)(length*Math.cos(t));
y=200+(int)(wide*Math.sin(t));
t+=e;


}

        
}

解决方案 »

  1.   

    我的思路是算出地球的坐标,然后根据地球的坐标算出月球的坐标,下面的程序可以参考一下import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;public class TestComponent extends JComponent
    {
    public static final int WIDTH = 400;
    public static final int HEIGHT = 400;
    public static final int DELAY = 100;

    private Point sunPosition = new Point(200, 200);
    private int sunSize = 30; 
    private Color sunColor = Color.RED; private int fromSunToEarth = 100;
    private int earthSize = 20;
    private double earthRad = 0;
    private Color earthColor = Color.BLUE;
    private Point earthPosition = new Point((int) (sunPosition.x + fromSunToEarth * Math.cos(earthRad)), (int) (sunPosition.y + fromSunToEarth * Math.sin(earthRad)));

    private int fromEarthToMoon = 50;
    private int moonSize = 10;
    private double moonRad = 0;
    private Color moonColor = Color.DARK_GRAY;
    private Point moonPosition = new Point((int) (earthPosition.x + fromEarthToMoon * Math.cos(moonRad)), (int) (earthPosition.y + fromEarthToMoon * Math.sin(moonRad)));

    public TestComponent()
    {
    this.setPreferredSize(new Dimension(HEIGHT, WIDTH));
    new Thread(new TestRunnable()).start();
    }

    public void paintComponent(Graphics g)
    {
    g.setColor(sunColor);
    g.fillOval(sunPosition.x - sunSize, sunPosition.y - sunSize, sunSize, sunSize);

    g.setColor(earthColor);
    g.fillOval(earthPosition.x - earthSize, earthPosition.y - earthSize, earthSize, earthSize);

    g.setColor(moonColor);
    g.fillOval(moonPosition.x - moonSize, moonPosition.y - moonSize, moonSize, moonSize);
    }

    class TestRunnable implements Runnable
    {
    public void run()
    {
    while (true)
    {
    try
    {
    Thread.sleep(DELAY);
    }
    catch (InterruptedException e)
    {
    e.printStackTrace();
    }
    earthRad += 2 * Math.PI / 365;
    moonRad += 2 * Math.PI / 30;
    if (earthRad >= 2 * Math.PI)
    {
    earthRad -= 2 * Math.PI;
    }
    if (moonRad >= 2 * Math.PI)
    {
    moonRad -= 2 * Math.PI;
    }

    earthPosition = new Point((int) (sunPosition.x + fromSunToEarth * Math.cos(earthRad)), (int) (sunPosition.y + fromSunToEarth * Math.sin(earthRad)));
    moonPosition = new Point((int) (earthPosition.x + fromEarthToMoon * Math.cos(moonRad)), (int) (earthPosition.y + fromEarthToMoon * Math.sin(moonRad)));
    repaint();
    }
    }
    }

    public static void main(String[] args)
    {
    JFrame frame = new JFrame();
    frame.add(new TestComponent());
    frame.pack();
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    }