We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have been banging my head against this for hours now.
I would like to know how to get the x and y coordinates of the Java GLWindow relative to my screen.
When I call frame.getLocation(), I receive (0,23) which I believe is the render frame inside of the GLWindow.
However, I would like to know how to get the x,y coordinates of that window!
Any advice?
Answers
I think the robot library has methods for this.
Depends on which version of Processing you are using.
This works in Processing 3
Finally some1 join in my club of daring Java skills. I also love to check constant String objects w/
==
operator instead of that boring equals() method. \m/And why not
|
instead of||
. That'd be perfect:if (renderer == P2D | renderer == P3D)
:PI don't use
if (renderer == P2D | renderer == P3D)
because the second condition is always evaluated even if the first condition is true.
In this conditional it doesn't make any sense to use the bitwise-or (|) because if
renderer == P2D
is true thenrenderer == P3D
must be false so why evaluate it.I have the same aversion to using & rather than && in conditional statements.
@Quark: perfect answer. Thank you.
For those interested. The reason I needed to implement this was because our application was frequently crashing when we used CMD+q or hit the 'x' button at the top left. We realized it it was because sometimes we would leave the frame with our cursor, but in the process hover over a button. The frameRate was not fast enough to track the mouse position between between when it left the button and exited the Frame. This would result in the sketch getting hung up when trying to close.
Here's the full fix:
Cheers
Of course, that's why it's called eager eval, while the other is lazy eval + short-circuit.
However, given the evaluation of
renderer == P2D | renderer == P3D
is so fast, by the time the short-circuit is set up prior to the lazy eval version, the former had already finished! $-)The || uses short-circuit evaluation. The terms eager and lazy evaluation are used in a different context.
Since the short-circuit is setup by the compiler and not at runtime then this statement is incorrect.
I have changed the method to simplify the byte code
Here is the byte code for using the ||
Line 13 is the short circuit
Here is the byte code for |
Since the dawn of Java programmers have used the condition and/or operators in conditional statements. The only reason for using the bitwise is to force the evaluation of both conditions - a very rare need.
Dunno much bytecode specifics, so it's hard for me to judge which 1 is actually faster.
However, The eager eval w/o short-circuit relies on GOTO, which is the fastest memory address jumper available in machine code AFAIK. :ar!
The only time | will be quicker than || is if the second has to be evaluated e.g.
(a == 1 | b == 6)
will ONLY be faster than(a == 1 || b == 6)
if the first condition is false i.e. when a != 1Wow! Y'all are really nerding out now :P
Anyways, I've come up w/ a more complete solution which includes FX2D renderer as well.
Lotsa pain to dig in given inner class ResizableCanvas from processing.javafx.PSurfaceFX isn't
public
.And to make things worse,
package
javafx wasn't importable in Processing either! @-)So I had to go full reflection all the way down until reaching methods getX() & getY() for FX2D: #:-S
That depends whether IF_ACMPEQ L2 short-circuit is fast enough to beat down the GOTOs. :P
Evaluating something as simple as
renderer == P3D
is instantaneous after all. >-)There are no GOTOs here and nothing is instantaneous and the statements I made in my last post are correct.
In this particular case the speed difference will be measured in nanoseconds so it's not important, what is important is promoting good programming practice especially in a forum like this where many of the programmers are inexperienced.
Using the conditional operators && and || In conditional statements is good practice that has been accepted for decades. As I said, there will be some very, very rare occasions when it is important for both expressions to be evaluated in which case the bitwise operators should be used.
If being nerdy means pointing out good practice then I happy to be nerdy :)
Yeah sorry about that, I missed those GOTOs :\"> but it doesn't affect the conclusions I reached in my last 2 posts.