KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > render > PolygonRenderer


1 package prefuse.render;
2
3 import java.awt.Shape JavaDoc;
4 import java.awt.geom.GeneralPath JavaDoc;
5
6 import prefuse.Constants;
7 import prefuse.data.Schema;
8 import prefuse.util.GraphicsLib;
9 import prefuse.visual.VisualItem;
10
11
12 /**
13  * <p>Renderer for drawing a polygon, either as a closed shape, or as a
14  * series of potentially unclosed curves. VisualItems must have a data field
15  * containing an array of floats that tores the polyon. A {@link Float#NaN}
16  * value can be used to mark the end point of the polygon for float arrays
17  * larger than their contained points. By default, this renderer will
18  * create closed paths, joining the first and last points in the point
19  * array if necessary. The {@link #setClosePath(boolean)} method can be
20  * used to render open paths, such as poly-lines or poly-curves.</p>
21  *
22  * <p>A polygon edge type parameter (one of
23  * {@link prefuse.Constants#POLY_TYPE_LINE},
24  * {@link prefuse.Constants#POLY_TYPE_CURVE}, or
25  * {@link prefuse.Constants#POLY_TYPE_STACK}) determines how the
26  * edges of the polygon are drawn. The LINE type result in a standard polygon,
27  * with straight lines drawn between each sequential point. The CURVE type
28  * causes the edges of the polygon to be interpolated as a cardinal spline,
29  * giving a smooth blob-like appearance to the shape. The STACK type is similar
30  * to the curve type except that straight line segments (not curves) are used
31  * when the slope of the line between two adjacent points is zero or infinity.
32  * This is useful for drawing stacks of data with otherwise curved edges.</p>
33  *
34  * @author <a HREF="http://jheer.org">jeffrey heer</a>
35  */

36 public class PolygonRenderer extends AbstractShapeRenderer {
37
38     /**
39      * Default data field for storing polygon (float array) values.
40      */

41     public static final String JavaDoc POLYGON = "_polygon";
42     /**
43      * A Schema describing the polygon specification.
44      */

45     public static final Schema POLYGON_SCHEMA = new Schema();
46     static {
47         POLYGON_SCHEMA.addColumn(POLYGON, float[].class);
48     }
49     
50     private int m_polyType = Constants.POLY_TYPE_LINE;
51     private float m_slack = 0.08f;
52     private float m_epsilon = 0.1f;
53     private boolean m_closed = true;
54     private String JavaDoc m_polyfield = POLYGON;
55     
56     private GeneralPath JavaDoc m_path = new GeneralPath JavaDoc();
57     
58     /**
59      * Create a new PolygonRenderer supporting straight lines.
60      */

61     public PolygonRenderer() {
62         this(Constants.EDGE_TYPE_LINE);
63     }
64     
65     /**
66      * Create a new PolygonRenderer.
67      * @param polyType the polygon edge type, one of
68      * {@link prefuse.Constants#POLY_TYPE_LINE},
69      * {@link prefuse.Constants#POLY_TYPE_CURVE}, or
70      * {@link prefuse.Constants#POLY_TYPE_STACK}).
71      */

72     public PolygonRenderer(int polyType) {
73         m_polyType = polyType;
74     }
75
76     /**
77      * Get the polygon line type.
78      * @return the polygon edge type, one of
79      * {@link prefuse.Constants#POLY_TYPE_LINE},
80      * {@link prefuse.Constants#POLY_TYPE_CURVE}, or
81      * {@link prefuse.Constants#POLY_TYPE_STACK}).
82      */

83     public int getPolyType() {
84         return m_polyType;
85     }
86     
87     /**
88      * Set the polygon line type.
89      * @param polyType the polygon edge type, one of
90      * {@link prefuse.Constants#POLY_TYPE_LINE},
91      * {@link prefuse.Constants#POLY_TYPE_CURVE}, or
92      * {@link prefuse.Constants#POLY_TYPE_STACK}).
93      */

94     public void setPolyType(int polyType) {
95         if ( polyType < 0 || polyType >= Constants.POLY_TYPE_COUNT ) {
96             throw new IllegalArgumentException JavaDoc("Unknown edge type: "+polyType);
97         }
98         m_polyType = polyType;
99     }
100     
101     /**
102      * Indicates if this renderer uses a closed or open path. If true,
103      * the renderer will draw closed polygons, if false, the renderer will
104      * draw poly-lines or poly-curves.
105      * @return true if paths are closed, false otherwise.
106      */

107     public boolean isClosePath() {
108         return m_closed;
109     }
110
111     /**
112      * Sets if this renderer uses a closed or open path. If true,
113      * the renderer will draw closed polygons, if false, the renderer will
114      * @param closePath true to close paths, false otherwise.
115      */

116     public void setClosePath(boolean closePath) {
117         m_closed = closePath;
118     }
119
120     /**
121      * Gets the slack parameter for curved lines. The slack parameter
122      * determines how tightly the curves are string between the points
123      * of the polygon. A value of zero results in straight lines. Values
124      * near 0.1 (0.08 is the default) typically result in visible curvature
125      * that still follows the polygon boundary nicely.
126      * @return the curve slack parameter
127      */

128     public float getCurveSlack() {
129         return m_slack;
130     }
131
132     /**
133      * Sets the slack parameter for curved lines. The slack parameter
134      * determines how tightly the curves are string between the points
135      * of the polygon. A value of zero results in straight lines. Values
136      * near 0.1 (0.08 is the default) typically results in visible curvature
137      * that still follows the polygon boundary nicely.
138      * @param slack the curve slack parameter to use
139      */

140     public void setCurveSlack(float slack) {
141         m_slack = slack;
142     }
143
144     /**
145      * @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem)
146      */

147     protected Shape JavaDoc getRawShape(VisualItem item) {
148         float[] poly = (float[])item.get(m_polyfield);
149         if ( poly == null ) { return null; }
150         
151         float x = (float)item.getX();
152         float y = (float)item.getY();
153         
154         // initialize the path
155
m_path.reset();
156         m_path.moveTo(x+poly[0],y+poly[1]);
157         
158         if ( m_polyType == Constants.POLY_TYPE_LINE )
159         {
160             // create a polygon
161
for ( int i=2; i<poly.length; i+=2 ) {
162                 if ( Float.isNaN(poly[i]) ) break;
163                 m_path.lineTo(x+poly[i],y+poly[i+1]);
164             }
165         }
166         else if ( m_polyType == Constants.POLY_TYPE_CURVE )
167         {
168             // interpolate the polygon points with a cardinal spline
169
return GraphicsLib.cardinalSpline(m_path, poly,
170                     m_slack, m_closed, x, y);
171         }
172         else if ( m_polyType == Constants.POLY_TYPE_STACK )
173         {
174             // used curved lines, except for non-sloping segments
175
return GraphicsLib.stackSpline(m_path, poly,
176                     m_epsilon, m_slack, m_closed, x, y);
177         }
178         if ( m_closed ) m_path.closePath();
179         return m_path;
180     }
181
182 } // end of class PolygonRenderer
183
Popular Tags