Although that Monster sitting there in one spot is a work of artistic genius, it might be nice if the Monster actually did something. Let’s make the Monster follow the mouse as we move the mouse around the window. To simulate movement, we need to exploit the fact that computers actually redraw the screen repeatedly. The screen I am looking at right now is being redrawn approximately 60 times per second. Processing allows us to draw something new in the window every time the computer redraws the screen. To simulate movement, we can tell Processing to draw the Monster at different positions every time the window is redrawn.
So far, we have used methods like ellipse and size that are given to us as part of the main Processing object. But there are a couple of methods associated with the main Processing object that we get to define ourselves. In particular, we can define the setup and draw methods. The setup method is called when Processing first sets up the window. It is only called once. Anything that we want to have happen once should be put into setup. For example, we only want to call the size method once, when the window is first created, so the call to size should be in setup. The draw method gets called repeatedly, every time Processing redraws the window. In draw, we can tell the main Processing object to draw the Monster at different positions in order to simulate movement.
Figure 2.1 shows a simple program that draws an ellipse in the window using the setup and draw methods. You can run the code by clicking Start in Figure 2.2.
void setup() { size(800,400); } // end setup() void draw() { ellipse(100,20,70,50); } // end draw()
Writing methods is what we are going to spend most of this course doing, so let’s look at this code more carefully to make sure we understand it. In the line void draw(){, there are a couple of things to note. First, the name of the method is draw. When you have defined the method draw in the main Processing object, Processing will call this method every time the window is redrawn.
Recall that when we called the method ellipse, we passed information into that method inside the parentheses. We referred to this information as the arguments passed in. The empty parens after the word draw indicate that when draw is called, it does not expect to be given any arguments at all.
Also recall that some methods, like random, return information to us. By writing void at the beginning of the definition of draw, we are telling Java that this method has no information to return.
Finally, the opening brace marks the beginning of the method. The closing brace two lines down indicates the end of the method. Whenever we need to delineate a section of code, which is referred to as a block of code, we will use an opening and closing brace. This block of code is called the body of the method.
The // end draw() after the closing brace is a little note to myself that the closing brace marks the end of the draw method. That part of the line is considered a comment and is ignored by Java because of the //. Any time we put // on a line, Java will ignore the rest of the line. I put comments on the closing braces because braces are small and hard to spot, making them a silly way to start and end blocks of code.
Inside the block of code that makes up the draw method, there is one line, which is referred to as a statement. This statement tells the main Processing object to do something. In particular, it calls the ellipse method with arguments 100, 200, 70, and 50. The ellipse method is a method that is part of the main Processing object; it draws an ellipse with position and size determined by the values of the arguments. The call to the ellipse method is a statement that we want to executed every time the window is redrawn, so it needs to be in the draw method. On the other hand, the call to the size method is a statement that we want to execute once, when the window is first created, so it belongs in the block of code that makes up the body of the setup method.
As with any new subject, there is a lot of terminology to learn. In a couple of weeks, you will forget what it was like not to know all of these terms.