|
Chapter: Beyond
Static Sketches and on to Active mode
Beyond Static Sketches and on to Active mode
Static sketches are mainly used for testing basic programs that do not
incorporate interactivity or are used to output and image file or
sequence such as a .png or .pdf. The majority of programs produced with
Processing are made using active mode. Active mode requires two
fundamental additions to a sketch, the setup() and draw() functions.
setup() and draw() provide a new structure to sketches and allow for
the initialization of a code block within setup() and the repetition of
the code block within the draw() structure. Code blocks within the
setup() and draw() functions are always cradled between braces, so they
are easy to identify. The structure of an active mode sketch generally
follows the pattern identified in the example below:
void setup(){
code...// This code block will run only once,
… // at the start of
the sketch.
…
}
void draw(){
code...// This code block with run repeatedly,
… // throughout
the duration of the sketch.
…
}
A typical
active mode sketch structure with the setup() and draw() code blocks
highlighted
Up until now our sketches have not been interactive or included
animation. The use of the active mode structure, by means of the
setup() and draw() functions, allows us to truly access the powerful
features that programming offers to developers and extend our sketches
with interactivity, animation, logic and a host of other features that
we will explore.
First, we'll start by having a look at how setup() and draw() change
our sketches by making them more dynamic.
Methods, functions
and their contexts
Before we start using the setup() and draw() functions let's first
examine the context in which they exist within Processing. setup() and
draw() are also sometimes, albeit less accurately, referred to as
methods. A method is simply another way of saying function, but more
specifically it refers to the function of a class. As you might
remember, classes form the building blocks of object oriented
programming and can be packaged into groups consisting of many classes
which we refer to as Libraries. From Libraries we can access classes,
from which we can instantiate objects that we use in our programs. But
before using a particular library we would first need to tell
Processing to import that library for usage within our sketch, by using
the import keyword. This differs from using a class built into
Processing's API where we do not need to use the import keyword, for
example we could instantiate an object from the PImage class and call
it img like in our Hello World program, without using the import
keyword, like in the following code fragment:
PImage img;
One of the methods, that works, in a sense, just like a typical
function would of our main program, of img could be to resize the width
and height of the image that we have associated with the img object for
example:
img.resize(50,100);
This statement would resize the width and height of the image
associated with the img object to 50 and 100 respectively. In this
sense the resize() keyword used in relation to the img object would be
a method of the img object. Just like a function, a method's definition
can also exist independently of the main program and in this case it
does, as the resize method is defined within the PImage class which as
you are aware is not defined within our sketch but exits as a part of
Processing's API. So as you can see the term method is very similar to
that of function in terms of the purpose they both serve. However, it
is important that the terms are not used interchangeably as they can
lead to ambiguity and confusion.
As I have mentioned before Processing is a language on it's own and not
just a library consisting of classes for Java. If it was, referring to
setup() and draw() as methods would be perfectly acceptable because
they would be methods of a class defined within the Processing library
for Java. Although Processing can be used in this context we are not
using Processing as a library for Java we are using Processing as a
language that is independent of Java until implementation, this is
relevant because although Processing relies on Java currently for it's
implementation theoretically there is nothing stopping Processing from
being implemented within other environments. Within this context
Processing is a language and not exclusively a library for Java. What
that means is that, setup() and draw() within this definition cannot
accurately be discribed as methods of a class that has been
instantiated but rather as functions of a programing language. This
concept can be extended even further because setup() and draw() are
such fundamentally important functions within Processing, that they
could be placed in a category of their own and described as structural
defining functions, because they alter the structure of a program so
dramatically.
|
|