IntroJB10.pdf

(456 KB) Pobierz
Microsoft Word - CH10GettingStartedWithGraphicsProgramming.DOC
Part III: Graphics Programming
In Part II, "Object-Oriented Programming," you learned the
basics of object-oriented programming. The design of the API
for Java graphics programming is an excellent example of how
the object-oriented principle is applied. In the chapters
that follow in this part of the book, you will learn the
architecture of Java graphics programming API and use the
user interface components to develop graphics applications
and applets.
Chapter 10 Getting Started with Graphics
Programming
Chapter 11 Creating User Interfaces
Chapter 12 Applets and Advanced Graphics
387
10
Getting Started with Graphics Programming
Objectives
Describe the Java graphics programming class
hierarchy.
Use frames, panels, and simple UI components.
Understand the role of layout managers.
Use the FlowLayout, GridLayout, and BorderLayout
managers.
Become familiar with the paintComponent method.
Become familiar with the classes Color, Font, and
FontMetrics.
Be able to use the drawing methods in the Graphics
class.
Understand the concept of event-driven programming.
Become familiar with the Java event-delegation model:
event registration, listening, and handling.
388
734833496.001.png
Introduction
Until now, you have only used text-based input and output.
You used MyInput.readInt() and MyInput.readDouble() to read
numbers from the keyboard, and System.out.println to display
results on the console. This is the old-fashioned way to
program. Today’s client/server and Web-based applications
use a graphical user interface known as GUI (pronounced goo-
ee).
When Java was introduced, the graphics components were
bundled in a library known as the Abstract Windows Toolkit
(AWT). For every platform on which Java runs, the AWT
components are automatically mapped to the platform-specific
components through their respective agents, known as peers .
AWT is fine for developing simple GUI applications, but not
for developing comprehensive GUI projects. Besides, AWT is
prone to platform-specific bugs because its peer-based
approach heavily relies on the underlying platform. With the
release of Java 2, the AWT user interface components were
replaced by a more robust, versatile, and flexible library
known as the Swing components . Swing components are painted
directly on canvases using Java code, except for components
that are subclasses of java.awt.Window or java.awt.Panel,
which must be drawn using native GUI on a specific platform.
Swing components are less dependent on the target platform
and use less of the native GUI resource. For this reason,
Swing components that don’t rely on native GUI are referred
to as lightweight components, and AWT components are
referred to as heavyweight components . Although AWT
components are still supported in Java 2, I recommend that
you learn to program using the Swing components, because the
AWT user interface components will eventually fade away.
Java provides a rich set of classes to help you build
graphical user interfaces. You can use various GUI-building
classes—frames, panels, labels, buttons, text fields, text
areas, combo boxes, check boxes, radio buttons, menus,
scroll bars, scroll panes, and tabbed panes—to construct
user interfaces. This chapter introduces the basics of Java
graphics programming. Specifically, it discusses GUI
components and their relationships, containers and layout
managers, colors, fonts, and drawing geometric figures, such
as lines, rectangles, ovals, arcs, and polygons, and
introduces event-driven programming.
NOTE: The Swing components do not replace all
the classes in AWT, only the AWT user interface
components (Button, TextField, TextArea, etc.).
The AWT helper classes (Graphics, Color, Font,
FontMetrics, and LayoutManager) remain
389
734833496.002.png
unchanged. In addition, the Swing components use
the AWT event model.
The Java Graphics API
The design of the Java graphics programming API is an
excellent example of the use of classes, inheritance, and
interfaces. The graphics API contains the essential classes
listed below. Their hierarchical relationships are shown in
Figure 10.1.
***Insert Figure 10.1 (Same as Figure 8.1 in the 3 rd Edition,
p287)
Figure 10.1
Java graphics programming utilizes the classes shown in this
hierarchical diagram.
Component : This is a superclass of all user interface
classes.
Container: This is used to group components. A layout
manager is used to position and place components in a
container in the desired location and style. Frames,
panels, and applets are examples of containers.
JComponent: This is a superclass of all the
lightweight Swing components, which are drawn directly
on canvases using Java code, rather than on specific
platforms using native GUI. Its subclasses (JButton,
JCheckBox, JMenu, JRadioButton, JLabel, JList,
JTextField, JTextArea, JScrollPane) are the basic
elements for constructing the GUI.
JFrame: This is a window not contained inside another
window. JFrame is the container that holds other Swing
user interface components in Java graphical
applications.
JDialog This is a popup window, or message box
generally used as a temporary window to receive
additional information from the user or to provide
notification that an event has occurred.
JApplet: This is a subclass of Applet. You must extend
JApplet to create a Swing-based Java applet.
JPanel: This is an invisible container that holds user
interface components. Panels can be nested. You can
place panels inside panels and in a frame in Java
390
734833496.003.png
applications or in an applet in Java applets. JPanel
can also be used as a canvas to draw graphics.
Graphics: This is an abstract class that provides a
graphical context for drawing strings, lines, and
simple shapes.
Color: This deals with the colors of graphics
components. For example, you can specify background or
foreground colors in a component like JFrame and
JPanel, or you can specify colors of lines, shapes,
and strings in drawings.
Font: This is used for drawings in Graphics. For
example, you can specify the font type (SansSerif),
style (bold), and size (24 points) to draw a string.
FontMetrics: This is an abstract class used to get
properties of the fonts used in drawings.
The graphics classes can be classified into three groups:
the container classes , the component classes , and the helper
classes . The container classes, such as JFrame, JPanel, and
JApplet, are used to contain other components. The UI
component classes, such as JButton, JTextField, JTextArea,
JComboBox, JList, JRadioButton, and JMenu, are subclasses of
JComponent. The helper classes, such as Graphics, Color,
Font, FontMetrics , Dimension , and LayoutManager , are used by
components and containers to draw and place objects.
The JFrame, JApplet, JDialog, and JComponent classes and
their subclasses are grouped in the package javax.swing. All
the other classes in Figure 10.1 are grouped in the package
java.awt. Most Swing components are named with a prefixed J .
For example, the Swing version of Button is called JButton
to distinguish it from its AWT counterpart.
NOTE: Swing is a comprehensive solution to
developing graphic user interfaces. There are
over 250 classes in Swing, some of which are
illustrated in Figure 10.2. Since the discussion
in this book is only an introduction to Java
graphics programming using Swing, the components
listed in the dotted rectangle are not covered.
For a more detailed treatment of Swing
components, including model-view architecture,
look and feel, and advanced components, please
refer to my Rapid Java Application Development
Using JBuilder 4 , published by Prentice-Hall.
TIP: Do not mix Swing user interface components
like JButton with AWT user interface components
391
734833496.004.png
Zgłoś jeśli naruszono regulamin