190 likes | 284 Views
Chapter 7. Graphics and Event Handling. 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.”. Overview.
E N D
Chapter 7 Graphics and Event Handling
Overview • The java.awt and javax.swingpackages 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.”
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.
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).
Event-driven programming • Modern windows systems have an event-driven 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.”
Java event model • The AWT and Swing set support an event-delegation 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.
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.
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 );
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.
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.
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 left-to-right and top-to-bottom fashion.
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 setVisiblemethod with an argument of true.
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.
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.
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.
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.
Graphics context • A graphics context is an object that encapsulates information used in drawing operations, e.g., line width and style. • The AWT abstract Graphicsclass 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.
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.