Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised October 21, 2025)

Previous: Ball

Bounce

//
// Bounce.java
//


//
// Notes:
//
// 1) All of the actual Processing drawing code for each Ball is in
// the display() function of the Ball class above.
//
// 2) When you construct a new Ball, you must pass a reference to
// the PApplet class.  In the main program, Bounce extends PApplet
// (i.e. Bounce "is a" PApplet).  Inside a Bounce object, "this"
// refers to ourself.  In other words, "this" is the PApplet
// reference that we want to pass to our Ball objects.
//



import processing.core.*;
import java.util.*;   // for ArrayList


public class Bounce extends PApplet
{
    public void settings()
    {
        size(400, 400);
    }

    public void setup()
    {
        balls = new ArrayList<Ball>();
        balls.add(new Ball(this)); // 2: PApplet reference "this"
    }

    public void draw()
    {
        background(0);
        for (Ball b : balls)
            b.display(); // 1: draw the ball
    }

    public void keyPressed()
    {
        balls.add(new Ball(this)); // 2: PApplet reference "this"
    }

    private ArrayList<Ball> balls;

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

Next: