View Javadoc
1   package edu.uci.ics.jung.visualization;
2   
3   import java.awt.Component;
4   import java.awt.Dimension;
5   import java.awt.Graphics;
6   import java.awt.Image;
7   import java.util.LinkedHashSet;
8   import java.util.Set;
9   
10  import javax.swing.Icon;
11  import javax.swing.ImageIcon;
12  
13  /**
14   * An icon that is made up of a collection of Icons.
15   * They are rendered in layers starting with the first
16   * Icon added (from the constructor).
17   * 
18   * @author Tom Nelson
19   *
20   */
21  @SuppressWarnings("serial")
22  public class LayeredIcon extends ImageIcon {
23  
24  	Set<Icon> iconSet = new LinkedHashSet<Icon>();
25  
26  	public LayeredIcon(Image image) {
27  	    super(image);
28  	}
29  
30  	public void paintIcon(Component c, Graphics g, int x, int y) {
31          super.paintIcon(c, g, x, y);
32          Dimension d = new Dimension(getIconWidth(), getIconHeight());
33  		for (Icon icon : iconSet) {
34  			Dimension id = new Dimension(icon.getIconWidth(), icon.getIconHeight());
35  			int dx = (d.width - id.width)/2;
36  			int dy = (d.height - id.height)/2;
37  			icon.paintIcon(c, g, x+dx, y+dy);
38  		}
39  	}
40  
41  	public void add(Icon icon) {
42  		iconSet.add(icon);
43  	}
44  
45  	public boolean remove(Icon icon) {
46  		return iconSet.remove(icon);
47  	}
48  }