-1

I have been doing a ton of research and none of the questions I found really answered my question and that is why I am making this post.

I want to create a program that will have a circle, a "planet" orbit around a another circle, a "Sun."

I have the static gui set up, but nothing I have found in my book or online really helps solves the orbit problem. Any ideas?

NOTE: eventually the program needs to be multithreaded (one for the planet and one for the Sun) but I want to break the problem down before I get back into trying to get that to work so for now please disregard it.

GUI:

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;


public class PlanetsGUI extends JPanel
{   
private static final long serialVersionUID = 1L;

@Override
public void paintComponent(Graphics g) 
{
    super.paintComponent(g); 
    setBackground(Color.BLACK);
    paintSun(g);
    paintPlanet(g);
}

public void paintSun(Graphics g)
{
    super.paintComponent(g);

    //create circle and fill it as yellow to represent the sun
    g.setColor(Color.YELLOW);
    g.drawOval(100, 75, 75, 75);
    g.fillOval(100, 75, 75, 75);
} //end paintSun


public void paintPlanet(Graphics g)
{
    //create circle and fill it as blue to represent the orbiting planet
    g.setColor(Color.BLUE);
    g.drawOval(35, 50, 50, 50);
    g.fillOval(35, 50, 50, 50);


}//end paintPlanet

}//end class PlanetsGUI

MAIN:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.JFrame;


public class OrbitingPlants_main 
{
  private static final ExecutorService execute + Executors.newFixedThreadPool(2);

public static void main(String[] args) 
{

    PlanetsGUI planet = new PlanetsGUI();
    JFrame frame = new JFrame();

    frame.setTitle("Orbiting Planets");
    frame.setSize(300, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(planet); //add panel onto frame
    frame.setVisible(true);

    //PlanetsLogic r = new PlanetsLogic();
    //Thread sun = new Thread(sun);
    //sun.start();

    //execute.submit(new );





}//end main     


}//end class
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
user3712626
  • 115
  • 1
  • 9
  • Where you draw the image of the "planet", use variables to change the x and y coordinates to imitate "rotation" around the sun. You can use the unit circle ( originX + sin(value), originY + cos(value) ) – Steven Nov 24 '15 at 00:26
  • @StevenKimarro Im confused how do you get a value for 'value' that is inserted into sin and cos? Do I hard code in the unit circle values? – user3712626 Nov 24 '15 at 00:49
  • Basically the transformation of a trig equation is as follows: a*sin(b*x + c) where a is amplitude, b is the stretch, and c is the offset. Play around with it to get the perfect "circular movement". – Steven Nov 24 '15 at 00:59
  • Would that make 'originX' = to my xSun ( which is equal to 100)? – user3712626 Nov 24 '15 at 01:01
  • @StevenKimarro so the quantity asin(bx + c) would be: a = SunsHeight b = SunWidth, x = X corrd of the Sun and C = the spacing between the sun and planet, lets say 25px? Or do I have this all wrong? – user3712626 Nov 24 '15 at 01:05
  • the origin would be the position of the sun, yes. In this case a = rotation distance from the sun (how far it is from the sun), b = how fast it rotates, and x = time, and c should be 0. – Steven Nov 24 '15 at 01:11
  • http://funprogramming.org/68-Circular-motion-reviewed.html – Steven Nov 24 '15 at 01:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95975/discussion-between-user3712626-and-steven-kimarro). – user3712626 Nov 24 '15 at 01:34
  • 1
    See also this complete [example](http://stackoverflow.com/a/3256941/230513). – trashgod Nov 24 '15 at 02:42

1 Answers1

1

You're hard-coding where the images are being drawn, for example:

g.setColor(Color.BLUE);
g.drawOval(35, 50, 50, 50);
g.fillOval(35, 50, 50, 50);

making for images that can never change.

Instead get rid of the hard coded numbers and use variables, values that can change:

g.setColor(Color.BLUE);
// variables used below are fields declared in the class
g.drawOval(planetX, planetY, planetW, planetW); // probably don't need this line
g.fillOval(planetX, planetY, planetW, planetW);

and then change the values in your thread (or better, Swing Timer).

As far as "orbiting", that would be using the equations of a circle (or ellipse if you wanted to be extremely precise), and perhaps using a parametric equation to determine your planetX and planetY values.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373