Computer Science 202
Introduction to Programming

Fall 2013, The College of Saint Rose

Lab 1: Event Counting
Due: 11:59 PM, Monday, September 16, 2013

In this lab, you will gain experience with objectdraw graphical objects and mouse events using numbers.

You may work alone or with a partner on this lab.

Getting Set Up

Follow these steps to get your BlueJ environment set up for this week's lab assignment.

  1. Create a folder in which you will develop your programs for this lab. A good name might be "Lab1". Avoid spaces in this name.
  2. Start BlueJ and choose "New Project" from the "Project" menu. Navigate to your folder for this lab and choose a name for the project. "Lab1" would work again.
  3. Select "New Class..." and enter "Numbers" as the "Class Name" and be sure that "Class Type" is selected as "WindowController Class".

Event Counter Program

Your task here is to write a program that counts and displays the number of times the mouse has entered and exited the canvas window, and the number of times a mouse movement has been detected since the last mouse enter event. The latter should be displayed numerically and as a bar.

A working solution for the program should appear below. Click inside the applet to interact with it.


The program draws a 100x100 black square is drawn at (100,100) with white text overlayed to indicate the number of times that the mouse has entered and exited the canvas. There is black text below the square that shows how many times the mouse has moved (as measured by the number of Objectdraw mouse events have been processed) since the last time the mouse entered the canvas. Finally, there is a blue bar (just a FilledRect) whose width changes to show the number of times the mouse has moved.

Implementation Approach

This program is not huge, but it is large enough that you should not (and likely could not) type it all in, hit "Compile" and have any chance of it working. Moreover, it will be difficult to figure out what went wrong. For this, and pretty much every program you write this semester (and beyond), you will want to implement and test incrementally. If you add more than one or two new bits of functionality to your program, stop typing, make sure it compiles, and if it does, make sure it does what you expect. This means you are unlikely to introduce more than one or two problems at a time, and you will be more likely to be able to track down problems when you introduce them (they're almost definitely caused by those few lines of code you just added or modified).

For this program, there are a number of orderings of the tasks you need to complete that would work equally well. Here is one possible approach:

  1. Draw the initial rectangle and labels for the enter/exit counts.
  2. Implement the color change for the rectangle when the mouse enters/exits the canvas.
  3. Implement the counting of enter/exit events.
  4. Add the text and rectangle for the mouse move counting.
  5. Implement the mouse move counting and update the text display.
  6. Make the mouse move count rectangle grow as the count increases.
  7. Reset the mouse move count when the mouse enters the window.

Thinking of this in small pieces will feel much less overwhelming than trying to figure out how to do everything at once. Work step by step and don't move on to the next step until you are satisfied that the current step is complete!

Please also do not fall into this very bad habit that is so common among beginning programmers: treating documentation as a final step. Writing comments in your program is not what you do after it all works and before you print it and turn it in. Writing comments before you write the Java code (or at least as you are doing so) is much more effective. If you can't reason and state clearly in English what you want each section of your program to do, how can you hope to do so in Java? By documenting before and/or during the coding process, you will be thinking more clearly about what you are trying to accomplish (thereby being less likely to make mistakes), you will be more likely to remember what the code was supposed to be doing when you go back and look later, and you won't have that annoying last step of going back and trying to add comments so you won't lose points in the grading.

Changing Widths/Heights of Objects

You can set the width and height of a rectangle or oval graphical object by sending it the setWidth and setHeight messages. For example, if you have constructed a FramedRect called myRect and you wish to set its height to 200:


Think Constants

Use constants (private static final CONSTANT_NAME) for all the relevant location and size information. This makes it easier to change the layout and also makes your program much, much easier to read (presuming you give constants good names). Constant names are by convention written with all capital letters and underscores, e.g. THIS_IS_A_CONSTANT.

      private static final int RECT_SIZE = 100;

You may declare Location constants as well by initializing the constant variable with the results of a constructor:

      private static final Location RECT_LOCN = new Location(100,100);

However, you cannot initialize constants whose definition uses canvas (e.g., no FramedRect constants), since Java does not yet know about the canvas when it is initializing the constants.

The widths and heights of wash baskets and the item to be sorted, coordinates of the upper left corner of each of these, etc., are all good candidates for constants.

Note that the constructors for our graphics objects can take a Location object to specify its upper left corner instead of separate numbers for x and y, in cases where that is more convenient, e.g.:

  new FilledRect(RECT_LOCN, WIDTH, HEIGHT, canvas);

Bottom line, there should rarely be a "magic number" that represents a position or size of an object written in the body of one of your methods. Define named constants for all such numbers at the top of your class.

Submitting Your Work

Before 11:59 PM, Monday, September 16, 2013, submit your Java program for grading. There are three things you need to do to complete the submission: (i) upload a copy of your Java program (the .java file only) using Submission Box under assignment "Numbers", (ii) print a copy of your program and hand it to your instructor, and (iii) demonstrate the execution of your program for your instructor (2-day grace period for demos).

Don't forget to check your programs for compliance with the Style Guide for CSC 202 Programs before you submit!


This assignment is worth 35 points, which are distributed as follows:

> FeatureValueScore
Initial layout of objects 5
Enter/exit counts updated 5
Color update on exit/enter 3
Update move count number 5
Update move count "bar" 3
Reset count on enter 3
Appropriate comments 5
Good variable names 3
Good use of constants 2
Appropriate formatting 1
Total 35