Difficulty with mouseX & mouseY

I am VERY new to Java programming, (about a week now), and trying to complete a very simple Simon Says type game, (where you click on the colored boxes that randomly flash).

My is I can’t get my mouse X & Y coordinates to work. I’ve tried 2 different ways at the same time:

    import java.awt.*;
    Point p = MouseInfo.getPointerInfo().getLocation();
    int x = p.x;
    int y = p.y;
    println(x,y,mouseX,mouseY,);

When I click on the play button I get this:

image

The x,y for the getLocation work, but only for the monitor coordinates, NOT the new windows location. The mouseX&mouseY only give me zeros.

But the funny thing is that as soon as I move my mouse cursor over to my 2nd computer monitor I get this:

(sorry, I can’t post 2 images since I’m a new user)

The mouseX & mouseY update once, and only once, when I move it to the 2nd monitor. Then they remain the same until I move my mouse back to the first monitor, and then back again to the 2nd monitor, at which point they update once and then stay the same.

My getLocation X&Y work fine, but then only return the monitor position, NOT the position within the new window that get’s opened for my simon says game.

I’m not sure if there is some issue with my monitor drivers interfering with something or what. I’ve got just a basic Windows 10 computer with a 2nd monitor, nothing fancy.

Any help with this would be very much appreciated.
Thanks.
Ted

1 Like

Hi Ted,

Welcome to the forum. Processing has it’s own way of handling mouse and keys. Processing reference https://processing.org/reference/ is a good source to find these things. I know you don’t always know where to look at, so you can always ask the forum. Anyways processing has several event handlers for mouse. Event handler is a function that is called by the processing when something happens with the mouse. The one you are looking for is mousePressed() https://processing.org/reference/mousePressed_.html. If you just need mouse coordinates inside the processing window system variables mouseX and mouseY contain them.

Turorial https://processing.org/tutorials/overview/ explains this all too.

2 Likes

Thanks for the suggestions. I have read through the various mousePressed, mouseX, mouseY, mouseMoved, etc tutorials several times. I’ve learned not to bother googling any of my Java questions because unless it’s on the Processing site it usually ends up confusing me more.

I hadn’t used the mousePressed yet because I was just trying to get my various colored boxes to light up as my mouse moved over them. I hadn’t gotten to the point of actually pressing the mouse button yet, (LOL baby steps). While it wasn’t going to be part of my final Simon Says game, I just wanted to see if I could flash the colors as the mouse moved over the colored panels.

The aggravating thing is that I can take this code:

void setup() {
  size(100, 100);
  noStroke();
}

void draw() {
  background(126);
  ellipse(mouseX, mouseY, 33, 33);
    println(mouseX,mouseY);
}

Plug it into another Processing window, and it gives me exactly what I expect to happen. A little window opens and as I move my cursor around in it the circle moves, and I get the cursor coordinates in the text window.

But if I run my code I don’t get that. I get the cursor coordinates for my monitor, not the simon window.

To make this really funny. If I run my simon code, and get monitor coordinates. While I have that window open if I run the other circle code, I get the cursor coordinates for the little window in the Processing text area for the simon code. It’s hard to describe so here’s my one picture:

You can see the 99 89 coordinates are for the little test code window. Somehow by starting the 2nd code, (I assume my simon code is still running in the background), it all started to work. Note in the simon text window I ONLY get the coordinates for when I move the cursor around in the little window, not the whole monitor. I don’t understand how running the 2nd code fixes it.

Anyway, here is my (rather pathetic) code if someone wants to point out the very obvious thing I’m missing:

void setup(){
  size (800,800);
  noStroke();
}
  
void draw(){
    String Quit="No";
    import java.awt.*;
    boolean redActive = false;
    boolean greenActive = false;
    boolean blueActive = false;
    boolean yellowActive = false;
    final int LIT = 255;
    final int UNLIT = 150;
    
  while (Quit=="No") {
    Point p = MouseInfo.getPointerInfo().getLocation();
    int x = p.x;
    int y = p.y;
    int rdnNum = int(random(4));  //ignore random for now
    println(x,y,mouseX,mouseY, rdnNum);  

    if (x<=(width/2) && y<=(height/2)) {
      redActive=true; 
    } else if (y>(height/2)) {
      blueActive=true;
    }
    
    if (redActive) {
      fill(LIT, 0, 0);
    } else {
      fill(UNLIT, 0, 0);
    }
    rect(0, 0, width / 2, height / 2);
    
    if (greenActive) {
      fill(0, LIT, 0);
    } else {
      fill(0, UNLIT, 0);
    }
    rect(width / 2, 0, width / 2, height / 2);
    
    
    if (blueActive) {
      fill(0, 0, LIT);
    } else {
      fill(0, 0, UNLIT);
    }
    rect(0, height / 2, width / 2, height / 2);
    
    
    if (yellowActive) {
      fill(LIT, LIT, 0);
    } else {
      fill(UNLIT, UNLIT, 0);
    }
    rect(width / 2, height / 2, width / 2, height / 2);
    
    fill(5,5,5);
    rect (width/3,height/3,width/3,height/3);
        
     if (x>2000) {  
      Quit="Yes";  
      }
    

    }
   
stop();
}
1 Like
import java.awt.*;
Point p = MouseInfo.getPointerInfo().getLocation();

This will give you coordinates of mouse on the whole display. I don’t see much point to use this in a processing program.

mouseX and mouseY will give you coordinates of the mouse inside the processing window. You can access them within draw() or any of the eventhandlers.

It seems that you have all the necessary code in place already. You just need to replace that MouseInfo stuff with mouseX and mouseY.

1 Like