CSE 341 -- Programming Languages

Autumn 2000

Department of Computer Science and Engineering, University of Washington

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

Assignment J2

Version 1.2 of October 30.  Subject to change.

Java In-depth

Due dates and time: A milestone report is due on Wednesday, November 1, 2000 at 5:30. The full assignment is due on Monday, November 13, 2000 at 5:30.  When your applet is ready submit it using this form.

Title: Java -- Going more in-depth with painting

Purposes:  To deepen your understanding of object-oriented design, to gain more experience working with the AWT in Java, to explore more painting and image processing techniques, and to gain experience in user interface design.

Instructions:  Using the applet you wrote for assignment J1 as a base, write a more fully-featured paint program.  The program should support the following types of brushes: square, round, diagonal line, and spraypaint.  Users should be able to set the size and transparency of each brush.  The user should be able to change the current painting color by bringing up a color picker dialog box (at a minimum, a dialog box containing three text fields for the user to enter new red, green, and blue values).  Additionally, the spraypaint brush should be time-based, so that the longer the user holds downs the mouse button in one location, the more dense the spraying becomes.  As in J1, the user should be able to clear the entire screen.  In addition, the program should support an eraser tool and a smudge tool (note that if you are careful with your design, these can also be subclasses of PaintBrush).  Along with the Invert function from assignment J1, the program should also include "Lighten" and "Darken" functions which lighten or darken the entire image by a constant amount.  The program should support at least one level of Undo.  The design of the interface is up to you.  Some suggestions include creating a graphical toolbar for the user to select at least some of the tools from. The interface of the sample executable is not good. Please try to design something better. If you just duplicate the interface given in the sample executable, no points will be deducted. However, up to 10 points of extra credit will be given for user interface design.

By the way, you are welcome to base this assignment on your implementation from assignment J1, or you may use the sample implementation of J1 as a base, if you prefer. In creating the sample executable for this assignment, I am basing it directly on the sample executable for J1. You are welcome to do either and no points will be deducted for starting with the sample J1 code -- just please make a note of it in your documentation.

Note that there is an updated version of PaintCanvas.java available, which you should download and use in place of the old version. It fixes some a number of problems and adds some new functionality. Here is a the online documentation for it. One thing to note is that there is a new method called setApplet, which should be called once in your applet's init method. It is a static class method, so you only have to call it once for the whole PaintCanvas class, rather than having to call it for each instance of PaintCanvas you create. Any questions about this new version, don't hesitate to send email.

Here are some hints about implementing some of these features:

To paint a brush using transparency, first create a new PaintCanvas and set its size to the size of the brush.  Then call the brush's drawing routine using this new PaintCanvas and just have the brush paint itself in black.  Then scan the pixels of this temporary PaintCanvas.  Where you encounter a black pixel, set the corresponding pixel on the actual PaintCanvas to be a weighted average between the current painting color and the color already there (where the weighting depends on the degree of transparency).

In implementing the spraypaint tool, you will want to use a thread, to allow you to continue increasing the paint density between mouse drag events.  You can do this by having your applet implement the Runnable interface.  The Runnable interface defines one method, called run, which you can use to increase the paint density.  You will want to create a new Thread object in your applet to handle this.  See the examples below for a simple applet which demonstrates the use of a thread in a Runnable applet.

To implement a smudge tool, when the user clicks and drags with the smudge tool selected, examine the pixels within the tool area and the pixels immediately surrounding it and average them together.  You can get different results depending on how you do this averaging, so experiment with it until you get an effect you like.

Here are some sample applets which you may find useful to look at:
Sample solution to assignment J1
Applet which demonstrates loading and displaying an Image
Applet which demonstrates the use of dialog boxes
Simple applet demonstrating the use a Thread and the Runnable interface
Code for an ImageButton component and an applet demonstrating its use

Optional enhancements:  After you have completed the required functionality as described above, you may wish to add more features to your program.  Successful implementation of additional features will be given a small amount of extra credit.  Here are some suggestions for additional features: (1) support basic "edit menu" functions (cut, copy, paste, clear) using a "marquee" selection tool as can be found in most commercial paint programs, (2) add a flood fill tool, (3) add additional "filter" operations, such as changing the image contrast, flipping the image around some axis, distorting the image, etc. (see Jeremy for hints about this), (4) add additional brush types, (5) add a text entry tool, (6) using the same ideas as those employed in the creation of brush transparency, add a feature to allow any brush to be anti-aliased as it draws (7) add load and save functionality.

Here is the sample executable

Programming style:  Comment your method definitions clearly, using the style given in the first sample Java program.