Java Reference
In-Depth Information
poly.addPoint(width, halfHeight);
poly.addPoint(halfWidth, height);
}
public int getIconHeight() {
return height;
}
public int getIconWidth() {
return width;
}
public void paintIcon(Component c, Graphics g, int x, int y) {
g.setColor(color);
g.translate(x, y);
if (selected) {
g.fillPolygon(poly);
} else {
g.drawPolygon(poly);
}
g.translate(-x, -y);
}
}
Using an Icon
Once you have your Icon implementation, using the Icon is as simple as finding a component
with an appropriate property. For example, here's the icon with a JLabel :
Icon icon = new DiamondIcon(Color.RED, true, 25, 25);
JLabel label = new JLabel(icon);
Figure 4-10 shows what such a label might look like.
Figure 4-10. Using an Icon in a JLabel
ImageIcon Class
The ImageIcon class presents an implementation of the Icon interface for creating glyphs from
AWT Image objects, whether from memory (a byte[ ] ), off a disk (a file name), or over the
network (a URL ). Unlike with regular Image objects, the loading of an ImageIcon is immediately
started when the ImageIcon is created, though it might not be fully loaded when used. In addition,
 
Search WWH ::




Custom Search