# Thread: Make shape rounding (moving in circle)

1. Member
Join Date
Apr 2010
Posts
57
Rep Power
0

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

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

3. Member
Join Date
Apr 2010
Posts
57
Rep Power
0
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:
Java Code:
`g.fillOval(50, 50, 20, 20);`
Where and how to deal with the moving part?

Best regards,
Milica

4. 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:
Java 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

5. Member
Join Date
Apr 2010
Posts
57
Rep Power
0
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?
Last edited by mneskovic; 08-17-2010 at 01:28 PM.

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

7. Member
Join Date
Apr 2010
Posts
57
Rep Power
0
Can you explain me how to put timer in thread?

I have been making timers like this:
Java 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?

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

I have been making timers like this:
Java 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

9. Member
Join Date
Apr 2010
Posts
57
Rep Power
0
OK, thank you, I will take a look.

Greetings!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•