-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathAlphaIcon.java
More file actions
93 lines (84 loc) · 2.42 KB
/
AlphaIcon.java
File metadata and controls
93 lines (84 loc) · 2.42 KB
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
/**
* @(#)AlphaIcon.java 1.0 08/16/10
*/
//package darrylbu.icon;
import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.Icon;
/**
* An Icon wrapper that paints the contained icon with a specified transparency.
* <P>
* <B>Note:</B> This class is not suitable for wrapping an <CODE>ImageIcon</CODE>
* that holds an animated image. To show an animated ImageIcon with transparency,
* use the companion class {@link AlphaImageIcon}.
*
* @version 1.0 08/16/10
* @author Darryl
*/
public class AlphaIcon implements Icon {
private Icon icon;
private float alpha;
/**
* Creates an <CODE>AlphaIcon</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 AlphaIcon(Icon icon, float alpha) {
this.icon = icon;
this.alpha = alpha;
}
/**
* 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>AlphaIcon</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) {
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>AlphaIcon</CODE>.
* Returns the width of the wrapped icon.
*
* @return the width in pixels
*/
@Override
public int getIconWidth() {
return icon.getIconWidth();
}
/**
* Gets the height of the bounding rectangle of this <CODE>AlphaIcon</CODE>.
** Returns the height of the wrapped icon.
*
* @return the height in pixels
*/
@Override
public int getIconHeight() {
return icon.getIconHeight();
}
}