Java Reference
In-Depth Information
The Java Graphics Application
Java graphics applications must provide processing for handling events
suchastheuserclosingtheframe.Ifthislogicisnotprovidedbyyourpro-
gram, then the window can only be closed by system-level commands,
which is both inconvenient and awkward.
To create a frame that can be closed by means of the control button re-
quires implementing event-handlers and using event-handling code. With
the previous version of the AWT this task posed no great problems, since
theeventhandlingmodelwassimpleandintuitive.But,thismodel,which
was based on inheritance, had serious limitations. This led to the adop-
tion of a more complex delegation-based model. Although code that uses
the old model still works in Java 1.2, Java has declared that this support
will end in the near future.
In order to make the frames “closeable” we use a class called ActiveFrame.
Since ActiveFrame extends Frame, all the methods of the Frame class remain
available to our code.
An ActiveFrame object has the following properties:
The frame's default size is 300 by 200 pixels
The default location is the top left screen corner
The title bar displays the name of the class
The ActiveFrame class
Creating code that can process windows events requires creating a
WindowListenerobject.ThisisdonebymeansoftheWindowAdapterclass
whichispartofthejava.langpackage.ThewindowClosing()methodofthe
WindowAdapter class takes a WindowEvent object as a parameter. This
method is invoked when the application window is being closed thus pro-
viding implementation of a “user-closeable” window. The ActiveFrame
class, listed below, extends the Java Frame class and provides the neces-
sarycodeforconventionalclosingoftheprogramwindow.ActiveFrameis
defined as follows:
import java.awt.*;
import java.awt.event.*;
public class ActiveFrame extends Frame
{
//*************************
// Constructors
//*************************
Search WWH ::




Custom Search