Java Reference
In-Depth Information
27 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
28 frame.setVisible( true );
29 }
30 }
F IGURE 13.25
Six images are displayed in six ImageViewer components.
The ImageViewer class is implemented in Listing 13.13. ( Note: You may skip the imple-
mentation. ) The accessor and mutator methods for the properties image , stretched ,
xCoordinate , and yCoordinate are easy to implement. The paintComponent method
(lines 27-36) displays the image on the panel. Line 30 ensures that the image is not null
before displaying it. Line 31 checks whether the image is stretched or not.
implementation
skip implementation?
L ISTING 13.13 ImageViewer.java
1 import java.awt.*;
2 import javax.swing.*;
3
4
public class ImageViewer extends JPanel
{
5
/** Hold value of property image */
6
private java.awt.Image image;
properties
7
8
/** Hold value of property stretched */
9
private boolean stretched = true ;
10
11
/** Hold value of property xCoordinate */
12
private int xCoordinate;
13
14
/** Hold value of property yCoordinate */
15
private int yCoordinate;
16
17 /** Construct an empty image viewer */
18 public ImageViewer() {
19 }
20
21
constructor
/** Construct an image viewer for a specified Image object */
22
public ImageViewer(Image image) {
constructor
23
this .image = image;
24 }
25
26 @Override
27
protected void paintComponent(Graphics g)
{
28
super .paintComponent(g);
29
30
31 if ( )
32 g.drawImage(image, xCoordinate, yCoordinate,
33 getWidth(), getHeight(), this );
34 else
35 g.drawImage(image, xCoordinate, yCoordinate, this );
36 }
if (image != null )
image null?
isStretched()
stretched
nonstretched
 
 
Search WWH ::




Custom Search