Orbiting Planets and Moons
I'm trying to make a code that has several orbiting images, a moon orbiting the earth, and then a smaller moon orbiting the moon. Getting the main moon orbiting the earth wasn't too difficult, it just requires rotating the moon image around the center of the earth. However, I can't figure out how to get the smaller moon rotating around the big one. Any ideas on how to do this? The images I'm using are attached.
Attachment 2912Attachment 2913Attachment 2914Attachment 2915
public class GUIs extends JFrame
private Image earth = Toolkit.getDefaultToolkit().getImage("picFolder/Earth.gif");
private Image back = Toolkit.getDefaultToolkit().getImage("picFolder/galaxy2.png");
private Image rocket = Toolkit.getDefaultToolkit().getImage("picFolder/Rocket.gif");
private Image moon = Toolkit.getDefaultToolkit().getImage("picFolder/Moon.png");
public void makeEnvironment()
setTitle("This is the title");
setBounds(0, 0, 800, 800);
MyPanel panel = new MyPanel();
private class MyPanel extends JPanel
public void paintComponent(Graphics g)
Graphics2D g2d = (Graphics2D)g;
public static void main(String args)
GUIs gui = new GUIs();
public int rCurrent = 0;
public int rIncrement = 1;
public void drawStuff(Graphics g, Graphics2D g2d)
Thread thread = new Thread();
g.drawImage(earth, 200, 200, 100, 100, this);
g2d.rotate(Math.PI*(rCurrent)/360, 250, 250);
g.drawImage(moon, 400, 210, 60, 60, this);
g2d.rotate(Math.PI*(rCurrent*3)/360, 250, 250);
g.drawImage(rocket, 150, 240, 20, 20, this);
Re: Orbiting Planets and Moons
Do you have any restrictions on this project (is it for school or something)?
If not, I would recommend a far more modular approach. Consider that planets, moons, ect. are all celestial bodies. You are attempting to do everything in a single class, and you are trying to micro-manage each body. A modular approach will allow you to instead macro-manage your "universe".
I would create a class named: CelestialBody. This class would contain a name, an Image, a coordinate, and a reference to a "parent" celestial body (the body which it orbits). The CelestialBody object would be in charge of determining its orbit around its "parent" body.
So, let's say we had the Moon, Earth, and Sun. The moon's "parent" would be Earth. The Earth's "parent" would be the Sun. The moon would calculate its orbit based on the Earth, and the Earth would calculate its orbit based on the Sun.
If you create a general formula which describes how any given CelestialBody orbits its parent, you can just create the method in the CelestialBody class which calculates the body's new location, and when you create the Moon, Earth, and Sun bodies, they will be able to automatically calculate their new locations without you needing to micromanage them.
Since this is gravitational pull, you will probably need to calculate each planet's orbit with consideration from the gravitational pull from all other bodies in the system, but that can be sorted out with a few tweaks.
Re: Orbiting Planets and Moons
Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.
Good first post and an interesting project. Please keep us posted.