KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > svggen > BStroke


1 /*
2
3    Copyright 2001 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.svggen;
19
20 import java.awt.*;
21 import java.awt.geom.*;
22
23 /**
24  * This test validates convertion of BasicStroke
25  *
26  * @author <a HREF="mailto:cjolif@ilog.fr">Christophe Jolif</a>
27  * @author <a HREF="mailto:vhardy@eng.sun.com">Vincent Hardy</a>
28  * @version $Id: BStroke.java,v 1.3 2004/08/18 07:16:43 vhardy Exp $
29  */

30 public class BStroke implements Painter {
31     public void paint(Graphics2D g) {
32         g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
33                            RenderingHints.VALUE_ANTIALIAS_ON);
34
35         /*
36          * Strokes of varying width
37          */

38         java.awt.BasicStroke JavaDoc strokesWidth[] = {
39             new java.awt.BasicStroke JavaDoc(2.f),
40             new java.awt.BasicStroke JavaDoc(4.f),
41             new java.awt.BasicStroke JavaDoc(8.f),
42             new java.awt.BasicStroke JavaDoc(16.f)
43                 };
44
45
46         /*
47          * Strokes of varying termination styles
48          */

49         java.awt.BasicStroke JavaDoc strokesCap[] = {
50             new java.awt.BasicStroke JavaDoc(15.f, java.awt.BasicStroke.CAP_BUTT, java.awt.BasicStroke.JOIN_BEVEL), // No decoration
51
new java.awt.BasicStroke JavaDoc(15.f, java.awt.BasicStroke.CAP_SQUARE, java.awt.BasicStroke.JOIN_BEVEL), // Square end
52
new java.awt.BasicStroke JavaDoc(15.f, java.awt.BasicStroke.CAP_ROUND, java.awt.BasicStroke.JOIN_BEVEL), // Rounded end
53
};
54
55         /*
56          * Strokes of varying segment connection styles
57          */

58         java.awt.BasicStroke JavaDoc strokesJoin[] = {
59             new java.awt.BasicStroke JavaDoc(10.f, java.awt.BasicStroke.CAP_SQUARE, java.awt.BasicStroke.JOIN_BEVEL), // Connected with a straight segment
60
new java.awt.BasicStroke JavaDoc(10.f, java.awt.BasicStroke.CAP_SQUARE, java.awt.BasicStroke.JOIN_MITER), // Extend outlines until they meet
61
new java.awt.BasicStroke JavaDoc(10.f, java.awt.BasicStroke.CAP_SQUARE, java.awt.BasicStroke.JOIN_ROUND), // Round of corner.
62
};
63         /*
64          * Strokes of varying miterlimits
65          */

66         java.awt.BasicStroke JavaDoc strokesMiter[] = {
67             new java.awt.BasicStroke JavaDoc(6.f, java.awt.BasicStroke.CAP_SQUARE, java.awt.BasicStroke.JOIN_MITER, 1), // Actually cuts of all angles
68
new java.awt.BasicStroke JavaDoc(6.f, java.awt.BasicStroke.CAP_SQUARE, java.awt.BasicStroke.JOIN_MITER, 2f), // Cuts off angles less than 60degrees
69
new java.awt.BasicStroke JavaDoc(6.f, java.awt.BasicStroke.CAP_SQUARE, java.awt.BasicStroke.JOIN_MITER, 10f), // Cuts off angles less than 11 degrees
70
};
71
72         /*
73          * Srokes with varying dash styles
74          */

75         java.awt.BasicStroke JavaDoc strokesDash[] = {
76             new java.awt.BasicStroke JavaDoc(8.f,
77                             java.awt.BasicStroke.CAP_BUTT,
78                             java.awt.BasicStroke.JOIN_BEVEL,
79                             8.f,
80                             new float[]{ 6.f, 6.f },
81                             0.f),
82
83             new java.awt.BasicStroke JavaDoc(8.f,
84                             java.awt.BasicStroke.CAP_BUTT,
85                             java.awt.BasicStroke.JOIN_BEVEL,
86                             8.f,
87                             new float[]{ 10.f, 4.f },
88                             0.f),
89
90             new java.awt.BasicStroke JavaDoc(8.f,
91                             java.awt.BasicStroke.CAP_BUTT,
92                             java.awt.BasicStroke.JOIN_BEVEL,
93                             8.f,
94                             new float[]{ 4.f, 4.f, 10.f, 4.f },
95                             0.f),
96
97             new java.awt.BasicStroke JavaDoc(8.f,
98                             java.awt.BasicStroke.CAP_BUTT,
99                             java.awt.BasicStroke.JOIN_BEVEL,
100                             8.f,
101                             new float[]{ 4.f, 4.f, 10.f, 4.f },
102                             4.f)
103                 };
104
105         java.awt.geom.AffineTransform JavaDoc defaultTransform = g.getTransform();
106
107         // Varying width
108
g.setPaint(Color.black);
109         g.drawString("Varying width", 10, 10);
110         for(int i=0; i<strokesWidth.length; i++){
111             g.setStroke(strokesWidth[i]);
112             g.drawLine(10, 30, 10, 80);
113             g.translate(20, 0);
114         }
115
116         // Varying end caps
117
g.setTransform(defaultTransform);
118         g.translate(0, 120);
119         g.drawString("Varying end caps", 10, 10);
120         for(int i=0; i<strokesCap.length; i++){
121             g.setStroke(strokesCap[i]);
122             g.drawLine(15, 30, 15, 80);
123             g.translate(30, 0);
124         }
125
126         // Varying line joins
127
GeneralPath needle = new GeneralPath();
128         needle.moveTo(0, 60);
129         needle.lineTo(10, 20);
130         needle.lineTo(20, 60);
131         g.setTransform(defaultTransform);
132         g.translate(0, 240);
133         g.drawString("Varying line joins", 10, 10);
134         g.translate(20, 20);
135         for(int i=0; i<strokesJoin.length; i++){
136             g.setStroke(strokesJoin[i]);
137             g.draw(needle);
138             g.translate(35, 0);
139         }
140
141         // Varying miter limit
142
g.setTransform(defaultTransform);
143         g.translate(150, 120);
144         GeneralPath miterShape = new GeneralPath();
145         miterShape.moveTo(0, 0);
146         miterShape.lineTo(30, 0);
147         miterShape.lineTo(30, 60); // 90 degree elbow
148
miterShape.lineTo(0, 30); // 45 degree elbow.
149
g.drawString("Varying miter limit", 10, 10);
150         g.translate(10, 30);
151         for(int i=0; i<strokesMiter.length; i++){
152             g.setStroke(strokesMiter[i]);
153             g.draw(miterShape);
154             g.translate(40, 0);
155         }
156
157         // Varing dashing patterns
158
g.setTransform(defaultTransform);
159         g.translate(150, 0);
160         g.drawString("Varying dash patterns", 10, 10);
161         g.translate(20, 0);
162         for(int i=0; i<strokesDash.length; i++){
163             g.setStroke(strokesDash[i]);
164             g.drawLine(10, 20, 10, 80);
165             g.translate(20, 0);
166         }
167
168     }
169 }
170
Popular Tags