User Tools

Site Tools


courses:intro.prototyping.fall.2013.sep24
More processing

Today we looked into displaying and manipulating images, and mouse and keyboard input. Even though there was a wide range of how far you individually got in playing around with these concepts, I was happy to see that the important basics seemed to work for everybody.

Images

Add the images to the project in the menu Sketch→Add file…

The minimum to show an image:

PImage myPictureVar; //Creating a variable to contain the image
 
void setup()
{
  myPictureVar = loadImage("myFile.jpg"); //storing the image in the variable
}
 
void draw()
{
  image(myPictureVar,0,0); //drawing the image from our variable at coordinate 0 and 0
}

The command “image()” that we use to place a picture can also be used to resize the picture if you give it height and width.

We also used the command “tint()” to change brightness, color and transparancy of the image, and “filter” to add filters such as GRAY, THRESHOLD, INVERT, BLUR etc.

Input

When a key is pressed it will trigger a function called “keyPressed” if you added it. You write this part completely outside of setup and draw. Within there you can get the value of the key pressed and either use that or just trigger something else.

The minimum:

void keyPressed()
{
  //here something would happen if any key is pressed
  if (key == 'd')
  {
    //here something would happen if the key pressed is 'd' 
  }
}

But for buttons that are not characters? Those keys are CODED, and you have to check for that first. This is how you use the arrow keys:

void keyPressed()
{
  if (key == CODED)
  {
    if (keyCode == UP)
    {
      //here something happens if UP is pressed.
    }
  }
}    

What other CODED keys are there? Look in the reference for “keyCode”.

Very similar is the input using the mouse, where we played with mousePressed() and checked mouseButton for which button was pressed to trigger behaviour.

Almost as a teaser, but with some substance I also showed you that bringing in video is very close to what we did with images earlier. Here's the bare minimum example I showed you:

import processing.video.*;     //bringing in the video library for more functions
Movie myMovie;        //a video variable
 
void setup() {
  myMovie = new Movie(this, "video1.mp4");  //pointing it to my videofile
  myMovie.loop();
  size(640,480);
}
 
void draw() {
  if(myMovie.available())
  {
    myMovie.read();
    image(myMovie, 0,0, 640, 480);  //draw a frame once it's loaded, then loading the next
  }
}

But there was much more you could do with a video. Play with speed, direction, skip in time. Take a look at the long list in the library reference.

So the list of basic commands and system variables that we went through, and you can practice and find in the reference are:

image()
tint()
filter()
 
mousePressed()
mouseButton
 
keyPressed()
key
keyCode

For the next time (Oct 1st) I want you to make something using images, and keyboard or mouse interaction. Even better if you can bring in some from the last time (color, brightness, position, amount, 2D-shapes on top).

If you feel comfortable with the previous stuff, I challenge you to look in on the video functions and make something interactive that manipulates a video.

courses/intro.prototyping.fall.2013.sep24.txt · Last modified: 2013/09/24 07:12 by rickard