Students who complete this lab will demonstrate that they can:
Processing supports the digital animation of a variety of geometric figures.
This program models the semi-random movement of an object in 2D space using Brownian motion . You can animate this behavior by setting a fixed starting point and then repeatedly varying the x and the y coordinates by a small, random amount. You can run the animation by clicking on the animation image to the right. You must have the Java runtime environment (JRE) installed for this to work and you may need to enter the course materials password. |
Write an animation program named
Scribbler
whose output is like the one shown above (save all of your code for
this lab in a directory called
lab04a
). Start your scribble in the middle of the output pane and move it a
short random distance away from its current position on each
successive frame. Ensure that it never leaves the visible part of the
canvas shown in the output pane.
To implement the animation, you can follow an algorithm such as this one:
newX
and newY
equal to new
x-y coordinate values that are a small random distance away from
where x
and y
were in the previous frame
(Note that you can invoke the predefined
random
method
to compute a small random number around 0 and the constrain
method
to ensure that the new coordinates are on the visible output
pane);
x
,
y
) to the new coordinates (newX
, newY
);
x
, y
)
equal to the new coordinates (newX
, newY
)
- this ensures that the new coordinate values will be saved and
used as the starting point in the next iteration.
Note that in this algorithm the scope of the coordinates
(x, y)
is global to the whole program while the scope of
(newX, newY)
is local to the
draw()
method. When you have your program running, take a moment to ask
yourself these questions:
x
and y
local to draw()
?
Would the animation still run?
newX
and newY
global? Would it be good idea to do it that way?
Include brief answers to these questions in your program documentation.
Processing also supports mouse-based interaction between the user and the program.
Save a copy of your Scribbler from above as
Save this version of your program so that you can turn it in later. |
Although Processing requires that the
draw()
method be defined for all animation and interaction programs, it
doesn’t require that that method contain code. It could be
defined to do nothing:
void draw() { }
This can be used in programs that respond to user-initiated events only and not to a pre-defined frame rate.
Create an interactive program called
You can implement this by using an empty
Use this version of your program in the next exercise. |
Processing applications can also respond to key-pressing events.
Modify your program from the previous exercise so that when the user
presses any key on the keyboard, the program dumps the current image
to a JPG image file. You can implement this by adding a
keyPressed()
method that calls the save()
method. Save the image
using a hard-coded filename each time. Be sure that you can find the
image file that Processing dumps. Hint: Where does Processing like to
put auxiliary files?
Save this final version of your
Tracer
program so that you can turn it in later.
What do the following statements do (if anything)? You will want to evaluate each statement in turn. If any are invalid, comment them out, and note the error.
Explain your results.char char1 = 'H'; char friendlyChar = 'Hi'; char emptyChar = ''; String string1 = char1; String emptyString = "";
What is the return value of the following expressions (if any)?
Explain your results."All your base" + "are belong to us." "Somebody" + ' ' + "set us up the bomb." "A.D. " + 2101
Create a program called CountAnimation that
produces the output shown here, with one line appearing each frame.
You can do this as follows:
|
Submit all the code and supporting files for the exercises in this lab. We will grade this exercise according to the following criteria: