1 package prefuse.render; 2 3 import java.awt.Shape ; 4 import java.awt.geom.Ellipse2D ; 5 import java.awt.geom.GeneralPath ; 6 import java.awt.geom.Rectangle2D ; 7 8 import prefuse.Constants; 9 import prefuse.visual.VisualItem; 10 11 17 public class ShapeRenderer extends AbstractShapeRenderer { 18 19 private int m_baseSize = 10; 20 21 private Ellipse2D m_ellipse = new Ellipse2D.Double (); 22 private Rectangle2D m_rect = new Rectangle2D.Double (); 23 private GeneralPath m_path = new GeneralPath (); 24 25 28 public ShapeRenderer() { 29 } 30 31 35 public ShapeRenderer(int size) { 36 setBaseSize(size); 37 } 38 39 46 public void setBaseSize(int size) { 47 m_baseSize = size; 48 } 49 50 54 public int getBaseSize() { 55 return m_baseSize; 56 } 57 58 61 protected Shape 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 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 ("Unknown shape type: "+stype); 102 } 103 } 104 105 108 public Shape rectangle(double x, double y, double width, double height) { 109 m_rect.setFrame(x, y, width, height); 110 return m_rect; 111 } 112 113 116 public Shape ellipse(double x, double y, double width, double height) { 117 m_ellipse.setFrame(x, y, width, height); 118 return m_ellipse; 119 } 120 121 124 public Shape 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 136 public Shape 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 148 public Shape 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 160 public Shape 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 172 public Shape 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 194 public Shape 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 221 public Shape 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 238 public Shape 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 } | Popular Tags |