This follows Using classes with Processing in Eclipse.

This demonstration shows how to handle mouse presses when using Processing in Eclipse with more than 1 class. This demonstration also shows how to handle more than 2 classes.

Eye class

package moreClasses;

import processing.core.PApplet;

public class Eye
{
    private static final int RADIUS = 25;
    
    private PApplet parent;
    private int centerX, centerY;
    private boolean open;
    
    public Eye(PApplet p, int cX, int cY)
    {
        parent = p;
        centerX = cX;
        centerY = cY;
        open = true;
    }
    
    public void drawSelf()
    {
        if(open)
            parent.fill(255);
        else
            parent.fill(0);
        
        parent.ellipse(centerX, centerY, RADIUS * 2, RADIUS * 2);
    }
    
    public void close()
    {
        open = false;
    }
    
    public boolean isInside(int x, int y)
    {
        return PApplet.dist(centerX, centerY, x, y) <= RADIUS;
    }
}

The Eye class works similiarly to the Face class from the previous example.

If the eye is open, it is drawn as a white circle. If closed, it is drawn as a black circle. The eye starts open and can be closed by running the close method.

The isInside method returns true if the position represented by its parameters is within the eye.

Face class

package moreClasses;

import processing.core.PApplet;

public class Face
{
    private PApplet parent;
    private int centerX, centerY;
    private Eye leftEye, rightEye;

    public Face(PApplet p, int cX, int cY)
    {
        parent = p;
        centerX = cX;
        centerY = cY;
        leftEye = new Eye(parent, centerX - 100, centerY - 100);
        rightEye = new Eye(parent, centerX + 100, centerY - 100);
    }

    public void drawSelf()
    {
        parent.fill(0, 155, 255);
        parent.ellipse(centerX, centerY, 450, 450);

        leftEye.drawSelf();
        rightEye.drawSelf();

        parent.fill(255);
        parent.arc(centerX, centerY + 100, 150, 100, PApplet.radians(0), PApplet.radians(180));
    }
    
    public void handleMousePress()
    {
        if(leftEye.isInside(parent.mouseX, parent.mouseY))
            leftEye.close();
        else if(rightEye.isInside(parent.mouseX, parent.mouseY))
            rightEye.close();
    }
}

The Face class constructs 2 Eye objects. parent is passed as the first argument when constructing each Eye object. This allows the Eye class to access Processing methods and variables.

Method handleMousePress is intended to be run whenever the mouse is pressed. The method checks if the current mouse position is inside either eye. If the current mouse position is inside an eye, the method closes that eye.

handleMousePress must be run from the ManyFaces class (which extends PApplet). handleMousePress is not a special name. Although it is possible to name this method mousedPressed, doing so would not make this method run automatically when the mouse is pressed.

ManyFaces class

package moreClasses;

import processing.core.PApplet;

public class ManyFaces extends PApplet
{
    private Face f1, f2, f3;

    public static void main(String[] args)
    {
        PApplet.main("moreClasses.ManyFaces");
    }

    public void settings()
    {
        size(1500, 500);
    }

    public void setup()
    {
        f1 = new Face(this, 300, 585);
        f2 = new Face(this, 750, 250);
        f3 = new Face(this, 1250, 400);
    }

    public void draw()
    {
        f1.drawSelf();
        f2.drawSelf();
        f3.drawSelf();
    }
    
    public void mousePressed()
    {
        f1.handleMousePress();
        f2.handleMousePress();
        f3.handleMousePress();
    }
}

The mousedPressed method in ManyFaces overrides the Processing method. mousePressed is run by Processing each time the mouse is pressed. mousePressed runs the Face method handleMousePress.

All of the Processing methods intended to be overriden by a programmer are overriden in the (one and only) class that extends PApplet. Methods of other classes are then called.

Comments

Comment on More on using classes with Processing