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.

606 Chapter 16 Event-Driven <strong>Programming</strong><br />

but<strong>to</strong>ns<br />

circle canvas<br />

CirclePanel class<br />

paint the circle<br />

LISTING 16.2<br />

ControlCircleWithoutEventHandling.java<br />

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

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

3<br />

4 public class ControlCircleWithoutEventHandling extends JFrame {<br />

5 private JBut<strong>to</strong>n jbtEnlarge = new JBut<strong>to</strong>n("Enlarge");<br />

6 private JBut<strong>to</strong>n jbtShrink = new JBut<strong>to</strong>n("Shrink");<br />

7 private CirclePanel canvas = new CirclePanel();<br />

8<br />

9 public ControlCircleWithoutEventHandling() {<br />

10 JPanel panel = new JPanel(); // Use the panel <strong>to</strong> group but<strong>to</strong>ns<br />

11 panel.add(jbtEnlarge);<br />

12 panel.add(jbtShrink);<br />

13<br />

14 this.add(canvas, BorderLayout.CENTER); // Add canvas <strong>to</strong> center<br />

15 this.add(panel, BorderLayout.SOUTH); // Add but<strong>to</strong>ns <strong>to</strong> the frame<br />

16 }<br />

17<br />

18 /** Main method */<br />

19 public static void main(String[] args) {<br />

20 JFrame frame = new ControlCircleWithoutEventHandling();<br />

21 frame.setTitle("ControlCircleWithoutEventHandling");<br />

22 frame.setLocationRelativeTo(null); // Center the frame<br />

23 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />

24 frame.setSize(200, 200);<br />

25 frame.setVisible(true);<br />

26 }<br />

27 }<br />

28<br />

29 class CirclePanel extends JPanel {<br />

30 private int radius = 5; // Default circle radius<br />

31<br />

32 @Override /** Repaint the circle */<br />

33 protected void paintComponent(Graphics g) {<br />

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

35 g.drawOval(getWidth() / 2 - radius, getHeight() / 2 - radius,<br />

36 2 * radius, 2 * radius);<br />

37 }<br />

38 }<br />

second version<br />

inner class<br />

How do you use the but<strong>to</strong>ns <strong>to</strong> enlarge or shrink the circle? When the Enlarge but<strong>to</strong>n is clicked,<br />

you want the circle <strong>to</strong> be repainted with a larger radius. How can you ac<strong>com</strong>plish this? You can<br />

expand the program in Listing 16.2 in<strong>to</strong> Listing 16.3 with the following features:<br />

1. Define a listener class named EnlargeListener that implements ActionListener<br />

(lines 31–36).<br />

2. Create a listener and register it with jbtEnlarge (line 18).<br />

3. Add a method named enlarge() in CirclePanel <strong>to</strong> increase the radius, then repaint<br />

the panel (lines 42–45).<br />

4. Implement the actionPerformed method in EnlargeListener <strong>to</strong> invoke<br />

canvas.enlarge() (line 34).<br />

5. To make the reference variable canvas accessible from the actionPerformed<br />

method, define EnlargeListener as an inner class of the ControlCircle class<br />

(lines 31–36). (Inner classes are defined inside another class. We will introduce inner<br />

classes in the next section.)

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

Saved successfully!

Ooh no, something went wrong!