Java Components
Volume Number: 12
Issue Number: 10
Column Tag: Java Workshop
Your Own Java Components
Extending the Canvas class
By Andrew Downs
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
This article assumes you know what Java is and how object-oriented programming
works at a conceptual level. You should also be familiar with C syntax, as that is the
basis of Java syntax. This project was developed with Roaster™, the first Macintosh
Java integrated development environment (IDE). Roaster is a stable product, easy to
use, and priced competitively.
Java provides the Canvas class for developers creating their own Graphical User
Interface (GUI) objects. We extend the Canvas class to create a GUI design tool applet.
It shows how to create objects that function primarily as data containers, similar to
structs in C. This is a useful intermediate step for programmers who are learning
object-oriented programming. This article also shows the addition of methods that set
and return the object variable values. We will use the data and methods to draw an
outline of our object, and move it around inside a window. Our project will run as a
Java applet.
Stretching Canvas
When you hear the word canvas, you probably think of what painters paint on. In
this article, we will build upon that metaphor. First, some background information is
in order...
Java’s Abstract Windows Toolkit consists of classes. (See Figure 1.) The
Component class defines a generic GUI primitive; classes like Button, Choice, and
Canvas are subclassed from Component. (Component is an abstract class, which is a
class that is used not to instatiate objects, but to group similar classes together.)
The Component class defines many useful properties (that’s Java for class
variables) for a generic object, and methods (Java for functions) for getting and
setting the values of those properties. These properties include the bounds, color, text,
and numerous others. When you create a class that extends a class, your new class
inherits the properties and methods of the parent class. (The terms “extend” and
“subclass” are synonymous.) You can override (redefine) methods inherited from the
parent class, and you can also define new methods and properties. (The exception to
this inheritance rule are classes and methods declared using the final keyword, which
indicates that the class cannot be inherited or the method overridden. )
Figure 1. Class Hierarchy
Canvas is a subclass of Component (which is itself a subclass of Object, the
topmost Java class), provided for programmers who wish to subclass from Component.
If you want to create and manipulate your own components, do not subclass Component;
extend Canvas instead. A Canvas is a Component that does no default drawing or event
handling on its own. By subclassing Canvas, you will have access to Component’s
properties and methods.
In our project, we extend the Canvas class with two subclasses. First, we create
a backdrop to draw on, called the BackdropCanvas class. Unlike many drawing
surfaces, this backdrop plays an active role in our project; it has its own properties
and methods. Second, we create a generic object class, called the DecafObject class,
which forms the basis of the GUI objects we will create and draw on the backdrop. This
generic object class will also have its own properties and methods.
We need to determine how to construct our applet, and what it will include. The
general steps we will follow are:
• define the frame (window) that will hold our backdrop
• create/initialize an instance of our backdrop
• define the objects to display on our backdrop (both properties and methods)
• create/initialize the objects we will draw on our backdrop
To make things more interesting, we will also create a separate frame that will
function as our “tool palette”. This tool palette will contain buttons that let us control
objects on our backdrop, popup menus that let us set the colors of objects, and a button
to let us gracefully exit our applet.
Figure 2 shows the relationships between the Java source code files that
comprise our project. Each file contains one class definition. Demo.java, the file in the
center of the diagram, is our actual applet definition; the Demo class extends the Applet