Links
|
Examples of graphics programming
CS 284 (CSA), Spring 2005
GUI concepts
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 JButton
TextField and JTextField
Applet and JApplet
Choice and JComboBox
Frame and JFrame
Window and JWindow
Panel and JPanel
Label and JLabel
ScrollPane and JScrollPane
Top-level containers include applets, frames, windows,
and dialog boxes.
Containers are components that can contain other
components. For example, a JApplet can contain
JButton s, JTextField s, 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.
Java's AWT graphics
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:
- Native peers for different systems behave a little differently.
- AWT must focus on the "least common denominator" for all systems for
consistency, which limits capabilities of Java components and/or
inhibits taking advantage of special features that
may be available on one system and not another.
- Large Java applications become harder to maintain as the native
graphics systems diverge.
Conclusion: AWT works fine for small applets, but
breaks down for large, ambitious applications (e.g., bn.com ).
Java Swing
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:
- programmable look and feel,
- components with non-rectangular regions,
- many new component types,
- ability to change views dynamically (while the program runs),
- ability to add interesting borders to components,
- serializability (e.g., send a graphics object over network)
- accessibility (e.g., make GUI applications handicap-accessible),
- image backgrounds for buttons.
Disadvantages:
- Mixing lightweight and heavyweight components in the same GUI can
lead to unexpected effects, e.g., one component covering up another.
- Issues with multithreaded applications: for example, a race
condition may arise if the model is changed while Java is
redrawing the view.
|