Join Barton Poulson for an in-depth discussion in this video Building code blocks, part of Interactive Data Visualization with Processing.
One of the tools you have for arranging your code and Processing is putting it into blocks. Now most dynamic drawings actually include two blocks every time and that is what's called the setup and the draw and you've seen me do those a number of times. In this sketch I am going to show you a few other kinds of blocks that you can use and how they work in Processing. I'll begin by putting a comment up here at the top with my sketch name. Then I am going to insert a color palette. Now I've decided to save a little bit a time and I've included a text file that has all the color palettes on it and I am going to be using this one right here, styleyou.
I want to copy that and paste it right here into my sketch. Then I am going to define two variables that I'll be using in this the sketch. One is an incrementing variable and another one is of X coordinate variable and now that I've created these two global variables, I can go down to the first block of code that we will be using. Traditionally, that is void setup. And let me explain what's going on with this particular block. The void is the return type for the function, setup is the name of the function, and every function has a return type that is if it were to calculate a floating-point number, it would say float setup.
If it would were to create a Boolean value, it'd say Boolean setup. However the setup actually doesn't return a value at all, but you have to have something. You have to specify return type. So you say to void. It doesn't return anything. That explains a curious term and also the empty parentheses (), because every function needs to have a place where you could at least potentially put arguments. The setup doesn't have any arguments, but you do need the parentheses () there because it's a function. Instead, all the information we're going to do goes in the curly brackets that indicate a block of code in this case.
So what I am going to do is I am going to start inserting some code in here. This is where I put the size. This is where I can put the anti-aliasing to where I can put the stroke. Actually, I think for the stroke I'll use the palette and I'll use the 4th one and I can put the strokeWeight. In this case, I will use 5 pixels. So that completes a block of code. Now I didn't create any variables in there. If I had, they would have been local variables that applied only to that block of code or their values would've applied only to that.
Next I am going to go to the second standard block of code in most Processing sketches and that's the draw. So I go void draw, and again it has void because it's a function, but it doesn't return a value. So the void means there's no value to return. The open and close parentheses (), that's the space that's mandatory to allow parameters to be input, but we don't have a parameters for this, but we have to have that there as a marker. Then I do the curly brackets, the open, and then I am going to bump down to closing curly brackets. Then inside there I can put information about the background color and what I am going to do here for the background, I am going to do palette put, the 0 color.
Then I am going to fill in. Then I'm going to change to the letter i and that refers to the global variable that are defined up above. And the reason I put it up there, because I am going to be using that variable in more than one block of code. So it has to be outside of the code where things can get to it. Then I am going to draw a small rectangle. I am going to start it at x which I defined as 0 earlier up above, 75 pixels down, and then I'll make it 50 pixels wide and 50 pixels tall. Now if I'd simply go like this, I can hit draw, you can see I've got a pretty shocking rectangle there, but that's the basic idea.
Now what I want to show you is that Processing entry actually allows for several other blocks of code. I'm going to use two in this particular one. The one I'm going to use is what's called keyPressed. Now with keyPressed and with a similar one mousePressed, those can be used as functions within the draw block or they could be put as separate functions out here. They work little differently depending on this, too. But I am going to have this one out here. Again it's void, because it doesn't really return a dataType. I am going to put the curly brackets and what I can say is when I press the key, I want x, the location for the rectangle, to be incremented 3 pixels.
So each time I press the key x will be incremented 3 pixels. So I can press that press run and now I can just press a key, I'm pressing the Spacebar, and it's moving over. If I hold it, it moves faster. But I am going to add just one another block of code to show what can be done. I am going to void mousePressed, and again it doesn't have a dataReturnType, so I put void it doesn't have any arguments there. So I have the empty parentheses () and then I put the curly brackets where I am going to put my code.
What I am going to do is each time I press the mouse here, I am going to take the variable i, which I earlier used for the palette and I am going to have i take on a random value. I am into random (1, 5) from the palette. Now remember that the way random works is that the bottom limit is an inclusive so 1.00 is an option, but the top limit is exclusive. So it never goes to 5, it goes to 4.99 and when you take the random variable and you cast it into an integer, it just chops off the digits.
So you're going to get 1, 2, 3, or 4 from this. Now I can run this. I'll save it and I'll run it. If I hold down the Space key, it'll move over and if I hit the mouse, the color of the box changes. And it's a silly kind of interaction, but it lets you know that these different blocks of code function in a modular sense, and especially if you want to be able to use the same variable in different codes, and it needs to be declared as a global variable so more than one block can get at it.
And that's how we can organize some of the code in these sketches.
- Exploring the need for creative data visualization
- Drawing basic lines and shapes
- Introducing variables, strings, and arrays
- Modifying drawing attributes such as color
- Making drawings more dynamic with animation loops and spirals
- Creating keyboard- and mouse-based interactions
- Adding images, video, and sound
- Reading in text or XML data
- Creating plots and charts
- Publishing and sharing your work