Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
ImagePanel |
|
| 2.5;2.5 |
1 | /* |
|
2 | Wotonomy: OpenStep design patterns for pure Java applications. |
|
3 | Copyright (C) 2000 Intersect Software Corporation |
|
4 | ||
5 | This library is free software; you can redistribute it and/or |
|
6 | modify it under the terms of the GNU Lesser General Public |
|
7 | License as published by the Free Software Foundation; either |
|
8 | version 2.1 of the License, or (at your option) any later version. |
|
9 | ||
10 | This library is distributed in the hope that it will be useful, |
|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 | Lesser General Public License for more details. |
|
14 | ||
15 | You should have received a copy of the GNU Lesser General Public |
|
16 | License along with this library; if not, see http://www.gnu.org |
|
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 | 0 | this( null ); |
46 | 0 | } |
47 | ||
48 | 0 | public ImagePanel( Image anImage ) |
49 | 0 | { |
50 | 0 | image = anImage; |
51 | 0 | if ( anImage != null ) |
52 | { |
|
53 | 0 | prepareImage( image, this ); |
54 | // these may return -1 |
|
55 | 0 | imageWidth = image.getWidth( this ); |
56 | 0 | imageHeight = image.getHeight( this ); |
57 | 0 | } |
58 | else |
|
59 | { |
|
60 | 0 | imageWidth = 0; |
61 | 0 | imageHeight = 0; |
62 | } |
|
63 | 0 | } |
64 | ||
65 | protected void paintComponent(Graphics g) |
|
66 | { |
|
67 | 0 | if ( ( image != null ) && ( imageWidth > 0 ) && ( imageHeight > 0 ) ) |
68 | { |
|
69 | 0 | int width = getWidth(); |
70 | 0 | int height = getHeight(); |
71 | ||
72 | 0 | for ( int x = 0; x < width; x += imageWidth ) |
73 | { |
|
74 | 0 | for ( int y = 0; y < height; y += imageHeight ) |
75 | { |
|
76 | 0 | g.drawImage( image, x, y, |
77 | 0 | imageWidth, imageHeight, |
78 | 0 | getBackground(), this ); |
79 | } |
|
80 | } |
|
81 | } |
|
82 | 0 | } |
83 | ||
84 | public boolean imageUpdate(Image img, |
|
85 | int infoflags, |
|
86 | int x, |
|
87 | int y, |
|
88 | int width, |
|
89 | int height) |
|
90 | { |
|
91 | 0 | imageWidth = width; |
92 | 0 | imageHeight = height; |
93 | 0 | setPreferredSize( new Dimension( width, height ) ); |
94 | 0 | revalidate(); |
95 | 0 | repaint(); |
96 | ||
97 | 0 | if ( ( infoflags & ImageObserver.ALLBITS ) == ImageObserver.ALLBITS ) |
98 | { |
|
99 | 0 | return false; |
100 | } |
|
101 | 0 | return true; |
102 | } |
|
103 | ||
104 | } |