Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Orbiting Planets and Moons

  1. #1
    Junior Member
    Join Date
    May 2014
    Thanked 0 Times in 0 Posts

    Default 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.
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.Toolkit;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    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;
    			drawStuff(g, g2d);
    	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();
    		catch(InterruptedException e){e.printStackTrace();}
    		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);

  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default 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.

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    GregBrannon (May 4th, 2014)

  4. #3
    Super Moderator
    Join Date
    Jun 2013
    So. Maryland, USA
    My Mood
    Thanked 698 Times in 680 Posts

    Default 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.