An Animated Introduction To Computer Science
With Processing And Java

Thomas C. O'Connell


DRAFT 2024


Copyright © 2016, 2024 Thomas C. O'Connell, All Rights Reserved

2.2 Parameters

We need a way to tell the Monsters to draw themselves at different locations. When we call display, we want to tell the Monster something more than just display; we want to tell the Monster to display at a particular location. Right now, the Monsters display wherever the mouse is. No matter how many Monsters we create, we will only see one because they are all drawn at the same spot in the window.

We can redefine the display method inside Monster to take parameters. In this case, we will use the parameter to indicate the center of the Monster. This will replace the mouse position in our previous incarnation of the Monster class. To tell Java that a method requires parameters, you list the parameters preceded by their types. In Figure 3.1, the display method is defined to take two parameters, which we refer to as centerX and centerY. Since we will be passing these values into ellipse and ellipse expects each of its parameters to be a float, we will require each of our parameters to be a float as well.1 The main Processing object calls the display method as before, but now includes parameters to indicate to each Monster object the point at which the Monster object should be displayed. By specifying different parameters for each Monster object, we display the Monster objects at different points in the window.

image not found
image not found


Figure 3.1: The Monster class with parameters passed in to display Monsters in different positions.

Your browser does not support the canvas tag.



Figure 3.2: Two Monster objects are now visible.

Back To Top