This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
The moose likes Game Development and the fly likes MouseEvent too slow or am I missing something? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Game Development
Reply locked New topic

MouseEvent too slow or am I missing something?

Ivan Slavka

Joined: Feb 06, 2012
Posts: 11
Hi guys,

a month ago I decided to try to create a simple game. Well actually it's mostly playing with Java2D then creating a game.
Anyway here is my problem. I decided to learn how to move a shape (Sphere) based on the mouse position.
Ex. If I drag the mouse, the sphere follows the mouse, if I just press the button the sphere starts to move toward the mouse position.

My problem is I can't figure out how to stop the sphere when it's bounds contain the mouse position, basically when the sphere gets to the mouse it should stop.
Well to be more precise, I know how to do the logic to calculate if the mouse point is inside the sphere bounds, but the sphere doesn't stop. It just passes through.
And on some occasions when I start to drag the mouse very fast it stops.

At first I thought that it was concurrency problem (Main thread paints the sphere, MouseEvent thread assigns the mouse position), and I tested with locks, but the result was the same.
Is it possible that Java is just too slow for even this kind of simple animation, or am I missing something?

Here is my code:

Main class

Ball class
Darryl Burke

Joined: May 03, 2008
Posts: 4754

Please UseOneThreadPerQuestion. I'm locking this one. Continue in

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link:
subject: MouseEvent too slow or am I missing something?