Make shape rounding (moving in circle)

• 08-17-2010, 09:58 AM
mneskovic
Make shape rounding (moving in circle)
Hello!

I have one JApplet and JPanel on it. I have drawn a shape, actually a ball, on JPanel and I would like to make it move in circle (to make it rounding).

Can you explain me how to do that?

It would be very helpful for me.
• 08-17-2010, 10:33 AM
JosAH
Quote:

Originally Posted by mneskovic
Hello!

I have one JApplet and JPanel on it. I have drawn a shape, actually a ball, on JPanel and I would like to make it move in circle (to make it rounding).

Can you explain me how to do that?

It would be very helpful for me.

If the circle has a radius R and the ball has a radius r where r < R (otherwise the ball won't fit in the circle), the ball makes a circular motion over the circle (m, R-r) where m is the midpoint of the original circle.

kind regards,

Jos
• 08-17-2010, 10:58 AM
mneskovic
OK, I understood explanation. Thank you very much for that.

I don't know how to code that. I have drawn a shape in paintComponent method like this:
Code:

`g.fillOval(50, 50, 20, 20);`
Where and how to deal with the moving part?

Best regards,
Milica
• 08-17-2010, 12:55 PM
JosAH
Quote:

Originally Posted by mneskovic
OK, I understood explanation. Thank you very much for that.

I don't know how to code that. I have drawn a shape in paintComponent method like this:
Code:

`g.fillOval(50, 50, 20, 20);`
Where and how to deal with the moving part?

Best regards,
Milica

Assume the circle's midpoint is the origin (0, 0); the midpoint of the ball makes a circular movement over a cirlce with radius R-r, so every point can be described as x= (R-r)*cos(phi), y= (R-r)*sin(phi) for any value of phi in [0, 2*PI).

You don't want to know where the midpoint of the ball is, you want to know the top left position of the bounding square; if the midpoint of the ball is at position (x, y) (see above) then the top left point of the bounding rectangle is at position (x-(R-r), y-(R-r))

The variable phi serves as a loop counter.

kind regards,

Jos
• 08-17-2010, 01:25 PM
mneskovic
Thank you very much for this explanation.

Should I use timer for this moving (to put that loop's code in it) or something similar?
• 08-17-2010, 01:53 PM
JosAH
Quote:

Originally Posted by mneskovic
Thank you very much for this explanation.

Should I use timer for this moving (to put that loop's code in it) or something similar?

Yep, use a Timer in another thread; it should adjust the value of variable phi and repaint() the component.

kind regards,

Jos
• 08-17-2010, 02:08 PM
mneskovic
Can you explain me how to put timer in thread?

I have been making timers like this:
Code:

```        timer1 = new Timer(500, new ActionListener() {         public void actionPerformed(ActionEvent e) {             x1++;         PanelName.this.repaint();         }});```
and then I call variable x1 in paintComponent method and start timer in button defined to start it.

I would like to stop delays in timer, but if I put 0 instead of 500 it doesn't work well.

How to do that?
• 08-17-2010, 02:16 PM
JosAH
Quote:

Originally Posted by mneskovic
Can you explain me how to put timer in thread?

I have been making timers like this:
Code:

```        timer1 = new Timer(500, new ActionListener() {         public void actionPerformed(ActionEvent e) {             x1++;         PanelName.this.repaint();         }});```
and then I call variable x1 in paintComponent method and start timer in button defined to start it.

I would like to stop delays in timer, but if I put 0 instead of 500 it doesn't work well.

How to do that?

Read the API documentation for that Times class; you can stop it (and start another Timer if wanted/needed).

kind regards,

Jos
• 08-17-2010, 03:05 PM
mneskovic
OK, thank you, I will take a look.

Greetings!