mouseWheel() syntax question SOLVED

edited November 2016 in How To...

Hello. What is the proper syntax for mouseWheel() function?

I want to make this snippet of code work:

void mouseWheel(UP){

port.write("u");

}

void mouseWheel(DOWN){

port.write("d");

}

Thanks very much!

Answers

  • I would also like to know how to use the mouse left and right as inputs as well.

    Ex: void mouseWheel(LEFT) or (RIGHT), etc.

    Thanks!

  • Thanks, but neither of those make any sense to me. Could you elaborate on them? mouseWheel(MouseEvent event){

    how does it know which direction you moved the wheel? Do you replace 'event' with 'UP' or something?

  • edited October 2013

    As GoToLoop had given you the reference for the mouseWheel Event: So here what is happening

      void mouseWheel(MouseEvent event) {
        float e = event.getAmount();
        println(e);
      }
    

    float e returns the direction of the wheen. How? here is the example

            void setup() {
              size(100, 100);
            }
    
            void draw() {
            } 
    
            int up=0, down=0;
            void mouseWheel(MouseEvent event) {
              float e = event.getAmount();
              if (e>0) {
                up++; 
                down =0;
              }
              if (e<0) {
                down++; 
                up=0;
              }
              println("up: " + up + " down: " + down);
            }
    

    P.S. : Further more the drawing application reference was actually my post ( curiousbrain ) :D :D

  • edited April 2014
    void mouseWheel(MouseEvent me) {
      final int inc = keyPressed & keyCode == CONTROL ? -1 : -5;
      diam = constrain(diam + (int) me.getAmount() * inc, 1, 100);
    
      redraw();
    }
    

    We gotta use method get.Amount() from MouseEvent to know whether wheel moved up (-1.0) or down (1.0)! :-\"

    Both get.Amount() & getClickCount() methods are deprecated! Use getCount() from now on:
    diam = constrain(diam + me.getCount()*inc, 1, 100);

  • OK! that makes much more sense. Ill play around with it. Thanks very much!

  • I got it to work perfectly. Thank you so much!

  • edited September 2014

    P.S. : Further more the drawing application reference was actually my post ( curiousbrain )

    I've decided to tweak it a little more. And since I can't post it in the old forum, I'll use the space here! O:-)
    Besides some performance gain, even though it was already fast, fixed a nasty bug which would halt the program until killed! [..]
    That usually happen when dragging the mouse outside canvas! :O

    Anyways, here it is for whom it may concern: 8->

    /** 
     * Drawing & Erasing (v2.44)
     * by  Amnon.Owed & Quadrobite (2013/Aug)
     * mod GoToLoop
     * 
     * forum.processing.org/one/topic/drawing-application-need-help
     *
     * forum.processing.org/two/discussion/598/
     * mousewheel-syntax-question-solved
     */
    
    PGraphics canvas;
    PImage bg;
    
    int diam = 50;
    
    void setup() {
      size(800, 600, JAVA2D);
      frameRate(50);
      noLoop();
      noFill();
    
      canvas = createGraphics(width, height);
    
      canvas.beginDraw();
      canvas.smooth(4);
      canvas.strokeWeight(diam);
      canvas.endDraw();
    
      bg = createBG(0150);
    
      mouseX = width  >> 1;
      mouseY = height >> 1;
    
      mouseButton = CENTER;
      mousePressed();
    }
    
    void draw() {
      background(bg);
      image(canvas, 0, 0);
    
      ellipse(mouseX, mouseY, diam, diam);
    }
    
    void keyPressed() {
      if (key == CODED)  return;
    
      canvas.clear();
      canvas.endDraw();
    
      redraw();
    }
    
    void mousePressed() {
      if (mouseButton != CENTER)  return;
    
      final color c = (color) random(#000000);
      stroke(c);
    
      canvas.stroke(c);
      canvas.endDraw();
    
      redraw();
    }
    
    void mouseMoved() {
      redraw();
    }
    
    void mouseDragged() {
      if (mouseX < 0 | mouseX >= width
        | mouseY < 0 | mouseY >= height)  return;
    
      if (mouseButton == LEFT)  drawCanvas();
      else                      eraseCanvas();
    
      redraw();
    }
    
    void mouseWheel(MouseEvent me) {
      final int inc = keyPressed & keyCode == CONTROL ? -1 : -5;
      diam = constrain(diam + me.getCount()*inc, 1, 100);
    
      canvas.strokeWeight(diam);
      canvas.endDraw();
    
      redraw();
    }
    
    void drawCanvas() {
      canvas.line(mouseX, mouseY, pmouseX, pmouseY);
      canvas.endDraw();
    }
    
    void eraseCanvas() {
      final int cp[] = canvas.pixels, cw = canvas.width;
      final int rad  = diam>>1, radSq = rad*rad;
      final int mx = mouseX, my = mouseY;
    
      final int xStart, yStart, xEnd, yEnd;
    
      xStart = max(mx - rad, 0);
      xEnd   = min(mx + rad, cw);
    
      yStart = max(my - rad, 0);
      yEnd   = min(my + rad, canvas.height);
    
      for (int y = yStart; y != yEnd; y++) {
        final float myySq = sq(my - y);
    
        for (int cwy = cw*y, x = xStart; x != xEnd; x++)
          if (sq(mx - x) + myySq <= radSq)   cp[cwy + x] = 0;
      }
    
      canvas.updatePixels();
    }
    
    PImage createBG(int div) {
      int i = div = constrain(div, 1, 0400);
    
      final PGraphics pg = createGraphics(width, height);
      final int wdiv = width/div, grey = 0400/div;
    
      pg.beginDraw();
      pg.smooth(4);
      pg.noStroke();
    
      while (i-- != 0) {
        pg.fill(i*grey);
        pg.rect(map(i, div, 0, width, 0), 0, wdiv, height);
      }
    
      pg.endDraw();
      return pg.get();
    }
    
Sign In or Register to comment.