Mouse Inputs in Processing - PowerPoint PPT Presentation

About This Presentation
Title:

Mouse Inputs in Processing

Description:

Mouse Inputs in Processing Interacting with the Mouse mouseX and mouseY: pg 205-211 The position of the mouse in the canvas pmouseX and pmouseY: pg 208 The previous ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 12
Provided by: Rebecc168
Learn more at: https://www.cs.unca.edu
Category:

less

Transcript and Presenter's Notes

Title: Mouse Inputs in Processing


1
Mouse Inputs in Processing
2
Interacting with the Mouse
  • mouseX and mouseY pg 205-211
  • The position of the mouse in the canvas
  • pmouseX and pmouseY pg 208
  • The previous position of the mouse (in the last
    frame)
  • mouseButton pg 212-213
  • Which mouse button has pressed
  • mousePressed pg 212-213
  • true if the mouse button is pressed, false
    otherwise
  • mousePressed() pg 229-231
  • Function that runs when the mouse is pressed

3
Hello Mouse
  • void setup()
  • size(400, 400)
  • stroke(255)
  • void draw()
  • // try moving background() to setup()
  • background(192, 64, 200)
  • // mouseX is a variable that stores the
    horizontal position
  • // of the mouse.
  • // mouseY is a variable that stores the
    vertical position
  • // of the mouse.
  • line(150, 25, mouseX, mouseY)

4
Moving with the Mouse
  • // Circle follows the cursor
  • void setup()
  • size(100, 100)
  • smooth()
  • noStroke()
  • void draw()
  • background(126)
  • ellipse(mouseX, mouseY, 33, 33)

5
Highlighting with the Mouse
  • // Cursor position selects a quadrant of the
    display window
  • void setup()
  • size(100, 100)
  • noStroke()
  • fill(0)
  • void draw()
  • background(204)
  • if ((mouseX lt 50) (mouseY lt 50))
  • rect(0, 0, 50, 50) // Upper-left
  • else if ((mouseX lt 50) (mouseY gt 50))
  • rect(0, 50, 50, 50) // Lower-left
  • else if ((mouseX gt 50) (mouseY lt 50))
  • rect(50, 0, 50, 50) // Upper-right
  • else
  • rect(50, 50, 50, 50) // Lower-right

6
pmouse
  • // Draw a line between the current and previous
    positions
  • void setup()
  • size(100, 100)
  • strokeWeight(8)
  • smooth()
  • void draw()
  • background(204)
  • // pmouseX is a variable that stores the
    horizontal position
  • // of the mouse in the previous frame.
  • // pmouseY is a variable that stores the vertical
    position of
  • // the mouse in the previous frame.
  • line(mouseX, mouseY, pmouseX, pmouseY)

7
mousePressed and mouseButton
  • Like mouseX and mouseY, mousePressed is a
    property of the mouse (a variable), but it is of
    type boolean.
  • The mousePressed variable stores true if any
    mouse button is pressed, and false otherwise. It
    reverts to false as soon as the button is
    released.
  • The mouseButton variable is LEFT, CENTER, or
    RIGHT depending on the mouse button most recently
    pressed.
  • mouseButton retains its value until a different
    mouse button is pressed

8
A Simple Paint Example
  • int brushSize 20
  • void setup()
  • size(400, 400)
  • smooth()
  • noStroke()
  • background(255)
  • void draw()
  • if (mousePressed)
  • brush(mouseX, mouseY)
  • void brush(int x, int y)
  • fill(CC3300)
  • // note the color is specified as a
    hexadecimal number
  • arc(x, y, brushSize, brushSize, 0, PI)
  • fill(003333)

9
mousePressed()
  • We can try to get the same functionality by using
    the mousePressed() event instead of the
    mousePressed variable.
  • To use event, we need to write a function by the
    same name in our sketch, and it will be called
    automatically every time the event occurs. In the
    following code, mousePressed() will be called
    every time the mouse is pressed.

10
  • int brushSize 20
  • void setup()
  • size(400, 400)
  • smooth()
  • noStroke()
  • background(255)
  • void draw() // keep the motor running
  • void mousePressed()
  • brush(mouseX, mouseY)
  • void brush(int x, int y)
  • fill(CC3300)
  • arc(x, y, brushSize, brushSize, 0, PI)
  • fill(003333)

11
mousePressed vs. mousePressed()
  • You'll notice that the program does not behave
    the same way. Although the mousePressed variable
    and the mousePressed() event have the same name,
    they represent different things
  • mousePressed is true for as long as the mouse
    button is down, whereas mousePressed() is called
    once whenever the mouse becomes pressed, i.e.
    once per click.
Write a Comment
User Comments (0)
About PowerShow.com