LWN.net Logo

An example program in Processing

    // dots_v1.pde -- draw a multicolored mouse trail
    // (C) 2012 Michael Kerrisk, licensed GNU GPLv2 or later
 
    int strokeweight = 20;               // Weight of drawing stroke
    int bgcolor = 50;                    // Color for canvas background
 
    void setup() {         // Executed once
      size(400, 400);                    // Canvas size
      smooth();                          // Enable antialiasing                  
      background(bgcolor);               // Set background colour for canvas
                                         // (RGB levels all the same, thus grey)
      strokeWeight(strokeweight);        // Set thickness of stroke
 
      textSize(20);
      textAlign(CENTER);
      text("Click the mouse here to start", 200, 150);
      text("Space bar: clear canvas", 200, 200);
      text("Left/right arrows: change stroke size", 200, 250);
    }
 
    void draw() {          // Executed continuously
      if (focused)                       // If we have mouse focus
        point(mouseX, mouseY);
    }
 
    void keyTyped() {      // Executed when a key is typed
      if (key == int(' '))               // Space bar clears the canvas
        background(bgcolor);
    }
 
    void mouseMoved() {    // Executed on mouse moves
      stroke(random(255), random(255), random(255));
    }
 
    boolean firstClick = true;
 
    void mouseClicked() {  // Executed for click of any mouse button
      if (firstClick) {
        firstClick = false;
        background(bgcolor);
      }
    }
 
    void keyPressed() {    // Executed on any keyboard key press
      if (key == CODED) {           // Is it a key sequence?
        if (keyCode == LEFT && strokeweight > 1) {           // Left arrow
          strokeweight--;
          strokeWeight(strokeweight);
        } else if (keyCode == RIGHT && strokeweight < 50) {  // Right arrow
          strokeweight++;
          strokeWeight(strokeweight);
        }
      }
    }

(Log in to post comments)

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds