KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > render > ShapeRenderer


1 package prefuse.render;
2
3 import java.awt.Shape JavaDoc;
4 import java.awt.geom.Ellipse2D JavaDoc;
5 import java.awt.geom.GeneralPath JavaDoc;
6 import java.awt.geom.Rectangle2D JavaDoc;
7
8 import prefuse.Constants;
9 import prefuse.visual.VisualItem;
10
11 /**
12  * Renderer for drawing simple shapes. This class provides a number of built-in
13  * shapes, selected by an integer value retrieved from a VisualItem.
14  *
15  * @author <a HREF="http://jheer.org">jeffrey heer</a>
16  */

17 public class ShapeRenderer extends AbstractShapeRenderer {
18
19     private int m_baseSize = 10;
20     
21     private Ellipse2D JavaDoc m_ellipse = new Ellipse2D.Double JavaDoc();
22     private Rectangle2D JavaDoc m_rect = new Rectangle2D.Double JavaDoc();
23     private GeneralPath JavaDoc m_path = new GeneralPath JavaDoc();
24
25     /**
26      * Creates a new ShapeRenderer with default base size of 10 pixels.
27      */

28     public ShapeRenderer() {
29     }
30     
31     /**
32      * Creates a new ShapeRenderer with given base size.
33      * @param size the base size in pixels
34      */

35     public ShapeRenderer(int size) {
36        setBaseSize(size);
37     }
38     
39     /**
40      * Sets the base size, in pixels, for shapes drawn by this renderer. The
41      * base size is the width and height value used when a VisualItem's size
42      * value is 1. The base size is scaled by the item's size value to arrive
43      * at the final scale used for rendering.
44      * @param size the base size in pixels
45      */

46     public void setBaseSize(int size) {
47         m_baseSize = size;
48     }
49     
50     /**
51      * Returns the base size, in pixels, for shapes drawn by this renderer.
52      * @return the base size in pixels
53      */

54     public int getBaseSize() {
55         return m_baseSize;
56     }
57     
58     /**
59      * @see prefuse.render.AbstractShapeRenderer#getRawShape(prefuse.visual.VisualItem)
60      */

61     protected Shape JavaDoc getRawShape(VisualItem item) {
62         int stype = item.getShape();
63         double x = item.getX();
64         if ( Double.isNaN(x) || Double.isInfinite(x) )
65             x = 0;
66         double y = item.getY();
67         if ( Double.isNaN(y) || Double.isInfinite(y) )
68             y = 0;
69         double width = m_baseSize*item.getSize();
70         
71         // Center the shape around the specified x and y
72
if ( width > 1 ) {
73             x = x-width/2;
74             y = y-width/2;
75         }
76         
77         switch ( stype ) {
78         case Constants.SHAPE_NONE:
79             return null;
80         case Constants.SHAPE_RECTANGLE:
81             return rectangle(x, y, width, width);
82         case Constants.SHAPE_ELLIPSE:
83             return ellipse(x, y, width, width);
84         case Constants.SHAPE_TRIANGLE_UP:
85             return triangle_up((float)x, (float)y, (float)width);
86         case Constants.SHAPE_TRIANGLE_DOWN:
87             return triangle_down((float)x, (float)y, (float)width);
88         case Constants.SHAPE_TRIANGLE_LEFT:
89             return triangle_left((float)x, (float)y, (float)width);
90         case Constants.SHAPE_TRIANGLE_RIGHT:
91             return triangle_right((float)x, (float)y, (float)width);
92         case Constants.SHAPE_CROSS:
93             return cross((float)x, (float)y, (float)width);
94         case Constants.SHAPE_STAR:
95             return star((float)x, (float)y, (float)width);
96         case Constants.SHAPE_HEXAGON:
97             return hexagon((float)x, (float)y, (float)width);
98         case Constants.SHAPE_DIAMOND:
99             return diamond((float)x, (float)y, (float)width);
100         default:
101             throw new IllegalStateException JavaDoc("Unknown shape type: "+stype);
102         }
103     }
104
105     /**
106      * Returns a rectangle of the given dimenisions.
107      */

108     public Shape JavaDoc rectangle(double x, double y, double width, double height) {
109         m_rect.setFrame(x, y, width, height);
110         return m_rect;
111     }
112
113     /**
114      * Returns an ellipse of the given dimenisions.
115      */

116     public Shape JavaDoc ellipse(double x, double y, double width, double height) {
117         m_ellipse.setFrame(x, y, width, height);
118         return m_ellipse;
119     }
120     
121     /**
122      * Returns a up-pointing triangle of the given dimenisions.
123      */

124     public Shape JavaDoc triangle_up(float x, float y, float height) {
125         m_path.reset();
126         m_path.moveTo(x,y+height);
127         m_path.lineTo(x+height/2, y);
128         m_path.lineTo(x+height, (y+height));
129         m_path.closePath();
130         return m_path;
131     }
132     
133     /**
134      * Returns a down-pointing triangle of the given dimenisions.
135      */

136     public Shape JavaDoc triangle_down(float x, float y, float height) {
137         m_path.reset();
138         m_path.moveTo(x,y);
139         m_path.lineTo(x+height, y);
140         m_path.lineTo(x+height/2, (y+height));
141         m_path.closePath();
142         return m_path;
143     }
144     
145     /**
146      * Returns a left-pointing triangle of the given dimenisions.
147      */

148     public Shape JavaDoc triangle_left(float x, float y, float height) {
149         m_path.reset();
150         m_path.moveTo(x+height, y);
151         m_path.lineTo(x+height, y+height);
152         m_path.lineTo(x, y+height/2);
153         m_path.closePath();
154         return m_path;
155     }
156     
157     /**
158      * Returns a right-pointing triangle of the given dimenisions.
159      */

160     public Shape JavaDoc triangle_right(float x, float y, float height) {
161         m_path.reset();
162         m_path.moveTo(x,y+height);
163         m_path.lineTo(x+height, y+height/2);
164         m_path.lineTo(x, y);
165         m_path.closePath();
166         return m_path;
167     }
168     
169     /**
170      * Returns a cross shape of the given dimenisions.
171      */

172     public Shape JavaDoc cross(float x, float y, float height) {
173         float h14 = 3*height/8, h34 = 5*height/8;
174         m_path.reset();
175         m_path.moveTo(x+h14, y);
176         m_path.lineTo(x+h34, y);
177         m_path.lineTo(x+h34, y+h14);
178         m_path.lineTo(x+height, y+h14);
179         m_path.lineTo(x+height, y+h34);
180         m_path.lineTo(x+h34, y+h34);
181         m_path.lineTo(x+h34, y+height);
182         m_path.lineTo(x+h14, y+height);
183         m_path.lineTo(x+h14, y+h34);
184         m_path.lineTo(x, y+h34);
185         m_path.lineTo(x, y+h14);
186         m_path.lineTo(x+h14, y+h14);
187         m_path.closePath();
188         return m_path;
189     }
190
191     /**
192      * Returns a star shape of the given dimenisions.
193      */

194     public Shape JavaDoc star(float x, float y, float height) {
195         float s = (float)(height/(2*Math.sin(Math.toRadians(54))));
196         float shortSide = (float)(height/(2*Math.tan(Math.toRadians(54))));
197         float mediumSide = (float)(s*Math.sin(Math.toRadians(18)));
198         float longSide = (float)(s*Math.cos(Math.toRadians(18)));
199         float innerLongSide = (float)(s/(2*Math.cos(Math.toRadians(36))));
200         float innerShortSide = innerLongSide*(float)Math.sin(Math.toRadians(36));
201         float innerMediumSide = innerLongSide*(float)Math.cos(Math.toRadians(36));
202
203         m_path.reset();
204         m_path.moveTo(x, y+shortSide);
205         m_path.lineTo((x+innerLongSide),(y+shortSide));
206         m_path.lineTo((x+height/2),y);
207         m_path.lineTo((x+height-innerLongSide),(y+shortSide));
208         m_path.lineTo((x+height),(y+shortSide));
209         m_path.lineTo((x+height-innerMediumSide),(y+shortSide+innerShortSide));
210         m_path.lineTo((x+height-mediumSide),(y+height));
211         m_path.lineTo((x+height/2),(y+shortSide+longSide-innerShortSide));
212         m_path.lineTo((x+mediumSide),(y+height));
213         m_path.lineTo((x+innerMediumSide),(y+shortSide+innerShortSide));
214         m_path.closePath();
215         return m_path;
216     }
217
218     /**
219      * Returns a hexagon shape of the given dimenisions.
220      */

221     public Shape JavaDoc hexagon(float x, float y, float height) {
222         float width = height/2;
223         
224         m_path.reset();
225         m_path.moveTo(x, y+0.5f*height);
226         m_path.lineTo(x+0.5f*width, y);
227         m_path.lineTo(x+1.5f*width, y);
228         m_path.lineTo(x+2.0f*width, y+0.5f*height);
229         m_path.lineTo(x+1.5f*width, y+height);
230         m_path.lineTo(x+0.5f*width, y+height);
231         m_path.closePath();
232         return m_path;
233     }
234
235     /**
236      * Returns a diamond shape of the given dimenisions.
237      */

238     public Shape JavaDoc diamond(float x, float y, float height) {
239         m_path.reset();
240         m_path.moveTo(x,(y+0.5f*height));
241         m_path.lineTo((x+0.5f*height),y);
242         m_path.lineTo((x+height),(y+0.5f*height));
243         m_path.lineTo((x+0.5f*height),(y+height));
244         m_path.closePath();
245         return m_path;
246     }
247     
248 } // end of class ShapeRenderer
249
Popular Tags