• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MouseEvent too slow or am I missing something?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ivan, welcome to the Ranch! Please UseOneThreadPerQuestion <--link. I've locked your other thread over in Game Development.
 
Ivan Slavka
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);.
 
Ivan Slavka
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic