0

I'm drawing a circle in JPanel and using Swing Timer to update x,y co-ordinates of the circle.

How to move the circle in circular path.

protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    g2d.setColor(Color.RED);
    Shape planet = new Ellipse2D.Double(x, y, 20, 20);
    g2d.fill(planet);
    g2d.dispose();
}



public void actionPerformed(ActionEvent evt) {
    double R = 200;
    for (double t = 0; t < 2 * Math.PI; t += 0.01) {
        x = R * Math.cos(t) + 0;
        y = R * Math.sin(t) + 0;
        revalidate();
        repaint();
    }
}
Sorter
  • 9,704
  • 6
  • 64
  • 74
  • 1
    Use a Swing Timer, not a for loop. For example, please check [this link](http://stackoverflow.com/questions/19476685/java-keyevent-square-movement). – Hovercraft Full Of Eels Nov 16 '15 at 11:54
  • @HovercraftFullOfEels From the looks of it, that function is what draws the circle, not set a position in the orbital that frame. – Aiman Al-Eryani Nov 16 '15 at 12:17
  • @AimanAl-Eryani: The code has much more wrong with it. For one, it won't compile since it uses an undeclared variable, g2d, and since it uses a for loop without concern for the Swing event thread. As per my comment, there is absolutely no place for a for loop called on the Swing event thread, in this code (or in your solution), since due to Swing threading rules, this will not result in any animation whatsoever. Again, he needs to use a Swing Timer and to tell us about problems with compilation. From the looks of it, he has tried to borrow code, but not well. He's also disposing a Graphics obj – Hovercraft Full Of Eels Nov 16 '15 at 12:43
  • 1
    http://java-sl.com/tip_flatteningpathiterator_moving_shape.html – StanislavL Nov 16 '15 at 13:03
  • @HovercraftFullOfEels Thanks for pointing out. I've updated my code. – Sorter Nov 16 '15 at 13:17
  • 1
    A complete example is shown [here](http://stackoverflow.com/a/3256941/230513). – trashgod Nov 16 '15 at 14:27

2 Answers2

1

You really already have it there.

x = R * Math.cos(t) + 0;
y = R * Math.sin(t) + 0;

The 0s you have there represent the center (x, y respectively) of the circle. So to rotate the circle around in the path of another circle orbital of center 0,0 and radius R2. The orbital theta value (oTheta) will increase once per frame.

double R = 200;
double R2 = 1000;
oTheta += 0.1; // depending on your framerate, the more you add, the faster it will orbit
for (double t = 0; t < 2 * Math.PI; t += 0.01) {
    x = R * Math.cos(t) + 0 + R2 * Math.cos(oTheta);
    y = R * Math.sin(t) + 0 + R2 * Math.sin(oTheta);
    revalidate();
    repaint();
}
Aiman Al-Eryani
  • 709
  • 4
  • 19
0

The loop is not needed. Timer will call this after a specified interval and update oTheta.

public void actionPerformed(ActionEvent evt) {
    oTheta += 0.01;
    x = radius * Math.cos(oTheta) + centerX;
    y = radius * Math.sin(oTheta) + centerY;
    revalidate();
    repaint();
}
Sorter
  • 9,704
  • 6
  • 64
  • 74