Creating Custom Objects (Processing)
This activity looks at creating custom reusable objects in Processing. All code should allow you to copy and paste.
Step 1: Making One Object
Let's start with a regular sketch. To for projects that use multiple tabs, it's best to save the sketch even if you are just practicing.
Create the shape
Set up your canvas in setup().
void setup() {
// Create a canvas that's 600 by 600
size(600, 600);
}Let's create a simple button, but with a little complexity in its design. Something like this:

It's just layered squares.
We can do this in draw() so that it is drawn in each frame.
We have a LOT of repetition here - the color, the location of the squares - even the size is based on the size of the first square. Let's turn those things into reusable variables so we can update the whole thing easily.
At the very top, above setup() put the global and public variables:
Then, replace the values in the code with the variables.
Full code so far:
Add some interaction
We can create a custom function that changes the fill color when the user hovers their cursor over the shape, when they select it, and a color to determine when the button is "on."
What we need to add variable-wise at the top of the sketch:
whether the button is on or off (boolean)
state of the button (integer)
colors for each state (unselected/default, hover, selected, and "on")
Now, for the custom function, we need to test to see if the cursor is within the boundaries of our shape. Since the object is drawn from the center, the boundaries are half the size from that point.
After the final curly bracket } of draw(), add the custom functions - one for checking the button boundaries and sets the state and one function that determines the color:
Add one last function, the built-in mouseClicked() function to switch the buttonOn variable, but ONLY when the button is not unselected.
Now, call the checkButtonState() and determineColor() functions before the line referencing the fillColor variable.
Full code so far:
Output so far:

Step 2: Move the Code to a Class
Create a new tab
Near the play button, click the tab with a down arrow and select New Tab.

Give your object type a name.

This will create a tab with the name.

At the top, create a class that is named for your new object type - use a capital letter to begin:
In the first tab, copy the variables that relate to the object and paste them into the new tab. You can remove them from the first tab inside the curly brackets { }.
This means these variables only exist in this object.
We will be setting these values in a different way, so remove any initial values you want to customize.
We now need a "constructor" function that will take information from another part of the script - even a user - to determine these variable values instead of having them written out like this.
Just after the variables, but inside/before the final curly bracket }, we use the name of the object followed by parentheses ( ) with parameters in the parameters, separated by commas. Each parameter should have its variable type before its name and, since these are temporary variables, I usually put "temp" in the variable names.
Inside the Button() curly brackets { }, set each of the variables to the temp variables/parameters.
Now, we need a custom function to display the object. Put this inside/before the final curly bracket of the class.
Copy the code in draw() in the first tab and paste it in the display() function. You can delete it from the first tab.
Copy the functions that follow draw() in the first tab and paste them before the final curly bracket (inside the class) in the second tab (the object tab). The mouseClicked() needs to stay on the main tab to work.
Code so far:
First tab
Second tab
Step 3: Display the Objects in the Main Sketch
If you play it now, nothing but a black canvas will appear.
We should create some variables to hold our buttons at the top.
We can assign/set these buttons in setup() putting arguments in the parentheses that match the parameters in the constructor.
For each, I'll use an unselected color that is a darker version of the color, a hover and selected color that is a lighter tint, and the "on" color the full color.
Let's put code in the draw() function to display some button objects we made.
Code so far:
Output so far (the buttons should have hover and select effects):

Step 4: Get the Clicking Effect to Work
The mouseClicked() only works on the first tab, so we'll have to replace the code with references to our buttons.
Remember to take off the /* and */ lines.
Full code so far:
First tab:
Second tab:
Output:

Last updated
Was this helpful?