Lab 2

Crosshairs: an introduction to Java, DrJava, and the objectdraw library

Open your CFS directory, which is known as m:.

Create the directory m:\lab2for this assignment. (Right click in the explorer window and select New > Folder. Then right-click the new folder and select Rename to change the name to lab2.) In the future, create a new directory for each assignment.in this way.

Save Crosshairs.java to the assignment directory. (Right click the preceding link and select Save target as ...) This is a "starter file" the provides the skeleton of your first program to help you get started.

Do all the steps in the DrJava tutorial. Come back here when you are done.

Finish modifying your Crosshairs.java file so that it behaves like the following applet:

Of course your program will not be an applet, but rather a class that, when instantiated, pops up a frame that contains an area that behaves like the applet. (If you would like to convert any objectdraw program into an applet, see the instructions on the course system notes page. You won't need to do so for this course, but you might want to use an objectdraw program in some web page of your own.)

This program has the following characteristics (try them out using the applet above):

  1. It draws on an area with dimensions of 392 by 338 (width by height). There is a green border of width 5 just inside the drawing area.
  2. When the mouse is pressed or dragged in the drawing area, horizontal and vertical crosshairs appear going the width and height of the drawing area. The crosshairs intersect at the mouse point. Assume that the mouse is not pressed when the program starts.
  3. When the mouse is button is raised, a red dot is drawn with diameter 20, centered on the mouse point. These dots may obscure (be on top of) the border.
  4. When the mouse is pressed or dragged (and only then), its coordinates are displayed in the upper-left corner, just inside the border, in the form x=number y=number. It is ok for the numbers to end in .0, even though they are always integers. You will learn later how to avoid this.
  5. When the mouse exits the drawing area, everything is erased, and when it enters again, the border is redrawn.
  6. If you drag the mouse outside of the applet, you may get different behavior with different browsers, and some of them can be pretty strange! In all assignments, do not worry about what your program does in this case.

It is strongly recommended that you program these five features one at a time, trying to get each working before going on to the next. Of course if you are really stuck on one of them and can't get help right away, then it's ok to go on.

Read the first five items in the course style rules and guidelines. Pay special attention to the rules. You aren't expected to follow all the guidelines perfectly in the first assignment, but start getting used to them, for they will be increasingly important in future assignments.

Hints follow, organized by the five features above:

  1. Magic numbers, such as 392 and 338, should always be defined in constants. Be sure to use meaningful names for all constants and variables. You will need to use the FilledRect class and setColor method.
  2. When the program begins, create two instance of the Line class, one for the vertical crosshair and one for the horizontal one, and store them in instance variables. Since we have no location for the mouse when the program starts, these lines must be hidden at first. Also, they should be sent to the back so they do not show up over the border.

    Since everything that is done when the mouse is dragged also needs to be done when the mouse is pressed, you can simplify the program by using the method call onMouseDrag(point) in the method call in the onMousePress method. (Although all the method calls in the first two chapters are to library methods, you can of course call one of your own methods any time you like.) The onMousePress method also needs to show the two lines.
  3. There is no class just for drawing circles, but a circle is a special oval with equal height and width.
  4. You can set the text of a text object as often as you like.
  5. The clear() message can be sent to the canvas. Can you see another use for calling a method you have already defined?

If you have time

Each time a dot is drawn, use a random number generator to generate a random color for the dot (rather than using red all the time). See the documentation on the classes java.awt.Color and objectdraw.RandomIntGenerator.