I'm having a mouse event problem.

I've tried to use the example shown on Quark's site for multiple windows. (Linux on a BananaPi). Basically it works but the comparison of (event == MouseEvent.CLICK) fails with an error message that the two quantities can't be "compared". If I do a println(event) I get the valid long form of the event. Is there possibly a typo in the example that I haven't spotted? Thanks.

Tagged:

Answers

  • This is the code I've been using: // Experiment with inset window // quotes " " ' '

    import g4p_controls.*;

    GWindow window, window1; int mouseSel; void setup() { background(192, 192, 127); fill(127, 127, 192); stroke(0); window = GWindow.getWindow(this, "My new Window", 40, 20, 480, 320, JAVA2D); window1 = GWindow.getWindow(this, "My new new Window", 240, 20, 480, 320, JAVA2D); window.setActionOnClose(G4P.CLOSE_WINDOW); window.addMouseHandler(this, "windowMouse"); window.addDrawHandler(this, "windowDraw"); window1.setActionOnClose(G4P.CLOSE_WINDOW); window1.addDrawHandler(this, "windowDraw1"); window1.addMouseHandler( this, "windowMouse1"); window1.addData(new MyData()); window.addData(new MyData()); }

    void draw() { rect(100, 100, 100, 100); //line(width/2, height/2, mouseX, mouseY); }

    void windowDraw(PApplet app, GWinData data) { app.background(127, 127, 0); app.strokeWeight(2); app.stroke(0); app.line(app.width/2, app.height/2, app.mouseX, app.mouseY); }

    void windowDraw1(PApplet app1, GWinData data) {

    if (mouseSel ==1) app1.fill(127, 0, 0); else app1.fill(255); app1.background(127, 127, 0); app1.strokeWeight(2); app1.stroke(0); app1.line(app1.width/2, app1.height/2, app1.mouseX, app1.mouseY); app1.rect(50, 50, 100, 100); if (app1.mouseX >=50 && app1.mouseX <=150 && app1.mouseY >= 50 && app1.mouseY <= 150) mouseSel = 1; else { mouseSel = 0; //println(mouseSel); window1.setVisible(true); } }

    public class MyData extends GWinData { public int lastClickX, lastClickY; }

    public void windowMouse1(PApplet app1, GWinData data, MouseEvent event) { MyData myData = (MyData) data; if (event == app1.MouseEvent.CLICK) //println( MouseEvent.CLICK); println(); }

    public void windowMouse(PApplet app, GWinData data, MouseEvent event) { MyData myData = (MyData) data;

    println( MouseEvent.CLICK); println(event ); }

    // void setVisible(false);

  • https://forum.processing.org/two/discussion/8045/how-to-format-code-and-text

    Java can have classes w/ same name but w/ diff. package. :-S

  • I apologize for the poorly formatted code. I've noticed there are various versions of Java (4 I believe). Do you think I may be using the wrong one? The only problem I've run into is with the Mouse Event. It seems the mouse handler statement is calling the function ok but the function isn't returning data in a form that the "if" statement can handle. A simplistic question is where should I go from here?

  • Rather than apologize, you should edit your post and fix the formatting.
    The way it is now it's not possible to help you.
    Moreover I wonder if that even compiles?

  • Yes it compiled with no problem. What I cut and pasted into here is a much messier version of the actual program Word Pad wasn't kind! I'll try again to get a cleaner post. Thank you for your attention to my problem.

  • ````// Experiment with inset window // quotes " " ' '

    import g4p_controls.*;
    
    GWindow window, window1;
    int mouseSel;
    void setup()
    {
      background(192, 192, 127);
      fill(127, 127, 192);
      stroke(0);
      window = GWindow.getWindow(this, "My new Window", 40, 20, 480, 320, JAVA2D);
      window1 = GWindow.getWindow(this, "My new new Window", 240, 20, 480, 320, JAVA2D);
      window.setActionOnClose(G4P.CLOSE_WINDOW);
      window.addMouseHandler(this, "windowMouse");
      window.addDrawHandler(this, "windowDraw");
      window1.setActionOnClose(G4P.CLOSE_WINDOW);
      window1.addDrawHandler(this, "windowDraw1");
      window1.addMouseHandler( this, "windowMouse1");
      window1.addData(new MyData());
      window.addData(new MyData());
    }
    
    void draw()
    {
      rect(100, 100, 100, 100);
      //line(width/2, height/2, mouseX, mouseY);
    }
    
    void windowDraw(PApplet app, GWinData data)
    { 
      app.background(127, 127, 0);
      app.strokeWeight(2);
      app.stroke(0);
      app.line(app.width/2, app.height/2, app.mouseX, app.mouseY);
    }
    
    void windowDraw1(PApplet app1, GWinData data)
    { 
    
      if (mouseSel ==1)
        app1.fill(127, 0, 0);
      else
        app1.fill(255);
      app1.background(127, 127, 0);
      app1.strokeWeight(2);
      app1.stroke(0);
      app1.line(app1.width/2, app1.height/2, app1.mouseX, app1.mouseY);
      app1.rect(50, 50, 100, 100);
      if (app1.mouseX >=50 && app1.mouseX <=150 && app1.mouseY >= 50 && app1.mouseY <= 150) 
        mouseSel = 1;
      else {
        mouseSel = 0;
        //println(mouseSel);
        window1.setVisible(true);
      }
    }
    
    public class MyData extends GWinData {
      public int lastClickX, lastClickY;
    }
    
    
    public void windowMouse1(PApplet app1, GWinData data, MouseEvent event)
    { 
      MyData myData = (MyData) data; 
      if (event == app1.MouseEvent.CLICK)
        //println( MouseEvent.CLICK);
        println();
    }
    
    
    public void windowMouse(PApplet app, GWinData data, MouseEvent event)
    { 
      MyData myData = (MyData) data; 
    
      println( MouseEvent.CLICK);
      println(event );
    }
    
    // void setVisible(false);````````
    
  • Hopefully this makes more sense!

  • edited December 2015 Answer ✓

    Yes it compiled with no problem.

    As I had guessed, your posted code above failed to compile: :-w
    if (event == app1.MouseEvent.CLICK) println();

    processing.event.MouseEvent isn't some processing.core.PApplet's nested class but an actual top class:
    https://GitHub.com/processing/processing/blob/master/core/src/processing/event/MouseEvent.java

    Therefore there's no sense relying on app1, which is a PApplet reference, in order to reach MouseEvent!

    And CLICK is an int constant. So you still need to get an int outta event, which holds a MouseEvent's reference.

    I've found processing.event.Event's method getAction() for it btW:
    https://GitHub.com/processing/processing/blob/master/core/src/processing/event/Event.java#L87

    if (event.getAction() == MouseEvent.CLICK) println();

  • Indeed, that worked, thank you for your effort on my behalf. I blame myself for not fully appreciating that the "processing" group, and Quark's G4P are independent of each other. In any case your suggestion worked fine. Have a Merry Christmas.

Sign In or Register to comment.