aspose file tools*
The moose likes Applets and the fly likes Java Applet moving a circle around a oval Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "Java Applet moving a circle around a oval" Watch "Java Applet moving a circle around a oval" New topic
Author

Java Applet moving a circle around a oval

davis vermillion
Greenhorn

Joined: Jul 20, 2012
Posts: 4
Hi,

Could anyone help me move this circle around the oval? Or could anyone tell me where i should start?

davis vermillion
Greenhorn

Joined: Jul 20, 2012
Posts: 4
If anyone knows how please reply soon as this is a urgent problem (assignment) because apparently my java teacher went on leave for a few weeks now for unknown reason so we had a sub teacher which doesn't know anything about java apparently and called us to figure it out ourself.

Any help would be greatly appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

If you want something to happen repeatedly on a schedule, like every 500 milliseconds in your example, then a Swing timer is the way to go. In fact you should be using a Swing JApplet instead of an AWT Applet in your code; AWT has been obsolete for over 10 years now.

Simply have your Swing timer object repaint the applet every 500 milliseconds, and put code into the paintComponent (not paint) method which draws a different circle each time it's run.
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
for the movement, your code won't work

g.drawOval(75,20,150,115);
g.fillOval(90,25,20,20); //<---this one

it will be redrawn repeatedly at the same spot.

you need to use variables (class ones, accessible by the painting method, and the timer)
so it would start out

int x = 90;
int y = 25;

then in paint/paintComponent it would be
g.drawOval(75,20,150,115);
g.fillOval(x,y,20,20);

now, in your timer, you adjust x and y
say you wanted the next small circle drawn 10 over and 10 up, in the timer you'd have
x += 10;
y -= 10;
repaint();

if you only want the single small circle shown in one location (moving), you'd need to include this as the first line in paintComponent
super.paintComponent(g);

once you get it all working, comment out the above line and see the difference.
davis vermillion
Greenhorn

Joined: Jul 20, 2012
Posts: 4
Is there some sort of calculation method so that it can automatically move along the outline of the oval? Or do i have to try and test to calculate?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Sure there's a method to calculate where the circle should go.

Although having said that, you really haven't said what you mean by "move this circle around the oval". I didn't see any circle in your original post, only an oval. But assuming that you might mean that the circle should be tangent to the outside (or maybe the inside) of the oval and that it should appear to roll around while continuing to be tangent to the oval, then yes, the calculation is just a matter of geometry.

However it sounds to me like that isn't going to be a very helpful answer. But at the moment it's the best I can give, based on what you've posted so far.
davis vermillion
Greenhorn

Joined: Jul 20, 2012
Posts: 4
Yes that is what i meant, is there any example codes i can see as i have no knowledge of geometry.

The exact question the assignment gave is to implement the logic using multi-threads to make the ball (g.fillOval(90,25,20,20);) roll along the oval. The speed of the movement should be reasonable for a user to observe. This must be implemented using Runnable interface and its methods built in Java.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18669
    
    8

Well, first you have to find a point on the fixed oval. (Actually you have to find a set of them which are reasonably close together, but you only work with one point at a time.) Then you have to find the line which is perpendicular to the fixed oval at that point. And finally you have to find a point which is 10 pixels along that line, moving away from the oval. That's the centre of your moving circle.

At least I think that's right. But if you don't know anything about geometry then you aren't going to be able to do those calculations.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Applet moving a circle around a oval