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.

504 Chapter 13 Graphics<br />

get current time<br />

override<br />

getPreferredSize<br />

94 int xHour = (int)(xCenter + hLength *<br />

95 Math.sin((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));<br />

96 int yHour = (int)(yCenter - hLength *<br />

97 Math.cos((hour % 12 + minute / 60.0) * (2 * Math.PI / 12)));<br />

98 g.setColor(Color.green);<br />

99 g.drawLine(xCenter, yCenter, xHour, yHour);<br />

100 }<br />

101<br />

102 public void setCurrentTime() {<br />

103 // Construct a calendar for the current date and time<br />

104 Calendar calendar = new GregorianCalendar();<br />

105<br />

106 // Set current hour, minute, and second<br />

107 this.hour = calendar.get(Calendar.HOUR_OF_DAY);<br />

108 this.minute = calendar.get(Calendar.MINUTE);<br />

109 this.second = calendar.get(Calendar.SECOND);<br />

110 }<br />

111<br />

112 @Override<br />

113 public Dimension getPreferredSize() {<br />

114 return new Dimension(200, 200);<br />

115 }<br />

116 }<br />

The program enables the clock size <strong>to</strong> adjust as the frame resizes. Every time you resize the<br />

frame, the paintComponent method is au<strong>to</strong>matically invoked <strong>to</strong> paint a new clock. The<br />

paintComponent method displays the clock in proportion <strong>to</strong> the panel width (getWidth())<br />

and height (getHeight()) (lines 60–63 in StillClock).<br />

Key<br />

Point<br />

13.10 Displaying Images<br />

You can draw images in a graphics context.<br />

You learned how <strong>to</strong> create image icons and display them in labels and but<strong>to</strong>ns in Section 12.10,<br />

Image Icons. For example, the following statements create an image icon and display it in a label:<br />

ImageIcon imageIcon = new ImageIcon("image/us.gif");<br />

JLabel jlblImage = new JLabel(imageIcon);<br />

An image icon displays a fixed-size image. To display an image in a flexible size, you need <strong>to</strong><br />

use the java.awt.Image class. An image can be created from an image icon using the<br />

getImage() method as follows:<br />

Image image = imageIcon.getImage();<br />

Using a label as an area for displaying images is simple and convenient, but you don’t have<br />

much control over how the image is displayed. A more flexible way <strong>to</strong> display images is <strong>to</strong><br />

use the drawImage method of the Graphics class on a panel. Four versions of the<br />

drawImage method are shown in Figure 13.22.<br />

ImageObserver specifies a GUI <strong>com</strong>ponent for receiving notifications of image information<br />

as the image is constructed. To draw images using the drawImage method in a Swing<br />

<strong>com</strong>ponent, such as JPanel, override the paintComponent method <strong>to</strong> tell the <strong>com</strong>ponent<br />

how <strong>to</strong> display the image in the panel.<br />

Listing 13.11 gives the code that displays an image from image/us.gif. The file<br />

image/us.gif (line 20) is under the class direc<strong>to</strong>ry. An Image object is obtained in line 21.<br />

The drawImage method displays the image <strong>to</strong> fill in the whole panel, as shown in<br />

Figure 13.23.

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

Saved successfully!

Ooh no, something went wrong!