Computer Science 252
Problem Solving with Java

Fall 2015, The College of Saint Rose

ClickCounter Demo

A working demo of ClickCounter will appear below. Click inside the applet to interact with it.



ClickCounter BlueJ Project

Click here to download a BlueJ project for ClickCounter.


ClickCounter Source Code

The Java source code for ClickCounter is below. Click on a file name to download it.


ClickCounter.java

import objectdraw.*;
import java.awt.*;

/*
 * Example ClickCounter
 * Program that displays how many times the mouse has been clicked.
 *
 * Jim Teresco, Siena College, CSIS 120, Spring 2011
 * The College of Saint Rose, CSC 252, Fall 2013
 * Based on example from Williams College, CSCI 134
 *
 * $Id: ClickCounter.java 2218 2013-10-18 14:06:39Z terescoj $
 */

public class ClickCounter extends WindowController {

    // Location of the display
    private static final int DISPLAY_X = 150;
    private static final int DISPLAY_Y = 200;

    // the Text object which displays the count
    private Text display;

    // the number of clicks
    private int count;

    // initialize the counter and the text message
    public void begin() {
        count = 0;
        display = new Text("Click count = 0", DISPLAY_X, DISPLAY_Y, canvas);
    }

    // increment the counter and update the text
    public void onMouseClick(Location point) {
        count = count + 1;
        display.setText("Click count = " + count);
    }
}