wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Getting mouse button state without events Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Getting mouse button state without events" Watch "Getting mouse button state without events" New topic
Author

Getting mouse button state without events

Bob Chad
Greenhorn

Joined: May 20, 2010
Posts: 19
Hello,

I'm building a Swing program with JOGL. I use mousevents for moving in the 3D scene wich work fine.

In the multipass rendering of the 3dscene <public void display(GLAutoDrawable gld)>
the user can interrupt rendering by moving the mouse

I read the mouse position at the start of the loop
X = MouseInfo.getPointerInfo().getLocation().x;
Y = MouseInfo.getPointerInfo().getLocation().y;

Then read them again inside the loop and break if mouse has moved

I would like to do the same but if the user clicks the mouse.
I can't seem to find a way to do this, there is no function that lets me read mouse button state



Could you help me ?

Thanks,



Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
Not sure why you are using the MouseInfo class. I would think you should be using a MouseListener and MouseMotionListener to listen for events.

Bob Chad
Greenhorn

Joined: May 20, 2010
Posts: 19
Thanks for your reply.

I can't use a listener because i'm inside the Display function of GLCanvas

What i'm trying to do, is to exit a loop by clicking with the mouse button

For {
If mouse button is down then exit for
}

I can do it if the mouse moves
MouseInfo.getPointerInfo().getLocation().y;

Ideally I would need a function like this:
MouseInfo.getButtonStatus

But it doesn't seem to exist

Thanks
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
I can't use a listener because i'm inside the Display function of GLCanvas


Still don't understand the problem. GLCanvas extend Component, so you can add a MouseListener to it.
Bob Chad
Greenhorn

Joined: May 20, 2010
Posts: 19
Hi

thanks for trying to help me out

Yes I have added listeners to my Glcanvas:

glCanvas.addMouseListener(this);
glCanvas.addMouseMotionListener(this);

and I've completed the corresponding mouse functions so the user can move around in the scene

But this doesn't help my problem
I want to exit the display functions on a mouse click

The display function is added this way
glCanvas.addGLEventListener(this);

I need to test the mouse state inside this function

Thanks


Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

You might have to listen to mousePressed and mouseReleased events, and use those to update a state variable that you can then check in your other methods.

Edit: as long as the method you want to break out of isn't running in the Swing thread. If it is the other mouse events will never be processed.
Bob Chad
Greenhorn

Joined: May 20, 2010
Posts: 19
Hi ,

Thanks but this won't work, as I want to break from rendering (display) if mouse is Down while rendering

Say it takes 5 seconds to render, if user presses mouse button after 3 seconds, while program is still in display loop, then quit
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

It would work if the rendering was done outside the Swing thread. And if it's done inside the Swing thread, I think you're stuck as the UI isn't going to respond to anything.
Bob Chad
Greenhorn

Joined: May 20, 2010
Posts: 19
Ok but It does work...the UI is responding

If I use
MouseInfo.getPointerInfo().getLocation().x;
I can test the mouse position to quit the loop

All I need is the same function, but querying the mouse button state.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
All I need is the same function, but querying the mouse button state.


This approach won't work. The mouse will only be "pressed" for an instant (unless you expect your users to hold the mouse down for 5 seconds while the rendering loop executes).

You need to restructure your animation/timing loop to use a Swing Timer to periodically check the state of the execution of the program. In the MouseListener, when ever the mouse is pressed you set a boolean variable to indicate "stop processing". Same thing for the MouseMotionListener.
Bob Chad
Greenhorn

Joined: May 20, 2010
Posts: 19
Hi,
Thanks for your answer.

I'm not sure I understand right.

You say
use a Swing Timer to periodically check the state of the execution of the program


But it's the same problem, the code of the timer needs to get the mouse button state to position the boolean variable
Or otherwise, the code of the timer needs to force execution of the listener to get the button state ?
I don't know how to do that
thanks

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
But it's the same problem, the code of the timer needs to get the mouse button state to position the boolean variable


The MouseListener sets the boolean variable when the mouse is pressed. Then when the Timer fires you check the state of the variable. That way if the mouse is pressed at any time the boolean variable is set and you don't have to check the current state of the mouse button.
Bob Chad
Greenhorn

Joined: May 20, 2010
Posts: 19
Ok thanks, but I don't understand how this can work

Inside the display() function, the mouse listener won't be executed.
The display function runs from start to end without being interrupted

So the boolean variable will be set only when display is finished, or before it is called


Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
If it takes 5 seconds to do your rendering, then the rendering should be on a separate thread so the GUI isn't blocked. Therefore the GUI will still respond to events. So when the mouse event occurs you can set the variable. Now in your rendering code you can check the value of this variable as frequently as you want and stop rendering if requested.
Bob Chad
Greenhorn

Joined: May 20, 2010
Posts: 19
Ok I understand...
Thanks alot,

I'll try this, but I will have to solve the problem of Opengl context in different threads before...

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting mouse button state without events