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

Create successful ePaper yourself

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

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

Method Math.abs gets the absolute value of the subtractions x1 - x2 and y1 - y2 that<br />

determine the width and height of the oval’s bounding rectangle, respectively. When the<br />

calculations are complete, paintComponent draws the oval. The call <strong>to</strong> the superclass<br />

version of paintComponent at the beginning of the method guarantees that the previous<br />

oval displayed on the SelfContainedPanel is erased before the new one is displayed.<br />

Look-and-Feel Observation 13.9<br />

Most Swing GUI components can be transparent or opaque. If a Swing GUI component is<br />

opaque, when its paintComponent method is called, its background will be cleared. Otherwise,<br />

its background will not be cleared. Only opaque components can display a cus<strong>to</strong>mized<br />

background color. 13.9<br />

Look-and-Feel Observation 13.10<br />

JPanel objects are opaque by default. 13.10<br />

When the user releases the mouse but<strong>to</strong>n, method mouseReleased (lines 34–39)<br />

captures in variables x2 and y2 the final location of the mouse and invokes repaint <strong>to</strong><br />

draw the final version of the oval.<br />

Class SelfContainedPanelTest’s construc<strong>to</strong>r (lines 21–57 of Fig. 13.5) creates<br />

an instance of class SelfContainedPanel (line 24) and sets the background color (line<br />

25) of the SelfContainedPanel <strong>to</strong> yellow so that its area is visible against the background<br />

of the application window.<br />

1 // Fig. 13.5: SelfContainedPanelTest.java<br />

2 // Creating a self-contained subclass of JPanel<br />

3 // that processes its own mouse events.<br />

4<br />

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

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

7 import java.awt.event.*;<br />

8<br />

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

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

11<br />

12 // Deitel packages<br />

13 import com.deitel.jhtp4.ch13.SelfContainedPanel;<br />

14<br />

15 public class SelfContainedPanelTest extends JFrame {<br />

16 private SelfContainedPanel myPanel;<br />

17<br />

18<br />

19 // set up GUI and mouse motion event handlers for<br />

20 // application window<br />

21 public SelfContainedPanelTest()<br />

22 {<br />

23 // set up a SelfContainedPanel<br />

24 myPanel = new SelfContainedPanel();<br />

25 myPanel.setBackground( Color.yellow );<br />

26<br />

27 Container container = getContentPane();<br />

Fig. Fig. 13.5 13.5 Capturing mouse events with a JPanel (part 1 of 3).

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

Saved successfully!

Ooh no, something went wrong!