Difference between revisions of "Processing Introduction"

From Interaction Station Wiki
Jump to navigation Jump to search
Line 25: Line 25:
  
  
 +
 +
Now, let's try to draw something
 +
 +
<syntaxhighlight lang="java" line='line'>
 +
 +
void setup(){
 +
size(400,400);
 +
//println("hello!Nan");
 +
background(30,50,80);
 +
 +
}
 +
void draw(){
 +
//println("welcome to nteraction station!");
 +
stroke(200);
 +
line(width/2, height/2, mouseX, mouseY);
 +
}
 +
 +
 +
<syntaxhighlight>
  
 
==draw==
 
==draw==

Revision as of 15:02, 11 April 2021

Processing

Say Hello

setup()

only run once.

draw()

will continuously repeat the action inside of the {}


1void setup(){
2size(400,400);
3println("Hello!Nan");
4
5}
6void draw(){
7println("welcome to interaction station!");
8}

copy-paste the code above and observe the message from the console.


Now, let's try to draw something

 1void setup(){
 2size(400,400);
 3//println("hello!Nan");
 4background(30,50,80);
 5
 6}
 7void draw(){
 8//println("welcome to nteraction station!");
 9stroke(200);
10line(width/2, height/2, mouseX, mouseY);
11}
12
13
14<syntaxhighlight>
15
16==draw==
17
18==Communication with Arduino==
19
20==draw a image==
21
22==Image sequence:==
23
24<syntaxhighlight lang="java" line='line'>
25
26
27int numImages = 115; // total number of images
28int startNumImage = 1; // first number in sequence
29
30PImage[] images = new PImage[numImages]; // the images will be stored in this list
31
32// setup is executed once, when the program started
33void setup() {
34
35  // size of the window
36  //size(400,400);// use size "(displayWidth, displayHeight)" for fullscreen  
37  size(displayWidth, displayHeight);
38
39  // load the image sequence
40  loadImageSequence();
41}
42
43// draw is repeatedly executed, as fast as possible, or according to frameRate setting
44void draw() {
45  background(0); // draw a black background
46
47  // map x position to an image in the sequence
48  int imgInSeq = (int)map(mouseX, 0, width, 0, numImages - 1); // map center of mass to image in sequence
49
50
51  // draw the image scaled and stretched to the window size
52  image(images[imgInSeq], 0, 0, width, height);
53}
54
55// load the image sequence
56void loadImageSequence() {
57  String filename = "";
58
59  for (int i = 0; i < numImages; i++) {
60    // STUDENT: adjust how the filename is being generated to fit the naming of your files
61    filename = "headtrack/img__" +nf(startNumImage + i, 3) + ".jpg";   
62    images[i] = loadImage(filename);
63  }
64}