Examples of graphics programming
CSA-Prep, Fall 2004
One of Java's main design goals is platform independence: A (byte-compiled) Java program should run on any correct JVM interpreter, no matter what computer that interpreter is running on. (The combination of hardware and software for a particular computing system is called a platform.)
GUIs or graphics user interfaces, provide graphics components to their users that (hopefully!) visually explain the activities available for a user. The following are some examples of components in Java:
Button and JButtonTextField and JTextFieldApplet and JAppletChoice and JComboBoxFrame and JFrameWindow and JWindowPanel and JPanelLabel and JLabelScrollPane and JScrollPaneTop-level containers include applets, frames, windows, and dialog boxes.
Containers are components that can contain other
components. For example, a JApplet can contain
JButtons, JTextFields, etc. On the other
hand, Button and JButton are not a
containers.
A layout manager is an object that determines the arrangement of components within a container. Java provides numerous layout managers, including:
BorderLayout, in which components can be added to the
"north," "south," "east," "west," or "center" of a container;FlowLayout, in which components are added from left
to right then from top to bottom (similar to the order we write in);
and GridBagLayout, which allows for rectangular
arrangements of components organized into rows and columns with
"gutter" spacing between, etc.AWT (Abstract Window Toolkit) graphics is available for Java version 1.1 and later. In AWT, each component in a Java program has a counterpart in the underlying graphics platform.
Example: If running on a Linux platform, every Java
Button component has a corresponding X-windows
Button object (which is what actually appears on the
screen).
If running on a Windows platform, a Windows
Button is used instead; likewise for a Macintosh.
The platform-specific counterpart of a Java component is sometimes called that component's native peer.
Observe that the "look and feel" of the GUI is determined by the graphics platform when AWT is used. For example, on an older Macintosh system, several parallel horizontal lines appear in the title bar of each window.
Advantages: Reuse the code already written for the native peers.
Disadvantages:
Conclusion: AWT works fine for small applets, but
breaks down for large, ambitious applications (e.g., bn.com).
The Swing graphics system appeared in Java 1.2. In Swing, only top-level components have native peers; all other components are drawn on those top-level components.
Components with native peers are often refered to as heavyweight components, having the "overhead" of their native peers when they are used. Components that are merely drawn on heavyweight components are called lightweight components.
The lightweight components are those with a J at
the beginnings of their names, e.g., JButton,
JLabel, etc.
Swing takes advantage of the Model/View/Control (MVC) model for behavior of a graphics system. Consider the example of a scroll pane:
The data model include the length of the "thumb" and its maximum and minimum values.
The view is the way that scroll pane appears visually on the screen.
The control includes the ability of a user to drag the thumb.
For another example, consider the Scribble applet.
The state variables lastX and lastY
are part of the model.
The scribbling region and the JButton and
JComboBox menu make up the view.
Each callback is part of the control.
The control determines data model values and may
cause changes in the view (e.g., requesting a repaint()
to overwrite any scribbling); the
model values determine what the view can draw; and the
view may provide for control via user actions (e.g.,
selecting a new color or clearing the drawing area).
Since Swing lightweight components are implemented entirely in
Java, Swing can separate model from
view and control and provide these to the
programmer. Swing combines view and control
in an object called a UI delegate; the programmer can choose
which UI delegate to use.
Advantages: Many new features are available under Java programmer control in Swing, including:
Disadvantages:
rab@stolaf.edu, October 27, 2004