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.

2. Whenever a <strong>com</strong>ponent (e.g., a but<strong>to</strong>n, a label, or a panel) is displayed, the JVM<br />

au<strong>to</strong>matically creates a Graphics object for the <strong>com</strong>ponent on the native platform<br />

and passes this object <strong>to</strong> invoke the paintComponent method <strong>to</strong> display the<br />

drawings.<br />

3. Normally you use JPanel as a canvas. To draw on a JPanel, you create a new class<br />

that extends JPanel and overrides the paintComponent method <strong>to</strong> tell the panel<br />

how <strong>to</strong> draw graphics.<br />

4. Invoking super.paintComponent(g) is necessary <strong>to</strong> ensure that the viewing area<br />

is cleared before a new drawing is displayed. The user can request the <strong>com</strong>ponent <strong>to</strong><br />

be redisplayed by invoking the repaint() method defined in the Component class.<br />

Invoking repaint() causes paintComponent <strong>to</strong> be invoked by the JVM. The user<br />

should never invoke paintComponent directly. For this reason, the protected visibility<br />

is sufficient for paintComponent.<br />

5. The Component class has the setBackground, setForeground, and setFont<br />

methods. These methods are used <strong>to</strong> set colors and fonts for the entire <strong>com</strong>ponent. If<br />

you want <strong>to</strong> draw several messages in a panel with different colors and fonts, you<br />

have <strong>to</strong> use the setColor and setFont methods in the Graphics class <strong>to</strong> set the<br />

color and font for the current drawing.<br />

6. FontMetrics can be used <strong>to</strong> <strong>com</strong>pute the exact length and width of a string,<br />

which is helpful for measuring the size of a string in order <strong>to</strong> display it in the right<br />

position.<br />

7. To display an image, first create an image icon. You can then use ImageIcon’s<br />

getImage() method <strong>to</strong> get an Image object for the image and draw the image using<br />

the drawImage method in the java.awt.Graphics class.<br />

<strong>Programming</strong> Exercises 509<br />

TEST QUESTIONS<br />

Do the test questions for this chapter online at www.cs.armstrong.edu/liang/intro9e/test.html.<br />

PROGRAMMING EXERCISES<br />

Sections 13.2–13.7<br />

*13.1 (Display a 3 * 3 grid) Write a program that displays a 3 * 3 grid, as shown in<br />

Figure 13.26a. Use red color for vertical lines and blue for horizontals.<br />

**13.2 (Create a cus<strong>to</strong>m but<strong>to</strong>n class) Develop a cus<strong>to</strong>m but<strong>to</strong>n class named<br />

OvalBut<strong>to</strong>n that extends JBut<strong>to</strong>n and displays the but<strong>to</strong>n text inside an oval.<br />

Figure 13.26b shows two but<strong>to</strong>ns created using the OvalBut<strong>to</strong>n class.<br />

*13.3 (Display a checkerboard) <strong>Programming</strong> Exercise 12.10 displays a checkerboard<br />

in which each white and black cell is a JBut<strong>to</strong>n. Rewrite a program that draws a<br />

checkerboard on a JPanel using the drawing methods in the Graphics class, as<br />

shown in Figure 13.26c. Use the drawRect method <strong>to</strong> draw each cell in the<br />

checkerboard.<br />

*13.4 (Display a multiplication table) Write a program that displays a multiplication<br />

table in a panel using the drawing methods, as shown in Figure 13.27a.

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

Saved successfully!

Ooh no, something went wrong!