mapping event (modifying x and y)

edited November 2015 in How To...

Hi. I'm working on a "control window", meaning a secondary window that can control the primary one. In that control window I have a little representation of the main window. And i wish i could click on that little section and map mouseEvents to primary window. Issue is that mouseEvent class has no public method to modify x and y values. Is there any java trick I could implement? Thanks in advance.

Answers

  • edited November 2015
    // forum.processing.org/two/discussion/13573/mapping-event-modifying-x-and-y
    // 2015-Nov-20
    
    int mx, my;
    boolean clicked;
    
    void settings() {
      size(300, 200, FX2D);
      smooth(4);
    }
    
    void setup() {
      surface.setTitle("Main");
      runSketch(new String[] {""}, new ControlWindow(this));
    }
    
    void draw() {
      if (clicked) {
        clicked = false;
        println(mx, my);
        background((color) random(#000000));
      }
    }
    
    class ControlWindow extends PApplet {
      final PApplet p;
    
      ControlWindow(PApplet mainWindow) {
        p = mainWindow;
      }
    
      void settings() {
        size(300, 200, JAVA2D);
        smooth(4);
      }
    
      void setup() {
        surface.setTitle("Control");
        noLoop();
      }
    
      void draw() {
      }
    
      void mousePressed() {
        mx = mouseX;
        my = mouseY;
        clicked = true;
        redraw();
      }
    }
    
  • Thanks. This is good. However I'm needing something else. Because for each key/mouse event I need to send an update to the other window using a real MouseEvent object (so maybe creating my own MouseEvent class and making appropiated setters would work).

    EDITED. Nevermind. I'm making the controlwindow drawing area the same size than the target area. That way coordinated are the same and it works. Well, at least works with 800x600 resolution windows :) Maybe in a future I should came back to this "resizing/mapping coords" issue.


    Just a last comment. I tried to alter these variables from one window to another and it crashes with: Exception in thread "Animation Thread" java.lang.NullPointerException. Why?

    sketchWindow.mousePressed = true; sketchWindow.mouseX = event.getX(); sketchWindow.mouseY = event.getY();

  • edited November 2015 Answer ✓

    Exception in thread "Animation Thread" java.lang.NullPointerException. Why?

    Each PApplet got its own canvas and its own "Animation" & "Event Dispatch" threads.
    Messing w/ them in the "wrong" time can be disastrous! :-SS

    sketchWindow.mousePressed = true; can't cause NPE b/c mousePressed is a primitive boolean variable and it's just being assigned true.

    Now the expressions event.getX() & event.getY() cause NPE if event is null.
    Or if getX() or getY() deals w/ something internal that can't be messed w/ at the "wrong" time.

    If it's the latter, you're gonna need to find a way to synchronized () those calls w/ 1 shared mutex.

    Nonetheless, I've come up w/ another rudimentary way to pass ControlWindow's mouseX & mouseY to the corresponding 1s in MainSketch.

    I'm using noLoop() & redraw() as a way to coordinate the mouse events.
    Making the whole thing an event-driven app: :ar!

    // forum.processing.org/two/discussion/13573/mapping-event-modifying-x-and-y
    // 2015-Nov-20
    
    void settings() {
      size(300, 200);
      smooth(4);
      noLoop();
    }
    
    void setup() {
      surface.setTitle("Main");
      runSketch(platformNames, new ControlWindow(this));
    }
    
    void draw() {
      println(mouseX, mouseY);
      background((color) random(#000000));
    }
    
    class ControlWindow extends PApplet {
      final PApplet p;
    
      ControlWindow(PApplet mainWindow) {
        p = mainWindow;
      }
    
      void settings() {
        size(300, 200);
        smooth(4);
        noLoop();
      }
    
      void draw() {
        surface.setTitle("Control" + "   (" + mouseX + ", " + mouseY + ')');
      }
    
      void mousePressed() {
        p.mouseX = mouseX;
        p.mouseY = mouseY;
        p.redraw();
        redraw = true;
      }
    }
    
  • Cool. Good stuff. Thanks so many for the explanation & code.

Sign In or Register to comment.