1    /*
2    Copyright 2000 by Ralph Hartley
3    This software is licenced under the terms of the
4    Gnu Public Licence
5    */
6    import java.awt.*;
7    import java.awt.geom.*;
8    import java.util.*;
9    
10   
11   public class Doted extends SavableStroke {
12     static double tol  = 0.5;
13   
14     static double cfact = Math.sqrt(2);
15   
16     public double thickness;
17     public double ticklength;
18   
19     public Doted(double thickness,double ticklength) {
20       super(thickness);
21       this.thickness = thickness;
22       this.ticklength = ticklength;
23     }
24   
25     public Shape createStrokedShape(Shape p) {
26         //System.out.println("shape = "+p);
27       PathIterator path = p.getPathIterator(new AffineTransform(),tol);
28       GeneralPath res = new GeneralPath();
29   
30       //System.out.println("res = "+res+" path = "+path);
31   
32       double[] points;// = null;
33       double[] current = null;
34       int type;
35       double[] start = null;
36   
37       double dist;
38   
39       double[] anchor;
40   
41       double phase = ticklength;
42   
43       //System.out.println("path "+path);
44       for (;!path.isDone();path.next()) {
45           //System.out.println("done= "+path.isDone());
46         points = new double[2];
47         type = path.currentSegment(points);
48         //System.out.println("Segment "+type);
49         switch (type) {
50         case PathIterator.SEG_MOVETO:
51             res.moveTo((float)points[0],(float)points[1]);
52             start = points;
53             current = points;
54             break;
55         case PathIterator.SEG_CLOSE:
56             points = start;
57         case PathIterator.SEG_LINETO:
58             dist = Math.sqrt((points[0]-current[0])*(points[0]-current[0]) +
59                             (points[1]-current[1])*(points[1]-current[1]));
60             while (phase < dist) {
61               anchor = new double[2];
62               anchor[0] = current[0] + phase*(points[0]-current[0])/dist;
63               anchor[1] = current[1] + phase*(points[1]-current[1])/dist;
64   
65               res.moveTo((float)(anchor[0]-thickness),(float)anchor[1]);
66   
67               res.curveTo((float)(anchor[0]-thickness),(float)(anchor[1]+thickness*cfact),
68           		(float)(anchor[0]+thickness),(float)(anchor[1]+thickness*cfact),
69           		(float)(anchor[0]+thickness),(float)anchor[1]);
70   
71               res.curveTo((float)(anchor[0]+thickness),(float)(anchor[1]-thickness*cfact),
72           		(float)(anchor[0]-thickness),(float)(anchor[1]-thickness*cfact),
73           		(float)(anchor[0]-thickness),(float)anchor[1]);
74   
75                     dist -= phase;
76               phase = ticklength;
77               current = anchor;
78             }
79             //System.out.println("backbone done");
80   
81             if (dist>0) {
82               phase -= dist;
83               current = points;
84                   }
85         }
86       }
87       
88       return(res);
89     }
90   }
91