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.

480 Chapter 13 Graphics<br />

Problem<br />

Key<br />

Point<br />

13.1 <strong>Introduction</strong><br />

You can draw cus<strong>to</strong>m shapes on a GUI <strong>com</strong>ponent.<br />

Suppose you want <strong>to</strong> draw shapes such as a bar chart, a clock, or a s<strong>to</strong>p sign, as shown in<br />

Figure 13.1. How do you do so?<br />

(a) (b) (c)<br />

FIGURE 13.1<br />

You can draw shapes using the drawing methods in the Graphics class.<br />

paintComponent<br />

Key<br />

Point<br />

This chapter describes how <strong>to</strong> use the methods in the Graphics class <strong>to</strong> draw strings,<br />

lines, rectangles, ovals, arcs, polygons, and images, and how <strong>to</strong> develop reusable GUI <strong>com</strong>ponents.<br />

13.2 The Graphics Class<br />

Each GUI <strong>com</strong>ponent has a graphics context, which is an object of the Graphics<br />

class. The Graphics class contains the methods for drawing various shapes.<br />

The Graphics class provides the methods for drawing strings, lines, rectangles, ovals, arcs,<br />

polygons, and polylines, as shown in Figure 13.2.<br />

Think of a GUI <strong>com</strong>ponent as a piece of paper and the Graphics object as a pencil or<br />

paintbrush. You can apply the methods in the Graphics class <strong>to</strong> draw graphics on a GUI<br />

<strong>com</strong>ponent.<br />

To paint, you need <strong>to</strong> specify where <strong>to</strong> paint. Each <strong>com</strong>ponent has its own coordinate system<br />

with the origin (0, 0) at the upper-left corner. The x-coordinate increases <strong>to</strong> the right, and<br />

the y-coordinate increases downward. Note that the <strong>Java</strong> coordinate system differs from the<br />

conventional coordinate system, as shown in Figure 13.3.<br />

The Graphics class–an abstract class—provides a device-independent graphics interface<br />

for displaying figures and images on the screen on different platforms. Whenever a <strong>com</strong>ponent<br />

(e.g., a but<strong>to</strong>n, a label, or a panel) is displayed, the JVM au<strong>to</strong>matically creates a<br />

Graphics object for the <strong>com</strong>ponent on the native platform and passes this object <strong>to</strong> invoke<br />

the paintComponent method <strong>to</strong> display the drawings.<br />

The signature of the paintComponent method is as follows:<br />

protected void paintComponent(Graphics g)<br />

This method, defined in the JComponent class, is invoked whenever a <strong>com</strong>ponent is first displayed<br />

or redisplayed.<br />

To draw on a <strong>com</strong>ponent, you need <strong>to</strong> define a class that extends JPanel and overrides its<br />

paintComponent method <strong>to</strong> specify what <strong>to</strong> draw. Listing 13.1 gives an example that draws<br />

a line and a string on a panel, as shown in Figure 13.4.

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

Saved successfully!

Ooh no, something went wrong!