1

I'm currently using this code to check my mouse's x and y coordinate and see if it coincides with any of my buttons.

    if (e.getY() > 204 && e.getY() < 280) {
        if (e.getX() > 190 && e.getX() < 525) {
            selected = 1;
        }
    } else if (e.getY() > 322 && e.getY() < 397) {
        if (e.getX() > 231 && e.getX() < 481) {
            selected = 2;
        }
    } else if (e.getY() > 439 && e.getY() < 512) {
        if (e.getX() > 271 && e.getX() < 442) {
            selected = 3;
        }
    } else if (e.getY() > 560 && e.getY() < 634) {
        if (e.getX() > 282 && e.getX() < 425) {
            selected = 4;
        }
    } else {
        selected = 0;
    }

It works 100% but it seems to slow down my program a fair bit. Is there a more efficient way to check and see if my mouse is over one of my 4 buttons?

Thanks

Eric Citaire
  • 4,355
  • 1
  • 29
  • 49
Nat
  • 890
  • 3
  • 11
  • 23
  • 1
    Unless ``getX()`` and ``getY()`` are slow, I do not think this piece of code slows down anything. – Jean Logeart Oct 13 '14 at 15:13
  • 1
    I suggest you look into the partitioning logic of a [k-d tree](http://en.wikipedia.org/wiki/K-d_tree), although for only four buttons it's probably not really going to make much of a difference. As@JeanLogeart says, this really shouldn't be slowing anything down unless it's getting called a *lot* more than once per mouse update or ``get.()`` is really slow. You could probably improve performance slightly by only calling ``getX()`` and ``getY()`` once before the conditionals and storing them to a local, rather than calling them up to 8 times each (which is the majority of the time). – aruisdante Oct 13 '14 at 15:13
  • How are you dealing with the result of this? if you are processing this result in the event dispatcher thread, this might make your program slow to respond and appear laggy. – PeterK Oct 13 '14 at 15:20
  • Are you using SWT or what ? In SWT you have [this](http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fswt%2Fevents%2FMouseTrackListener.html) – StackFlowed Oct 13 '14 at 15:22
  • How much of a slow down are you seeing? How often is this method being called (actual rather than expected), and how fast/slow are getX and getY? – Chris K Oct 13 '14 at 15:24
  • Is this within a ClickListener? A while loop? How often is this thing checking? – Compass Oct 13 '14 at 15:24
  • This is inside `mouseMoved` of a mouse motion listener. I need to use a mouse motion listener as opposed to a clicklistener because I want buttons to highlight when they're hovered over. The solution might be to set getX and getY as local variables before the if statements, Ill give that a shot – Nat Oct 13 '14 at 15:30
  • Ok then ``getX()`` and ``getY()`` do notr have any logic and should be very fast. So I think your slow down is somewhere else in your code. Try to measure it. – Jean Logeart Oct 13 '14 at 15:38
  • Looks like a problem elsewhere in the code. I'll try to track it down, thanks – Nat Oct 13 '14 at 15:39

2 Answers2

3

The only slow part in that code may be the getX() and getY() methods. The if logic should not slow down your program much.

Try using variables instead of calling the functions:

final int x = e.getX();
final int y = e.getY();
// + same logic
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • As far as i understand this make your code more readable and not faster ... http://stackoverflow.com/questions/25433460/calling-a-function-again-better-than-assigning-it-to-variable – StackFlowed Oct 13 '14 at 15:25
  • Ill try this and see if it solves the issue. It may be a problem somewhere else in the program if this doesn't solve it – Nat Oct 13 '14 at 15:32
  • @wrongAnswer Even if the getter called is just a simple "return member", using a local variable is not slowing things down if you are using it at least twice (even if the getter is inlined its still an indirection to hop over, while the local variable is basically free; its allocated on the stack and that is *cached* memory which is very fast). – Durandal Oct 13 '14 at 18:01
0

You should use a private method like private void updateMouse() that you call whenever you need it, then put local variables to store the getter methods int x = e.getX() and int y = e.getY() inside the method, so it will update just when you will call the method updateMouse the getX() and the getY() positions, and you will also save efficiency needed to call getX() and getY().

Then put your code inside it, like this:

private void updateMouse() 
{ 
    int x = e.getX(); 
    int y = e.getY()

    **** your if statement here
}

Now you will be able to run it in specific times, like every 10 frames, and not just continuous. This should allow you to save a little of efficiency.

(Sorry for my bad english)

snailer
  • 62
  • 6