1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2000 Intersect Software Corporation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/
package net.wotonomy.ui.swing.components;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.ImageObserver;
import javax.swing.JPanel;
/**
* A JPanel that renders an image, tiling as necessary to fill the panel. The
* preferred size of the panel is the size of the image and will change until
* the image is fully loaded, so using a media tracker is recommended.
*
* @author michael@mpowers.net
* @version $Revision: 904 $
*/
public class ImagePanel extends JPanel implements ImageObserver {
protected Image image;
protected int imageWidth, imageHeight;
public ImagePanel() {
this(null);
}
public ImagePanel(Image anImage) {
image = anImage;
if (anImage != null) {
prepareImage(image, this);
// these may return -1
imageWidth = image.getWidth(this);
imageHeight = image.getHeight(this);
} else {
imageWidth = 0;
imageHeight = 0;
}
}
protected void paintComponent(Graphics g) {
if ((image != null) && (imageWidth > 0) && (imageHeight > 0)) {
int width = getWidth();
int height = getHeight();
for (int x = 0; x < width; x += imageWidth) {
for (int y = 0; y < height; y += imageHeight) {
g.drawImage(image, x, y, imageWidth, imageHeight, getBackground(), this);
}
}
}
}
public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) {
imageWidth = width;
imageHeight = height;
setPreferredSize(new Dimension(width, height));
revalidate();
repaint();
if ((infoflags & ImageObserver.ALLBITS) == ImageObserver.ALLBITS) {
return false;
}
return true;
}
}
|