19.09.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

13.3 Drawing Strings, Lines, Rectangles, and Ovals 483<br />

Tip<br />

Some textbooks define a canvas class by subclassing JComponent. The problem with<br />

doing that is if you want <strong>to</strong> set a background in the canvas, you have <strong>to</strong> write the code<br />

<strong>to</strong> paint the background color. A simple setBackground(Color color) method<br />

will not set a background color in a JComponent.<br />

13.1 Suppose that you want <strong>to</strong> draw a new message below an existing message. Should the<br />

x-coordinate, y-coordinate, or both increase or decrease?<br />

13.2 How is a Graphics object created?<br />

13.3 How is the paintComponent method invoked? How can a program invoke this<br />

method?<br />

13.4 Why is the paintComponent method protected? What happens if you change it<br />

<strong>to</strong> public or private in a subclass? Why is super.paintComponent(g)<br />

invoked in line 22 in Listing 13.1?<br />

13.5 Can you draw things on any Swing GUI <strong>com</strong>ponent? Why should you use a panel as<br />

a canvas for drawings rather than a label or a but<strong>to</strong>n?<br />

13.3 Drawing Strings, Lines, Rectangles, and Ovals<br />

You can draw strings, lines, rectangles, and ovals in a graphics context.<br />

The drawString(String s, int x, int y) method draws a string starting at the point<br />

(x, y), as shown in Figure 13.5a.<br />

The drawLine(int x1, int y1, int x2, int y2) method draws a straight line<br />

from point (x1, y1) <strong>to</strong> point (x2, y2), as shown in Figure 13.5b.<br />

extends JComponent?<br />

✓Point✓ Check<br />

Key<br />

Point<br />

drawString<br />

drawLine<br />

(0, 0) (getWidth(), 0)<br />

(0, 0) (getWidth(), 0)<br />

(x1, y1)<br />

(x, y)<br />

(0, getHeight())<br />

s is displayed here<br />

(getWidth(), getHeight())<br />

(a) drawString<br />

(x2, y2)<br />

(0, getHeight()) (getWidth(), getHeight())<br />

(b) drawLine<br />

FIGURE 13.5 (a) The drawString(s, x, y) method draws a string starting at (x, y). (b) The drawLine(x1, y1,<br />

x2, y2) method draws a line between two specified points.<br />

<strong>Java</strong> provides six methods for drawing the outline of rectangles or rectangles filled with<br />

color. You can draw or fill plain rectangles, round-cornered rectangles, or three-dimensional<br />

rectangles.<br />

The drawRect(int x, int y, int w, int h) method draws a plain rectangle, and<br />

the fillRect(int x, int y, int w, int h) method draws a filled rectangle. The<br />

parameters x and y represent the upper-left corner of the rectangle, and w and h are its width<br />

and height (see Figure 13.6).<br />

The drawRoundRect(int x, int y, int w, int h, int aw, int ah) method<br />

draws a round-cornered rectangle, and the fillRoundRect(int x, int y, int w, int<br />

h, int aw, int ah) method draws a filled round-cornered rectangle. Parameters x, y, w,<br />

and h are the same as in the drawRect method, parameter aw is the horizontal diameter of the<br />

arcs at the corner, and ah is the vertical diameter of the arcs at the corner (see Figure 13.7a).<br />

drawRect<br />

fillRect<br />

drawRoundRect<br />

fillRoundRect

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

Saved successfully!

Ooh no, something went wrong!