1 package prefuse.util; 2 3 import java.awt.BasicStroke ; 4 5 import prefuse.util.collections.IntObjectHashMap; 6 7 13 public class StrokeLib { 14 15 private static final IntObjectHashMap strokeMap = new IntObjectHashMap(); 16 private static int misses = 0; 17 private static int lookups = 0; 18 19 20 public static final float[] DOTS = new float[] { 1.0f, 2.0f }; 21 22 public static final float[] DASHES = new float[] { 5.0f, 5.0f }; 23 24 public static final float[] LONG_DASHES = new float[] { 10.0f, 10.0f }; 25 26 31 public static BasicStroke getStroke(float width) { 32 return getStroke(width,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER); 33 } 34 35 46 public static BasicStroke getStroke(float width, float[] dashes) { 47 return getStroke(width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER, 48 10.0f, dashes, 0f); 49 } 50 51 64 public static BasicStroke getStroke(float width, int cap, int join) { 65 return getStroke(width, cap, join, 10.0f, null, 0f); 66 } 67 68 90 public static BasicStroke getStroke(float width, int cap, int join, 91 float miterLimit, float[] dashes, float dashPhase) 92 { 93 int key = getStrokeKey(width,cap,join,miterLimit,dashes,dashPhase); 94 BasicStroke s = null; 95 if ( (s=(BasicStroke )strokeMap.get(key)) == null ) { 96 s = new BasicStroke (width, cap, join, 97 miterLimit, dashes, dashPhase); 98 strokeMap.put(key, s); 99 ++misses; 100 } 101 ++lookups; 102 return s; 103 } 104 105 108 protected static int getStrokeKey(float width, int cap, int join, 109 float miterLimit, float[] dashes, float dashPhase) 110 { 111 int hash = Float.floatToIntBits(width); 112 hash = hash * 31 + join; 113 hash = hash * 31 + cap; 114 hash = hash * 31 + Float.floatToIntBits(miterLimit); 115 if (dashes != null) { 116 hash = hash * 31 + Float.floatToIntBits(dashPhase); 117 for (int i = 0; i < dashes.length; i++) { 118 hash = hash * 31 + Float.floatToIntBits(dashes[i]); 119 } 120 } 121 return hash; 122 } 123 124 131 public static BasicStroke getDerivedStroke(BasicStroke s, float width) { 132 if ( s.getLineWidth() == width ) { 133 return s; 134 } else { 135 return getStroke(width*s.getLineWidth(), s.getEndCap(), 136 s.getLineJoin(), s.getMiterLimit(), 137 s.getDashArray(), s.getDashPhase()); 138 } 139 } 140 141 145 public static int getCacheMissCount() { 146 return misses; 147 } 148 149 153 public static int getCacheLookupCount() { 154 return lookups; 155 } 156 157 160 public static void clearCache() { 161 strokeMap.clear(); 162 } 163 164 } | Popular Tags |