KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > render > AbstractShapeRenderer


1 package prefuse.render;
2
3 import java.awt.BasicStroke JavaDoc;
4 import java.awt.Graphics2D JavaDoc;
5 import java.awt.Shape JavaDoc;
6 import java.awt.geom.AffineTransform JavaDoc;
7 import java.awt.geom.Point2D JavaDoc;
8
9 import prefuse.util.GraphicsLib;
10 import prefuse.visual.VisualItem;
11
12
13 /**
14  * <p>Abstract base class implementation of the Renderer interface for
15  * supporting the drawing of basic shapes. Subclasses should override the
16  * {@link #getRawShape(VisualItem) getRawShape} method,
17  * which returns the shape to draw. Optionally, subclasses can also override the
18  * {@link #getTransform(VisualItem) getTransform} method to apply a desired
19  * <code>AffineTransform</code> to the shape.</p>
20  *
21  * <p><b>NOTE:</b> For more efficient rendering, subclasses should use a
22  * single shape instance in memory, and update its parameters on each call
23  * to getRawShape, rather than allocating a new Shape object each time.
24  * Otherwise, a new object will be allocated every time something needs to
25  * be drawn, and then subsequently be arbage collected. This can significantly
26  * reduce performance, especially when there are many things to draw.
27  * </p>
28  *
29  * @version 1.0
30  * @author alan newberger
31  * @author <a HREF="http://jheer.org">jeffrey heer</a>
32  */

33 public abstract class AbstractShapeRenderer implements Renderer {
34     
35     public static final int RENDER_TYPE_NONE = 0;
36     public static final int RENDER_TYPE_DRAW = 1;
37     public static final int RENDER_TYPE_FILL = 2;
38     public static final int RENDER_TYPE_DRAW_AND_FILL = 3;
39
40     private int m_renderType = RENDER_TYPE_DRAW_AND_FILL;
41     protected AffineTransform JavaDoc m_transform = new AffineTransform JavaDoc();
42     protected boolean m_manageBounds = true;
43     
44     public void setManageBounds(boolean b) {
45         m_manageBounds = b;
46     }
47     
48     /**
49      * @see prefuse.render.Renderer#render(java.awt.Graphics2D, prefuse.visual.VisualItem)
50      */

51     public void render(Graphics2D JavaDoc g, VisualItem item) {
52         Shape JavaDoc shape = getShape(item);
53         if (shape != null)
54             drawShape(g, item, shape);
55     }
56     
57     /**
58      * Draws the specified shape into the provided Graphics context, using
59      * stroke and fill color values from the specified VisualItem. This method
60      * can be called by subclasses in custom rendering routines.
61      */

62     protected void drawShape(Graphics2D JavaDoc g, VisualItem item, Shape JavaDoc shape) {
63         GraphicsLib.paint(g, item, shape, getStroke(item), getRenderType(item));
64     }
65
66     /**
67      * Returns the shape describing the boundary of an item. The shape's
68      * coordinates should be in abolute (item-space) coordinates.
69      * @param item the item for which to get the Shape
70      */

71     public Shape JavaDoc getShape(VisualItem item) {
72         AffineTransform JavaDoc at = getTransform(item);
73         Shape JavaDoc rawShape = getRawShape(item);
74         return (at==null || rawShape==null ? rawShape
75                  : at.createTransformedShape(rawShape));
76     }
77
78     /**
79      * Retursn the stroke to use for drawing lines and shape outlines. By
80      * default returns the value of {@link VisualItem#getStroke()}.
81      * Subclasses can override this method to implement custom stroke
82      * assignment, though changing the <code>VisualItem</code>'s stroke
83      * value is preferred.
84      * @param item the VisualItem
85      * @return the strok to use for drawing lines and shape outlines
86      */

87     protected BasicStroke JavaDoc getStroke(VisualItem item) {
88         return item.getStroke();
89     }
90     
91     /**
92      * Return a non-transformed shape for the visual representation of the
93      * item. Subclasses must implement this method.
94      * @param item the VisualItem being drawn
95      * @return the "raw", untransformed shape.
96      */

97     protected abstract Shape JavaDoc getRawShape(VisualItem item);
98     
99     /**
100      * Return the graphics space transform applied to this item's shape, if any.
101      * Subclasses can implement this method, otherwise it will return null
102      * to indicate no transformation is needed.
103      * @param item the VisualItem
104      * @return the graphics space transform, or null if none
105      */

106     protected AffineTransform JavaDoc getTransform(VisualItem item) {
107         return null;
108     }
109
110     /**
111      * Returns a value indicating if a shape is drawn by its outline, by a
112      * fill, or both. The default is to draw both.
113      * @return the rendering type
114      */

115     public int getRenderType(VisualItem item) {
116         return m_renderType;
117     }
118     
119     /**
120      * Sets a value indicating if a shape is drawn by its outline, by a fill,
121      * or both. The default is to draw both.
122      * @param type the new rendering type. Should be one of
123      * {@link #RENDER_TYPE_NONE}, {@link #RENDER_TYPE_DRAW},
124      * {@link #RENDER_TYPE_FILL}, or {@link #RENDER_TYPE_DRAW_AND_FILL}.
125      */

126     public void setRenderType(int type) {
127         if ( type < RENDER_TYPE_NONE || type > RENDER_TYPE_DRAW_AND_FILL ) {
128             throw new IllegalArgumentException JavaDoc("Unrecognized render type.");
129         }
130         m_renderType = type;
131     }
132     
133     /**
134      * @see prefuse.render.Renderer#locatePoint(java.awt.geom.Point2D, prefuse.visual.VisualItem)
135      */

136     public boolean locatePoint(Point2D JavaDoc p, VisualItem item) {
137         if ( item.getBounds().contains(p) ) {
138             // if within bounds, check within shape outline
139
Shape JavaDoc s = getShape(item);
140             return (s != null ? s.contains(p) : false);
141         } else {
142             return false;
143         }
144     }
145
146     /**
147      * @see prefuse.render.Renderer#setBounds(prefuse.visual.VisualItem)
148      */

149     public void setBounds(VisualItem item) {
150         if ( !m_manageBounds ) return;
151         Shape JavaDoc shape = getShape(item);
152         if ( shape == null ) {
153             item.setBounds(item.getX(), item.getY(), 0, 0);
154         } else {
155             GraphicsLib.setBounds(item, shape, getStroke(item));
156         }
157     }
158
159 } // end of abstract class AbstractShapeRenderer
160
Popular Tags