Practical Coding in Java

Learn to write and validate your own code

Darren Kessner, PhD

(revised January 9, 2026)

Previous: HighScores

Obstacles

//
// Obstacle.java
//


//
// The Obstacle class is an abstract class, which is like an
// interface, except that some of the functions may be defined, so
// that they can be used by all subclasses.
//
// The functions that must be implemented by subclasses are
// specified with the keyword "abstract".
// 
// The Obstacle class has one abstract function display(), i.e.
// all Obstacle subclasses must implement the display() function.
//
// There is also one non-abstract function isInside() that provides
// an implementation that all subclasses may either use or
// override.
//
// The Obstacle class also has protected variables that may
// be used by subclasses.
//


import processing.core.*;


public abstract class Obstacle
{
    public Obstacle(PApplet p, PVector position, float radius)
    {
        this.p = p;
        this.position = position.copy();
        this.radius = radius;
        this.color = p.color(255);
    }

    // display() must be implemented by all subclasses
    abstract public void display();

    public boolean isInside(float x, float y)
    {
        return p.dist(x, y, position.x, position.y) < radius;
    }

    protected PApplet p;
    protected PVector position;
    protected float radius;
    protected int color;
}
//
// ObstacleCircle.java
//


import processing.core.*;


public class ObstacleCircle extends Obstacle
{
    public ObstacleCircle(PApplet p, PVector position, float radius)
    {
        super(p, position, radius);
        this.color = p.color(0, 255, 0);
    }

    public void display()
    {
        p.fill(this.color);
        p.ellipse(position.x, position.y, radius*2, radius*2);
    }
}
//
// ObstacleRectangle.java
//


import processing.core.*;


public class ObstacleRectangle extends Obstacle
{
    public ObstacleRectangle(PApplet p, PVector position, float radius)
    {
        super(p, position, radius);
        this.color = p.color(0, 0, 255);
    }

    public void display()
    {
        p.fill(this.color);
        p.rectMode(PApplet.CENTER);
        p.rect(position.x, position.y, radius*2, radius*2);
    }
}
//
// ObstacleDemo.java
//


import processing.core.*;
import java.util.*;


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

    public void setup()
    {
        obstacles = new ArrayList<Obstacle>();

        // create some Obstacle objects randomly and add
        // them to the ArrayList

        for (int i=0; i<5; i++)
        {
            PVector position = new PVector(random(100, width-100), 
                                           random(100, height-100));
            float radius = random(20, 50);

            if (random(0, 1) < .5)
                obstacles.add(new ObstacleCircle(this, position, radius));
            else
                obstacles.add(new ObstacleRectangle(this, position, radius));
        }
    }

    public void draw()
    {
        background(0);
        strokeWeight(5);

        for (Obstacle o : obstacles)
        {
            // highlight the selected object, using the
            // isInside() function

            if (o.isInside(mouseX, mouseY))
                stroke(255);
            else
                noStroke();

            // draw the obstacle

            o.display();
        }
    }

    private ArrayList<Obstacle> obstacles;

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

Next: