-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlphaImageIcon.java
165 lines (152 loc) · 4.52 KB
/
AlphaImageIcon.java
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.ImageObserver;
import javax.swing.Icon;
import javax.swing.ImageIcon;
/**
* An Icon wrapper that paints the contained icon with a specified transparency.
* <P>
* This class is suitable for wrapping an <CODE>ImageIcon</CODE>
* that holds an animated image. To show a non-animated Icon with transparency,
* the companion class {@link AlphaIcon} is a lighter alternative.
*
* @version 1.0 08/16/10
* @author Darryl
*/
public class AlphaImageIcon extends ImageIcon {
private Icon icon;
private Image image;
private float alpha;
/**
* Creates an <CODE>AlphaImageIcon</CODE> with the specified icon and opacity.
* The opacity <CODE>alpha</CODE> should be in the range 0.0F (fully transparent)
* to 1.0F (fully opaque).
*
* @param icon the Icon to wrap
* @param alpha the opacity
*/
public AlphaImageIcon(Icon icon, float alpha) {
this.icon = icon;
this.alpha = alpha;
}
/**
* Overridden to return the image of a wrapped ImageIcon, or null if the wrapped icon
* is not an ImageIcon.
*
* @return the Image object for a wrapped ImageIcon, or null
*/
@Override
public Image getImage() {
return image;
}
/**
* Overridden to forward to a wrapped ImageIcon. Does nothing if the wrapped icon
* is not an ImageIcon.
* <P>
* In common with <code>ImageIcom</code>, the newly set image will only be shown when the
* concerned component(s) are repainted.
*
* @param image Sets the image displayed by a wrapped ImageIcon
*/
@Override
public void setImage(Image image) {
if (icon instanceof ImageIcon) {
((ImageIcon) icon).setImage(image);
}
}
/**
* Overridden to return the status of the image loading operation of a wrapped ImageIcon,
* or 0 if the contained icon is not an ImageIcon.
*
* @return the loading status as defined by java.awt.MediaTracker
*/
@Override
public int getImageLoadStatus() {
if (icon instanceof ImageIcon) {
return ((ImageIcon) icon).getImageLoadStatus();
}
return 0;
}
/**
* Overridden to return the ImageObserver of the image of a wrapped ImageIcon, or null if
* the contained icon is not an ImageIcon.
*
* @return the loading status as defined by java.awt.MediaTracker
*/
@Override
public ImageObserver getImageObserver() {
if (icon instanceof ImageIcon) {
return ((ImageIcon) icon).getImageObserver();
}
return null;
}
/**
* Overridden to forward to a wrapped ImageIcon. Does nothing if the wrapped icon is
* not an ImageIcon.
*
* @param observer the image observer
*/
@Override
public void setImageObserver(ImageObserver observer) {
if (icon instanceof ImageIcon) {
((ImageIcon) icon).setImageObserver(observer);
}
}
/**
* Gets this <CODE>AlphaIcon</CODE>'s opacity
* @return the opacity, in the range 0.0 to 1.0
*/
public float getAlpha() {
return alpha;
}
/**
* Gets the icon wrapped by this <CODE>AlphaIcon</CODE>
* @return the wrapped icon
*/
public Icon getIcon() {
return icon;
}
/**
* Paints the wrapped icon with this <CODE>AlphaImageIcon</CODE>'s transparency.
*
* @param c The component to which the icon is painted
* @param g the graphics context
* @param x the X coordinate of the icon's top-left corner
* @param y the Y coordinate of the icon's top-left corner
*/
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
if (icon instanceof ImageIcon) {
image = ((ImageIcon) icon).getImage();
} else {
image = null;
}
Graphics2D g2 = (Graphics2D) g.create();
g2.setComposite(AlphaComposite.SrcAtop.derive(alpha));
icon.paintIcon(c, g2, x, y);
g2.dispose();
}
/**
* Gets the width of the bounding rectangle of this <CODE>AlphaImageIcon</CODE>.
* Overridden to return the width of the wrapped icom.
*
* @return the width in pixels
*/
@Override
public int getIconWidth() {
return icon.getIconWidth();
}
/**
* Gets the height of the bounding rectangle of this <CODE>AlphaImageIcon</CODE>.
* Overridden to return the height of the wrapped icon.
*
* @return the height in pixels
*/
@Override
public int getIconHeight() {
return icon.getIconHeight();
}
}