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:
I apologize for double posting this thread, I posted it by mistake to the game development forum, and I don't know how to move it here.
Ivan, welcome to the Ranch! Please UseOneThreadPerQuestion <--link. I've locked your other thread over in Game Development.
There are no new questions, but there may be new answers.
Joined: Feb 06, 2012
Thank you for welcoming me to the ranch Darryl, and once again I apologize for double posting.
yes I am overriding the paint() method of the JPanel. If I don't override it, where else would I draw my images?
I didn't want to post my whole Main class, in order not to scare other people , if you need more information I will gladly provide.
Here is my complete code of the above mentioned classes:
Notice it, doesn't have main method, that's because in my Main class I only instantiate the JFrame and then I add SphereBoard as a contentPane.
I apologize if the code seems a bit hard to read, but as I mentioned I am learning this Java2D and, I was experimenting.
It's correct. You actually can use a Thread, but then all interactions with the user interface needs to be done using EventQueue.invokeLater and/or EventQueue.invokeAndWait (SwingUtilities has the same methods which redirect to EventQueue). If you use a javax.swing.Timer then all interactions from the ActionListener will already occur on the Event Dispatcher Thread. Since your thread is a repetitive job a Timer is a better option, as you don't need to do the hard work (sleeping, calling invokeLater) yourself.
And about the paint() method. This article Oracle says to use paint with awt and paintComponent with Swing. And I'm using awt to draw the shapes.
It's not the drawing that matters but the component you're drawing for. In this case that's a JPanel, and that's a Swing component. If you'd use a java.awt.Panel then you'd need to override paint instead.
Joined: Feb 06, 2012
Guys, sorry for the trouble, but I figured out, what was the problem, my condition was incorrect. I fixed it by multiplying the radius. Because before it was only taking into the consideration only upper quarter as a boundary.
I was trying to resolve this for two days, I'm not sure if I'm suppose to laugh or cry.
Anyway, Rob thanks for the insight you gave me regarding the Timer and paint() method, I'll make sure to implement my code that way.
subject: MouseEvent too slow or am I missing something?