Hi there. Complete beginner on Java and OO principles. Having a little trouble in trying to figure out how to make objects move across the screen. Eventually, this will be a model of the solar system.

So I have a planetObject class that represents a planet in the solar system:

public class PlanetObject {

    private double distance;
    private double angle;
    private double diameter;
    private String colour;
    private double speed;

    public double getDistance(){
        return distance;
    }
    public double getAngle(){
        return angle;
    }
    public double getDiameter(){
        return diameter;
    }
    public String getColour(){
        return colour;
    }
    public double getSpeed(){
        return speed;
    }

    public double move(){
        angle += speed;
        return angle;
    }

    public PlanetObject(double distance, double angle, double diameter, String colour, double speed){
        this.distance = distance;
        this.angle = angle;
        this.diameter = diameter;
        this.colour = colour;
        this.speed = speed;

    }
}

And a solarModel class - think of this as a driver

public class SolarModel {

    public static void main(String[] args){
        SolarSystem model = new SolarSystem(700, 700);

        /* Start the game loop. Invoke each object to the screen */ 
        for(int i = 10; i <= 4000; i++){
            /* Draw the sun */
            SunObject sun = new SunObject(30, 50, 50, "YELLOW", 0);
            model.drawSolarObject(sun.getDistance(), sun.getAngle(), sun.getDiameter(), sun.getColour());

            /*Draw the earth */
            PlanetObject earth = new PlanetObject(100, 30, 15, "GREEN", 10);
            model.drawSolarObject(earth.getDistance(), earth.getAngle(), earth.getDiameter(), earth.getColour());
            earth.move();

            model.finishedDrawing();
        }
    }

}

Now, the class SolarSystem is supplied already, I am just invoking it and using the drawSolarObject() method by passing in the values.

My problem is, I cannot get the earth to rotate around the sun. Help would be much appreciated, thanks.

  1. I don't see any call to the move() method anywhere.
  2. You probably want to create the Earth outside the loop.

Ok. Added the earth.move() below my drawSolarObject. Shall I keep the loop for the sun? Seems pointless as it doesn't move

What graphics are you using - Swing?

It is swing, yes. It uses polar coordinates, which is throwing me off a bit. I changed my SolarModel to the following:

public class SolarModel {

    public static void main(String[] args){
        SolarSystem model = new SolarSystem(700, 700);

        /* Start the game loop. Invoke each object to the screen */
        SunObject sun = new SunObject(30, 50, 50, "YELLOW", 0);
        PlanetObject earth = new PlanetObject(100, 30, 15, "GREEN", 1);

        while(true){
            model.drawSolarObject(sun.getDistance(), sun.getAngle(), sun.getDiameter(), sun.getColour());
            model.drawSolarObject(earth.getDistance(), earth.move() + earth.getAngle(), earth.getDiameter(), earth.getColour());
            model.finishedDrawing();
        }
    }

}

But as the angle changes, it increases the distance, so the earth is not following a circular orbit. Any ideas why this is happening?

Fixed - problem solved!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.