Results 1 to 9 of 9
  1. #1
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mneskovic View Post
    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. #3
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    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. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mneskovic View Post
    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. #5
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    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. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mneskovic View Post
    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. #7
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

    Default

    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. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,454
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mneskovic View Post
    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. #9
    mneskovic is offline Member
    Join Date
    Apr 2010
    Posts
    57
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 04-02-2010, 12:48 PM
  2. Hey Guys I need some help with rounding...
    By blueduiker in forum New To Java
    Replies: 8
    Last Post: 12-08-2009, 08:58 AM
  3. Rounding
    By Moncleared in forum New To Java
    Replies: 2
    Last Post: 11-17-2009, 08:16 AM
  4. Need help rounding. =/
    By yo1mcool in forum New To Java
    Replies: 1
    Last Post: 10-07-2008, 05:02 AM
  5. Help with java Rounding
    By silvia in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 07:25 AM

Posting Permissions

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