File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT 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 EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "MouseEvent too slow or am I missing something?" Watch "MouseEvent too slow or am I missing something?" New topic
Author

MouseEvent too slow or am I missing something?

Ivan Slavka
Greenhorn

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



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.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

i doubt it is java too slow. i am dying to see the replies to this thread.

i notice you override paint()

i don't think you are supposed to do that. what class is it? a JPanel? something else?

Thread.sleep(1000 / FRAMES_PER_SECOND);


how much time is that?

i need more information about your main class.


SCJP
Visit my download page
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Ivan, welcome to the Ranch! Please UseOneThreadPerQuestion <--link. I've locked your other thread over in Game Development.


luck, db
There are no new questions, but there may be new answers.
Ivan Slavka
Greenhorn

Joined: Feb 06, 2012
Posts: 11
Thank you for welcoming me to the ranch Darryl, and once again I apologize for double posting.

Hello Randall,

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.

SphereBoard class


Sphere class



I apologize if the code seems a bit hard to read, but as I mentioned I am learning this Java2D and, I was experimenting.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Don't use a Thread, use a javax.swing.Timer. Read Concurrency in Swing for more information.

And as Randall noticed, why do you override paint? When you extend any JComponent sub class you should override paintComponent instead, and make sure the first call is super.paintComponent(g);.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ivan Slavka
Greenhorn

Joined: Feb 06, 2012
Posts: 11
Hello Rob,

well as I said, I'm learning Java2D, so that is one half of the answer to your question, and the other half is that I read many resources,
and couple of books about Java Game programming like,

Java 2 Game Programming by Thomas Petchel and
Beginning Java SE 6 Game Programming, Third Edition by Jonathan S. Harbour,

and in both of these books, they are using Thread not javax.swing.Timer as well they are overriding paint() method.

Now, I'm not here to argue or judge who is correct and who is not, I just want to learn correct Java Game Development(not applets but fullscreen).

So I should use javax.swing.Timer even though I have only JPanel from Swing package, the rest of graphics is awt?
Then is this response (StackOverflow) to the question incorrect?

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19541
    
  16

Ivan Slavka wrote:So I should use javax.swing.Timer even though I have only JPanel from Swing package, the rest of graphics is awt?

Swing is built on top of AWT. That means that you can mix them to some degree. You can still use Graphics, Dimension, Point etc from AWT, but you should not mix AWT and Swing components.

Then is this response (StackOverflow) to the question incorrect?

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.
Ivan Slavka
Greenhorn

Joined: Feb 06, 2012
Posts: 11
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MouseEvent too slow or am I missing something?
 
Similar Threads
A round gauge widget built using Swing
Event Handlers:Mouse over "circles"
MouseEvent too slow or am I missing something?
why it could not work properly