Homework 6 - AWT and Standalone Applications: A paint program
[Homepage] |
[General Lab Info] |
[TA's & Tutors] |
[FAQ's] |
[Homework] |
[Excellent Programs] |
[Exams]
Reading: Chapter 9.
Due: At 8:00am, February 23, 1999 submit is turned off
automatically. Work turned in after that time will not be accepted
for grading.
Program Description
For this assignment you will create a stand-alone application (not an
Applet) that uses components from AWT. You are free to design your own
interface to your program but it must have certain capabilities listed
below.
The program will be a painting/drawing program. For full credit it
must contain the following features:
- Allow drawing as was done in SimpleDraw (chapter 6).
The drawing must be done on an offscreen image so that changing
windows does not erase what was drawn.
- Allow changing of the color.
- Allow changing of the size of the drawing pen.
- Allow selective erasing (essentially painting in white).
- Allow for the drawing of arbitrary straight lines. Once placed the line is
just paint, it does not remain as an object that can be deleted in its
entirety. Of course the line can be erased by painting over it. The user
must specify the line using the mouse (e.g. two clicks, one at each end
of the line). It is not OK to prompt the user for
the coordinates of the endpoints of the line.
There are lots of opportunities for a 6 or 7 on this assignment. Here are
two possibilities of things you might consider adding. These can be done
with material covered in Chapter 9.
- Allow a "drawing" mode and a "painting" mode. In "drawing" mode
the objects retain their identity and can be modified (ala hw5). In this
case, the paint() method for your canvas should first draw the image containing
the "painting" mode picture, and then draw the list of objects on top of
that using a list of objects.
- Allow for painting or drawing of additional shapes beyond just lines (e.g.
rectangles, ovals, polygons).
Purpose
The purpose of this assignment is to become familiar with creating a
standalone application (not an Applet) that contains a GUI built from
AWT components. Specifically you will need to at the least use the AWT
classes, Frame, Button, Graphics, Canvas, Color and Image.
You will need to gain some familiarity with at least one layout manager.
You will
also gain experience handling both button events and mouse events. The
Button events, handled by an ActionEventListener will be used to
select different drawing options such as changing the color. The mouse
events will be used to do the drawing.
A MouseMotionListener will be used to paint, as with a paint brush. A
MouseListener will be used to detect clicks used to identify the
endpoints of a line, the corners of a rectangle, or the starting point
for placing text.
In addition to learning about specific AWT classes, you will also gain
some experience with using inheritance. You will need to extend Canvas
and you will need to implement some event listener interfaces.
Finally, adventuresome students will have an opportunity to apply
the knowledge they gained in program 5 by creating their own Shape
objects that know how to paint themselves, given an instance of the
class Graphics.
How to proceed
As a starting point I
suggest you try to convert the SimpleDraw applet from chapter 6 into a
stand-alone application. You should extend Canvas to create a
component on which you do the drawing, as was done with BarChart or
many of the other examples in chapter 9. In SimpleDraw, we called
createImage() to get the offscreen image. You should do the same thing
for your Canvas. Let me caution you to NOT call createImage() inside
of the constructor for your class that extends Canvas. You will need
to have a separate initializer that gets called after the Canvas is
constructed. If you call createImage() inside of the constructor it
will probably just return null. For reasons I don't want to explain
here, you should call the init() method for your Canvas object AFTER
calling pack() and show() on the Frame that contains the Canvas.
[Homepage] |
[General Lab Info] |
[TA's & Tutors] |
[FAQ's] |
[Homework] |
[Excellent Programs] |
[Exams]
This page maintained by Kevin Klenk.
Email regarding this site.