
Science SPORE Prize
November 2011

The Open Source Physics Project is supported by NSF DUE-0442581.
|
Mouse position.
post and replies
Return to the Mouse position. thread
Login to post to this thread
|
Mouse position
- Feb 28 at 1:43PM
|
Robert MacKay
4 Posts
|
How does one access the move position (x y) shown in the yellow box of the drawing panel? I wanted to click on the screen and use the mouse position for a trace or a new drawable circle or rectangle. This seems way too easy but I haven't yet found an example that uses this. Any help will be appreciated
|
|
Re: Mouse position - Feb 28 2:06PM
|
Francisco Esquembre
72 Posts
|
Hi Robert,
Suppose your drawing panel is called "myDrawingPanel", then:
double x = _view.myDrawingPanel.getMouseX(); double y = _view.myDrawingPanel.getMouseY();
give you what you want in the panel world coordinates.
While
int px = _view.myDrawingPanel.getMouseIntX(); int py = _view.myDrawingPanel.getMouseIntY();
give you the point in pixels (if that is of any use for you).
Paco
|
|
Re: Re: Mouse position - Feb 28 2:24PM
|
Robert MacKay
4 Posts
|
Thank you for your quick reply. I'll give that a try.
|
|
Re: Re: Mouse position - Feb 28 4:51PM
|
Robert MacKay
4 Posts
|
Hi Paco, I tried your suggestion.
my drawing panel is named drawingPanel for this test. My code in the evolution panel (of model) is:
int px = _view.drawingPanel.getMouseIntX(); int py = _view.drawingPanel.getMouseIntY(); _println("x= " +px +"y= "+py);
It seems to compile okay but I get the message (in output area)
Exception in thread "Thread-2" java.lang.NullPointerException at org.opensourcephysics.display.InteractivePanel.getMouseIntX(Unknown Source) at ch4.tryimage_pkg.tryimage._evolution1(tryimage.java:235) at ch4.tryimage_pkg.tryimage._stepModel(tryimage.java:190) at org.colos.ejs.library.Animation.step(Unknown Source) at org.colos.ejs.library.Animation.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
I also tried x = _view.drawingPanel.getMouseX(); y = _view.drawingPanel.getMouseY();
and got a similar error message.
Do I need to import something before running this or build a custom class? Thank you for your patience. Bob
|
|
Re: Re: Re: Mouse position - Feb 28 5:46PM
|
Francisco Esquembre
72 Posts
|
Hi Bob,
I see that you are calling the methods in the Evolution. This will not work.
You must call the methods as response to a panel action (one of On Press, On Drag, or On Release). Only then, the methods return something sensible.
Otherwise, you get these null pointer errors.
In the attached example, the On Press action of the panel calls your code and prints the correct information.
Paco
Attached File: ejs_PanelClick.zip
|
|
Re: Re: Re: Re: Mouse position - Feb 28 6:49PM
|
Robert MacKay
4 Posts
|
Thank you so much. this should be perfect for my planned use. Bob
|
|