This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Applet moving a circle around a oval

 
davis vermillion
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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

 
davis vermillion
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20711
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20711
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20711
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic