Transcript Chapter 7

Chapter 7
Graphics and Event Handling
1
Overview
• The java.awt and javax.swing
packages and their subpackages support
graphics and event handling.
• Many rudimentary AWT components are
“heavyweight” in that have nonrudimentary
native peers, which in this sense have “extra
baggage.”
2
Overview
• Swing set components are “lightweight” for
the most part in that they do not have native
peers but are rather rendered using primitive
graphics constructs such as lines and shaded
regions.
• The Swing set has only four “heavyweight”
components: JApplet, JFrame,
JDialog, and JWindow.
3
Overview
• In the Swing set, JComponents and their
descendants are “lightweight.”
• The Swing set’s JComponent class
extends the AWT’s Component class.
• Many AWT components (e.g., Button)
have Swing set equivalents (e.g.,
JButton).
4
Event-driven programming
• Modern windows systems have an eventdriven programming model.
– An application in this model typically has a
GUI to facilitate interaction.
– User-generated actions such as mouse clicks
generate events, which the system queues and
then dispatches to event-handling procedures or
“callbacks.”
5
Java event model
• The AWT and Swing set support an eventdelegation model:
– An event source such as a button can generate
events.
– An event listener is an object registered with
the source to be notified when an event occurs.
– A listener provides a callback to which an
Event argument is passed.
6
Java event model
• An event listener implements a particular
interface to signal that the listener is
prepared to handle particular types of
events.
– For instance, listeners of button clicks and
menu selections implement the
ActionListener interface.
7
Java event model
• Event interfaces declare methods that, when
defined by event listeners, serve as
callbacks for particular events.
– For instance, the ActionListener interface
declares the method
public void actionPerformed(
ActionEvent );
8
Java event model
• The basic classes for event handling reside
in the java.awt.event package. The
Swing set augments this package with
specialized event classes, e.g., classes to
handle events on Swing set components
such as JTrees.
9
Graphics basics
• Components and containers are at the core
of Java graphics:
– A component is a prebuilt part such as a button,
a checkbox, a list, or a menu.
– A container is a component that can embed
other components.
– Because containers are components,
components can be nested inside of others.
10
Graphics basics
• A container has a layout manager that
determines how embedded components are
organized and arranged.
– For instance, a FlowLayout arranges
components such as buttons and labels in a leftto-right and top-to-bottom fashion.
11
Graphics basics
• The basic AWT top-level window is the
Frame, and the basic Swing set top-level
window is the JFrame.
• Java windows are constructed as invisible.
A window can be made visible by invoking
its show() method or its setVisible
method with an argument of true.
12
Graphics basics
• The AWT and Swing set have a rich library
of basic components such as buttons, labels,
checkboxes, menu bars, menus (docked and
popup), menu items, tool bars, and lists.
• The AWT and Swing set provide dialog
windows of different types and modalities.
13
Model-View-Controller
• The Swing set has a model-view-controller
architecture.
– The model contains state information about a
component. For instance, a scrollbar’s model
stores information about the thumb’s position.
– The view renders the component visually.
– The controller handles event-driven interactions
between the model and the view.
14
Model-View-Controller
• In the Swing set, basic components such as
buttons do not expose their model. More
advanced components such as JTrees and
JTables expose their models as properties
(e.g., each has a public getModel()
method).
• The Swing set integrates the view and
controller as the UIDelegate.
15
Pluggable look and feel
• The Swing set supports PLAF or Pluggable
Look and Feel for components.
– Through PLAF, the same components can be
rendered in the same style regardless of the
underlying platform.
– AWT components, by contrast, are rendered
differently on different platforms.
16
Graphics context
• A graphics context is an object that
encapsulates information used in drawing
operations, e.g., line width and style.
• The AWT abstract Graphics class is the
superclass for various graphics contexts.
• A graphics context is passed as an argument
to the paint method, which can contain
arbitrary drawing and display code.
17
Summary of Java graphics
• Because Java provides standard packages
and classes for graphics, applications that
use graphics remain portable.
• The relatively simple AWT package
supports basic graphics and event handling.
• The Swing set augments and extends the
basics supplied in the AWT package.
18