19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

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

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

488 Chapter 13 Graphics<br />

The repaint method (lines 75, 86) is defined in the Component class. Invoking repaint<br />

causes the paintComponent method <strong>to</strong> be called. The repaint method is invoked <strong>to</strong><br />

refresh the viewing area. Typically, you call it if you have new things <strong>to</strong> display.<br />

don’t invoke<br />

paintComponent<br />

Caution<br />

The paintComponent method should never be invoked directly. It is invoked either by<br />

the JVM whenever the viewing area changes or by the repaint method. You should<br />

override the paintComponent method <strong>to</strong> tell the system how <strong>to</strong> paint the viewing<br />

area, but never override the repaint method.<br />

request repaint using<br />

repaint()<br />

why override<br />

getPreferredSize()?<br />

✓Point✓ Check<br />

Key<br />

Point<br />

Note<br />

The repaint method lodges a request <strong>to</strong> update the viewing area and returns immediately.<br />

Its effect is asynchronous, meaning that it is up <strong>to</strong> the JVM <strong>to</strong> execute the<br />

paintComponent method on a separate thread.<br />

The getPreferredSize() method (lines 95–97), defined in Component, is overridden<br />

in FigurePanel <strong>to</strong> specify the preferred size for the layout manager <strong>to</strong> consider when laying<br />

out a FigurePanel object. This property may or may not be considered by the layout manager,<br />

depending on its rules. For example, a <strong>com</strong>ponent uses its preferred size in a container<br />

with a FlowLayout manager, but its preferred size is ignored if it is placed in a container<br />

with a GridLayout manager. It is a good practice <strong>to</strong> override getPreferredSize() in a<br />

subclass of JPanel <strong>to</strong> specify a preferred size, because the default width and height for a<br />

JPanel is 0. You will see nothing if a JPanel <strong>com</strong>ponent with a default 0 width and height<br />

is placed in a FlowLayout container.<br />

13.12 Why should you override the preferredSize method in a subclass of JPanel?<br />

13.13 How do you get and set colors and fonts in a Graphics object?<br />

13.5 Drawing Arcs<br />

An arc is conceived as part of an oval bounded by a rectangle.<br />

The methods <strong>to</strong> draw or fill an arc are as follows:<br />

drawArc(int x, int y, int w, int h, int startAngle, int arcAngle)<br />

fillArc(int x, int y, int w, int h, int startAngle, int arcAngle)<br />

Parameters x, y, w, and h are the same as in the drawOval method; parameter startAngle<br />

is the starting angle; and arcAngle is the spanning angle (i.e., the angle covered by the arc).<br />

Angles are measured in degrees and follow the usual mathematical conventions (i.e., 0<br />

degrees is in the easterly direction, and positive angles indicate counterclockwise rotation<br />

from the easterly direction); see Figure 13.10.<br />

(x, y)<br />

w<br />

arcAngle<br />

h<br />

startAngle<br />

FIGURE 13.10<br />

The drawArc method draws an arc based on an oval with specified angles.

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

Saved successfully!

Ooh no, something went wrong!