1
2
3
4
5
6
7
8
9
10
11 package edu.uci.ics.jung.visualization.decorators;
12
13 import java.awt.Image;
14 import java.awt.Shape;
15 import java.awt.geom.AffineTransform;
16 import java.util.HashMap;
17 import java.util.Map;
18
19 import javax.swing.Icon;
20 import javax.swing.ImageIcon;
21
22 import com.google.common.base.Function;
23
24 import edu.uci.ics.jung.visualization.util.ImageShapeUtils;
25
26
27
28
29
30
31
32
33 public class VertexIconShapeTransformer<V> implements Function<V,Shape> {
34 protected Map<Image, Shape> shapeMap = new HashMap<Image, Shape>();
35 protected Map<V, Icon> iconMap;
36 protected Function<V, Shape> delegate;
37
38
39
40
41
42 public VertexIconShapeTransformer(Function<V, Shape> delegate) {
43 this.delegate = delegate;
44 }
45
46
47
48
49 public Function<V,Shape> getDelegate() {
50 return delegate;
51 }
52
53
54
55
56 public void setDelegate(Function<V,Shape> delegate) {
57 this.delegate = delegate;
58 }
59
60
61
62
63
64 public Shape apply(V v) {
65 Icon icon = iconMap.get(v);
66 if (icon != null && icon instanceof ImageIcon) {
67 Image image = ((ImageIcon) icon).getImage();
68 Shape shape = (Shape) shapeMap.get(image);
69 if (shape == null) {
70 shape = ImageShapeUtils.getShape(image, 30);
71 if(shape.getBounds().getWidth() > 0 &&
72 shape.getBounds().getHeight() > 0) {
73
74
75 int width = image.getWidth(null);
76 int height = image.getHeight(null);
77 AffineTransform transform = AffineTransform
78 .getTranslateInstance(-width / 2, -height / 2);
79 shape = transform.createTransformedShape(shape);
80 shapeMap.put(image, shape);
81 }
82 }
83 return shape;
84 } else {
85 return delegate.apply(v);
86 }
87 }
88
89
90
91
92 public Map<V, Icon> getIconMap() {
93 return iconMap;
94 }
95
96
97
98
99 public void setIconMap(Map<V, Icon> iconMap) {
100 this.iconMap = iconMap;
101 }
102
103
104
105
106 public Map<Image, Shape> getShapeMap() {
107 return shapeMap;
108 }
109
110
111
112
113 public void setShapeMap(Map<Image, Shape> shapeMap) {
114 this.shapeMap = shapeMap;
115 }
116 }