CSE 341 -- Programming Languages

Autumn 2000

Department of Computer Science and Engineering, University of Washington

Steve Tanimoto (instructor) and Jeremy Baer (teaching assistant).

Assignment J1

Version 1 of October 16.  Subject to change.

Java Warmup

Due date and time: Wednesday, October 25, 2000 at 5:30 PM (Submit your assignment electronically by filling out an online submission form). 

 

Title: Java -- A Warmup with Objects and Painting

Purposes:  To learn how to put classes, objects and inheritance  to work; to get started with the Abstract Windowing Toolkit (AWT) and the Java 1.1 event model; to get some experience with building paint program style applications; and to begin to build some infrastructure that we can use for the in-depth Java assignment as well as the miniproject.

Instructions:  Examine the Sample Java Applet containing a pair of buttons.  Then write a Java applet which implements a simple painting program.  The applet should provide a painting area, and five control buttons (see sample executable).  When the user clicks and drags in the painting area, the applet should lay down paint in the current color with the current brush style.  The buttons should include the following: "Square Brush" causes the user's subsequent painting to be done using a square brush; "Round Brush" causes the user's subsequent painting to be done using a round brush; "Change Color" changes the painting color to a randomly chosen color; "Clear" clears the painting area to all white; and "Invert" inverts the pixels in the painting area (i.e., performs a photo-negative pixel transformation).  Try out the sample executable to see the required functionality.

Here are some hints for implementing this:  First off, performing pixel manipulations in Java (particularly finding out what the current R, G, and B values are for a given pixel) is somewhat complicated.  To avoid the need to plunge deeply into the java.awt.image package, we are providing you with a file, PaintCanvas.java, which implements a subclass of the built-in Canvas class and provides a full set of drawing and pixel getting and setting routines.  Download this file, and use it to create your painting area.  All the methods you should need are commented.  Many of them just duplicate the functionality provided by many of the methods in the Graphics class, but there are some others which you will not find in a Graphics object, such as setPixel, paintPixel, getPixelColor, etc.  You can either subclass PaintCanvas to provide mouse event handling, or you can simply use an instance of it as-is and have your applet class process its mouse events.  The sample executable makes use of the latter strategy.  Use a BorderLayout layout manager for your applet, putting your PaintCanvas in the center position, and your panel of buttons in the north or south position.

Your solution should include three classes to implement the paint brushes.  You should have an abstract class called PaintBrush, and two concrete subclasses called SquareBrush and RoundBrush.  For now, PaintBrush isn't going to have a lot in it, so if you feel like there's not a whole lot to code there, that's OK.  For this tiny applet, we wouldn't really need this kind of inheritance hierarchy, but we will make more use of it later on.

Here is an executable for a sample solution.

Programming style:  Comment your function definitions clearly, using the style given in the sample program.