KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > prefuse > util > StrokeLib


1 package prefuse.util;
2
3 import java.awt.BasicStroke JavaDoc;
4
5 import prefuse.util.collections.IntObjectHashMap;
6
7 /**
8  * Library maintaining a cache of drawing strokes and other useful stroke
9  * computation routines.
10  *
11  * @author <a HREF="http://jheer.org">jeffrey heer</a>
12  */

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     /** Dash pattern for a dotted line */
20     public static final float[] DOTS = new float[] { 1.0f, 2.0f };
21     /** Dash pattern for regular uniform dashes */
22     public static final float[] DASHES = new float[] { 5.0f, 5.0f };
23     /** Dash pattern for longer uniform dashes */
24     public static final float[] LONG_DASHES = new float[] { 10.0f, 10.0f };
25     
26     /**
27      * Get a square capped, miter joined, non-dashed stroke of the given width.
28      * @param width the requested stroke width
29      * @return the stroke
30      */

31     public static BasicStroke JavaDoc getStroke(float width) {
32         return getStroke(width,BasicStroke.CAP_SQUARE,BasicStroke.JOIN_MITER);
33     }
34     
35     /**
36      * Get a square capped, miter joined, dashed stroke with the given width
37      * and dashing attributes.
38      * @param width the requested stroke width
39      * @param dashes an array describing the alternation pattern of
40      * a dashed line. For example [5f, 3f] will create dashes of length 5
41      * with spaces of length 3 between them. A null value indicates no
42      * dashing.
43      * @return the stroke
44      * @see java.awt.BasicStroke
45      */

46     public static BasicStroke JavaDoc getStroke(float width, float[] dashes) {
47         return getStroke(width, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_MITER,
48                          10.0f, dashes, 0f);
49     }
50     
51     /**
52      * Get a non-dashed stroke of the given width, cap, and join
53      * @param width the requested stroke width
54      * @param cap the requested cap type, one of
55      * {@link java.awt.BasicStroke#CAP_BUTT},
56      * {@link java.awt.BasicStroke#CAP_ROUND}, or
57      * {@link java.awt.BasicStroke#CAP_SQUARE}
58      * @param join the requested join type, one of
59      * {@link java.awt.BasicStroke#JOIN_BEVEL},
60      * {@link java.awt.BasicStroke#JOIN_MITER}, or
61      * {@link java.awt.BasicStroke#JOIN_ROUND}
62      * @return the stroke
63      */

64     public static BasicStroke JavaDoc getStroke(float width, int cap, int join) {
65         return getStroke(width, cap, join, 10.0f, null, 0f);
66     }
67     
68     /**
69      * Get a dashed stroke of the given width, cap, join, miter limit,
70      * and dashing attributes.
71      * @param width the requested stroke width
72      * @param cap the requested cap type, one of
73      * {@link java.awt.BasicStroke#CAP_BUTT},
74      * {@link java.awt.BasicStroke#CAP_ROUND}, or
75      * {@link java.awt.BasicStroke#CAP_SQUARE}
76      * @param join the requested join type, one of
77      * {@link java.awt.BasicStroke#JOIN_BEVEL},
78      * {@link java.awt.BasicStroke#JOIN_MITER}, or
79      * {@link java.awt.BasicStroke#JOIN_ROUND}
80      * @param miterLimit the miter limit at which to bevel miter joins
81      * @param dashes an array describing the alternation pattern of
82      * a dashed line. For example [5f, 3f] will create dashes of length 5
83      * with spaces of length 3 between them. A null value indicates no
84      * dashing.
85      * @param dashPhase the phase or offset from which to begin the
86      * dash pattern
87      * @return the stroke
88      * @see java.awt.BasicStroke
89      */

90     public static BasicStroke JavaDoc 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 JavaDoc s = null;
95         if ( (s=(BasicStroke JavaDoc)strokeMap.get(key)) == null ) {
96             s = new BasicStroke JavaDoc(width, cap, join,
97                                 miterLimit, dashes, dashPhase);
98             strokeMap.put(key, s);
99             ++misses;
100         }
101         ++lookups;
102         return s;
103     }
104     
105     /**
106      * Compute a hash-key for stroke storage and lookup.
107      */

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     /**
125      * Get a stroke with the same properties as the given stroke, but with
126      * a modified width value.
127      * @param s the stroke to base the returned stroke on
128      * @param width the desired width of the derived stroke
129      * @return the derived Stroke
130      */

131     public static BasicStroke JavaDoc getDerivedStroke(BasicStroke JavaDoc 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     /**
142      * Get the number of cache misses to the Stroke object cache.
143      * @return the number of cache misses
144      */

145     public static int getCacheMissCount() {
146         return misses;
147     }
148     
149     /**
150      * Get the number of cache lookups to the Stroke object cache.
151      * @return the number of cache lookups
152      */

153     public static int getCacheLookupCount() {
154         return lookups;
155     }
156     
157     /**
158      * Clear the Stroke object cache.
159      */

160     public static void clearCache() {
161         strokeMap.clear();
162     }
163     
164 } // end of class StrokeLib
165
Popular Tags