8. Introducing Graphics

8.1 The Abstract Window Toolkit (AWT)

8.2 The anatomy of an AWT GUI

The AWT provides ready-made classes for It also provides a framework for responding to user-generated events such as mouse-click, text input (this will be covered in Section 9). Elements used to create a GUI are part of the inheritance hierarchy shown below.
\epsfig {file=figs/AWT.eps,width=13 cm}
Most GUI elements are sub-classes of the Component class. These sub-classes fall into two groups:
  1. The Container class. This includes the Frame and Dialog sub-classes which are used to hold other components.
  2. The visual component classes. These describe components that you can see or interact with e.g. Button, Checkbox, Label, TextField and TextArea.
Custom drawing of lines, shapes etc is handled a bit differently. We will look at how to do this later in this section.

8.3 Building a GUI step-by-step

We'll create a simple GUI that graphically displays the (inevitable) "hello world" message. The code for HelloWorld.java is in Section 26 T he supplementary material (Section 26) lists some useful visual components and methods for creating Gui's. There are also several layout managers you can use to help layout the components in a container.
% latex2html id marker 2520
\fbox {\fbox{
\parbox{10cm} { \medskip {\underline{\...
...he full set of AWT classes at : {\tt
\$API/java/awt/package-summary.html }} }}}

8.4 Using graphics

8.5 The repaint() method

The complete code for this example is given in MovingShape.java in Section 26

8.6 Building a GUI using custom graphics

Objective : We want to build a GUI that will display a red rectangle 40*40 pixels on a blue background. The complete code is in the examples in Section 26.

8.7 The coordinate system

8.8 Other containers

The Graphics class has methods to draw rectangles, ovals, lines, text, polygons and arcs. Some useful methods are listed in the examples in Section 26. Also see: $API/java/awt/Graphics.html There is an example using Panels in Section 26
% latex2html id marker 2600
\fbox {\fbox{\parbox{10cm} { \medskip {\underline{\s...
... able to do Checkpoint 4 : a cellular automaton
simulation of traffic flow}
}}}