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.

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

The program of Fig. 13.2 and Fig. 13.3 demonstrates a cus<strong>to</strong>mized subclass of<br />

JPanel. Class Cus<strong>to</strong>mPanel (Fig. 13.2) has its own paintComponent method that<br />

draws a circle or a square, depending on the value passed <strong>to</strong> Cus<strong>to</strong>mPanel’s draw<br />

method. For this purpose, Cus<strong>to</strong>mPanel line 11 defines constants that enable the program<br />

<strong>to</strong> specify the shape a Cus<strong>to</strong>mPanel draws on itself with each call <strong>to</strong> its paint-<br />

Component method. Class Cus<strong>to</strong>mPanelTest (Fig. 13.3) creates a Cus<strong>to</strong>mPanel<br />

and a GUI that enable the user <strong>to</strong> choose which shape <strong>to</strong> draw.<br />

Class Cus<strong>to</strong>mPanel contains one instance variable, shape, that s<strong>to</strong>res an integer<br />

representing the shape <strong>to</strong> draw. Method paintComponent (lines 15–23) draws a shape<br />

on the panel. If shape is CIRCLE, Graphics method fillOval draws a solid circle.<br />

If shape is SQUARE, Graphics method fillRect draws a solid square. Method<br />

draw (lines 26–30) sets instance variable shape and calls repaint <strong>to</strong> refresh the Cus<strong>to</strong>mPanel<br />

object. Note that calling repaint (which is really this.repaint()) for<br />

the Cus<strong>to</strong>mPanel schedules a painting operation for the Cus<strong>to</strong>mPanel. Method<br />

paintComponent will be called <strong>to</strong> repaint the Cus<strong>to</strong>mPanel and draw the appropriate<br />

shape.<br />

1 // Fig. 13.2: Cus<strong>to</strong>mPanel.java<br />

2 // A cus<strong>to</strong>mized JPanel class.<br />

3<br />

4 // <strong>Java</strong> core packages<br />

5 import java.awt.*;<br />

6<br />

7 // <strong>Java</strong> extension packages<br />

8 import javax.swing.*;<br />

9<br />

10 public class Cus<strong>to</strong>mPanel extends JPanel {<br />

11 public final static int CIRCLE = 1, SQUARE = 2;<br />

12 private int shape;<br />

13<br />

14 // use shape <strong>to</strong> draw an oval or rectangle<br />

15 public void paintComponent( Graphics g )<br />

16 {<br />

17 super.paintComponent( g );<br />

18<br />

19 if ( shape == CIRCLE )<br />

20 g.fillOval( 50, 10, 60, 60 );<br />

21 else if ( shape == SQUARE )<br />

22 g.fillRect( 50, 10, 60, 60 );<br />

23 }<br />

24<br />

25 // set shape value and repaint Cus<strong>to</strong>mPanel<br />

26 public void draw( int shapeToDraw )<br />

27 {<br />

28 shape = shapeToDraw;<br />

29 repaint();<br />

30 }<br />

31<br />

32 } // end class Cus<strong>to</strong>mPanel<br />

Fig. Fig. 13.2 13.2 Defining a cus<strong>to</strong>m drawing area by subclassing JPanel.

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

Saved successfully!

Ooh no, something went wrong!