1 package org.glassbox.graphview;
2
3 import java.awt.*;
4 import java.awt.geom.AffineTransform;
5 import javax.swing.ImageIcon;
6
7 import de.fzi.wim.guibase.graphview.graph.*;
8 import de.fzi.wim.guibase.graphview.view.*;
9
10 /***
11 An abstract implementation of a NodePainter which paints an image for each node.
12 The size of the node ist determined by the according image.
13 Only methods that have to be implemented are <code>ImageIcon getImageForNode(Node)</code> to
14 decide which image should be painted for a node and <code>Collection getAllImages()</code>
15 to receive all images used by this painter.
16 */
17 public abstract class AbstractImageNodePainter implements ZoomableNodePainter {
18 private Dimension _imagesize;
19 private Rectangle _bounds;
20 private AffineTransform _transform;
21 private Insets _border;
22 private double _zf;
23 private double _w, _h;
24 private double _threshold;
25
26
27 /***
28 Constructor. Uses 0 as threshold (images are always painted).
29 @param imagesize The size of the images.
30 @param insets The insets of this painter (empty space around the images).
31 */
32 protected AbstractImageNodePainter(Dimension imagesize, Insets border) {
33 this(imagesize, border, 0d);
34 }
35
36 /***
37 Constructor.
38 @param imagesize The size of the images.
39 @param insets The insets of this painter (empty space around the images).
40 @param zoomthreshold The minimum zoom value to paint images.
41 */
42 protected AbstractImageNodePainter(Dimension imagesize, Insets border, double zoomthreshold) {
43 _imagesize = imagesize;
44 _bounds = new Rectangle();
45 _transform = new AffineTransform();
46 _border = border;
47 _threshold = zoomthreshold;
48 setZoomFactor(1d);
49 }
50
51 /***
52 This method has to be implemented by subclasses to
53 decide which image should be painted for a node.
54 */
55 protected abstract ImageIcon getImageForNode(Node node);
56
57 public void setZoomFactor(double zf) {
58 _zf = zf;
59 _w = ((double)(_imagesize.width + _border.left + _border.right)) * _zf;
60 _h = ((double)(_imagesize.height + _border.top + _border.bottom)) * _zf;
61 }
62
63 public void paintNode(JGraphPane graphpane,Graphics2D g,Node node) {
64 if (_zf > _threshold) {
65 ImageIcon image = getImageForNode(node);
66
67 if (image != null) {
68 Point p = graphpane.getScreenPointForNode(node);
69
70 double tx = p.x - ((double)_imagesize.width) * _zf / 2;
71 double ty = p.y - ((double)_imagesize.height) * _zf / 2;
72
73 double sx = ((double) _imagesize.width) / ((double) image.getIconWidth()) * _zf;
74 double sy = ((double) _imagesize.height) / ((double) image.getIconHeight()) * _zf;
75
76 _transform.setTransform(sx, 0d, 0d, sy, tx, ty);
77
78 g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
79 g.drawImage(image.getImage(), _transform, image.getImageObserver());
80 }
81 }
82 }
83
84 public boolean isInNode(JGraphPane graphpane,Node node,Point point) {
85 getNodeScreenBounds(graphpane, node, _bounds);
86 return _bounds.contains(point);
87 }
88
89 public void getNodeScreenBounds(JGraphPane graphpane, Node node, Rectangle nodebounds) {
90 Point p = graphpane.getScreenPointForNode(node);
91 nodebounds.setBounds((int)(p.x - _w / 2d),
92 (int)(p.y - _h / 2d),
93 (int)_w,
94 (int)_h);
95 }
96
97 public String getToolTipText(JGraphPane graphpane,Node node,Point point) {
98 return null;
99 }
100 }