Example
// Learning Processing
// Daniel Shiffman
// http://www.learningprocessing.com

// Example 3-2: mouseX and mouseY

void setup() {
  size(200,200);
}

void draw() {
  // Try moving background() to setup() and see the difference!
  background(255);

  // Body
  stroke(0);
  fill(175);
  rectMode(CENTER);
  
  // mouseX is a keyword that the sketch replaces with the horizontal position of the mouse.
  // mouseY is a keyword that the sketch replaces with the vertical position of the mouse.
  rect(mouseX,mouseY,50,50);   
}
  • Moisa

    I like that!

  • Indu

    Is there a way to set the size of the window equal to the size of the screen/interface on which the applet is displayed? 
    Thanks!

  • shiffman

    Processing 1.5.1:

    size(screen.width,screen.height)

    Processing 2.0 alpha:

    size(screenWidth,screenHeight);

  • Rey

    Why does rectMode(CENTER); has to be repeated within the draw() cycle? Can it be defined once, right after “size(200, 200,);” in the setup() section?

  • shiffman

    Yes, it absolutely could.  However, I find that it’s often good practice to specify the mode at the moment you draw the shape as you often draw many shapes with varying modes throughout your code.

  • Zhang

    what different between screenWidth and displayWidth?
    Thanks!

  • shiffman

    The variable name has changed with different versions of Processing. It’s http://processing.org/reference/displayWidth.html now.