26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

726 Graphical User Interface Components: Part 2 Chapter 13<br />

Look-and-Feel Observation 13.3<br />

Combining graphics and Swing GUI components can lead <strong>to</strong> incorrect display of the graphics,<br />

the GUI components or both. Using JPanels for drawing can eliminate this problem<br />

by providing a dedicated area for graphics. 13.3<br />

Swing components that inherit from class JComponent contain method paint-<br />

Component that helps them draw properly in the context of a Swing GUI. When cus<strong>to</strong>mizing<br />

a JPanel for use as a dedicated drawing area, the subclass should override method<br />

paintComponent and call the superclass version of paintComponent as the first<br />

statement in the body of the overridden method. This ensures that painting occurs in the<br />

proper order and that Swing’s painting mechanism remains intact. An important part of<br />

this mechanism is that subclasses of JComponent support transparency, which can be<br />

set with method setOpaque (a false argument indicates the component is transparent).<br />

To paint a component correctly, the program must determine whether the component<br />

is transparent. The code that performs this check is in the superclass version of<br />

paintComponent. When a component is transparent, paintComponent will not<br />

clear the component’s background when the program paints the component. When a component<br />

is opaque, paintComponent clears the background before continuing the<br />

painting operation. If the superclass version of paintComponent is not called, an<br />

opaque GUI component typically will not display correctly on the user interface. Also, if<br />

the superclass version is called after performing the cus<strong>to</strong>mized drawing statements, the<br />

results typically will be erased.<br />

Look-and-Feel Observation 13.4<br />

When overriding a JComponent’s paintComponent method, the first statement in the<br />

body should always be a call <strong>to</strong> the superclass’s original version of the method. 13.4<br />

Common <strong>Program</strong>ming Error 13.1<br />

When overriding a JComponent’s paintComponent method, not calling the superclass’s<br />

original version of paintComponent might prevent the GUI component from displaying<br />

properly on the GUI. 13.1<br />

Common <strong>Program</strong>ming Error 13.2<br />

When overriding a JComponent’s paintComponent method, calling the superclass’s<br />

paintComponent method after other drawing is performed erases the other drawings. 13.2<br />

Classes JFrame and JApplet are not subclasses of JComponent; therefore, they<br />

do not contain method paintComponent. To draw directly on subclasses of JFrame<br />

and JApplet, override method paint.<br />

Look-and-Feel Observation 13.5<br />

Calling repaint for a Swing GUI component indicates that the component should be painted<br />

as soon as possible. The background of the GUI component is cleared only if the component<br />

is opaque. Most Swing components are transparent by default. JComponent method<br />

setOpaque can be passed a boolean argument indicating whether the component is<br />

opaque (true) or transparent (false). The GUI components of package java.awt are<br />

different from Swing components, in that repaint results in a call <strong>to</strong> Component method<br />

update (which clears the component’s background) and update calls method paint<br />

(rather than paintComponent). 13.5

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!