1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.ui.swing.components;
20
21 import java.awt.Dimension;
22 import java.awt.Graphics;
23 import java.awt.Image;
24 import java.awt.image.ImageObserver;
25
26 import javax.swing.JPanel;
27
28 /***
29 * A JPanel that renders an image, tiling as necessary to
30 * fill the panel.
31 * The preferred size of the panel is the size of the image
32 * and will change until the image is fully loaded, so using
33 * a media tracker is recommended.
34 *
35 * @author michael@mpowers.net
36 * @version $Revision: 904 $
37 */
38 public class ImagePanel extends JPanel implements ImageObserver
39 {
40 protected Image image;
41 protected int imageWidth, imageHeight;
42
43 public ImagePanel()
44 {
45 this( null );
46 }
47
48 public ImagePanel( Image anImage )
49 {
50 image = anImage;
51 if ( anImage != null )
52 {
53 prepareImage( image, this );
54
55 imageWidth = image.getWidth( this );
56 imageHeight = image.getHeight( this );
57 }
58 else
59 {
60 imageWidth = 0;
61 imageHeight = 0;
62 }
63 }
64
65 protected void paintComponent(Graphics g)
66 {
67 if ( ( image != null ) && ( imageWidth > 0 ) && ( imageHeight > 0 ) )
68 {
69 int width = getWidth();
70 int height = getHeight();
71
72 for ( int x = 0; x < width; x += imageWidth )
73 {
74 for ( int y = 0; y < height; y += imageHeight )
75 {
76 g.drawImage( image, x, y,
77 imageWidth, imageHeight,
78 getBackground(), this );
79 }
80 }
81 }
82 }
83
84 public boolean imageUpdate(Image img,
85 int infoflags,
86 int x,
87 int y,
88 int width,
89 int height)
90 {
91 imageWidth = width;
92 imageHeight = height;
93 setPreferredSize( new Dimension( width, height ) );
94 revalidate();
95 repaint();
96
97 if ( ( infoflags & ImageObserver.ALLBITS ) == ImageObserver.ALLBITS )
98 {
99 return false;
100 }
101 return true;
102 }
103
104 }