View Javadoc

1   // University of California, Santa Cruz, USA and
2   // CERN, Geneva, Switzerland, Copyright (c) 2000
3   package org.freehep.graphicsio.test;
4   
5   import java.awt.BasicStroke;
6   import java.awt.Color;
7   import java.awt.Dimension;
8   import java.awt.Graphics;
9   import java.awt.Insets;
10  import java.awt.Stroke;
11  import java.awt.geom.AffineTransform;
12  import java.awt.geom.GeneralPath;
13  
14  import org.freehep.graphics2d.VectorGraphics;
15  
16  /**
17   * @author Charles Loomis
18   * @version $Id: TestLineStyles.java 8584 2006-08-10 23:06:37Z duns $
19   */
20  public class TestLineStyles extends TestingPanel {
21  
22      static float[] dash1 = new float[2];
23      static {
24          dash1[0] = 5.f;
25          dash1[1] = 2.f;
26      }
27  
28      static float[] dash2 = new float[2];
29      static {
30          dash2[0] = 0.f;
31          dash2[1] = 7.f;
32      }
33  
34      static float[] dash3 = new float[4];
35      static {
36          dash3[0] = 10.f;
37          dash3[1] = 5.f;
38          dash3[2] = 2.f;
39          dash3[3] = 5.f;
40      }
41  
42      static Stroke[][] strokes = new Stroke[3][3];
43  
44      static {
45          strokes[0][0] = new BasicStroke(3.f, BasicStroke.CAP_BUTT,
46                  BasicStroke.JOIN_BEVEL, 10.f, dash1, 0.f);
47          strokes[0][1] = new BasicStroke(5.f, BasicStroke.CAP_BUTT,
48                  BasicStroke.JOIN_BEVEL, 10.f, dash1, 0.f);
49          strokes[0][2] = new BasicStroke(20.f, BasicStroke.CAP_BUTT,
50                  BasicStroke.JOIN_BEVEL, 10.f);
51          strokes[1][0] = new BasicStroke(3.f, BasicStroke.CAP_ROUND,
52                  BasicStroke.JOIN_MITER, 10.f, dash2, 0.f);
53          strokes[1][1] = new BasicStroke(5.f, BasicStroke.CAP_ROUND,
54                  BasicStroke.JOIN_MITER, 10.f, dash2, 0.f);
55          strokes[1][2] = new BasicStroke(20.f, BasicStroke.CAP_ROUND,
56                  BasicStroke.JOIN_MITER, 10.f);
57          strokes[2][0] = new BasicStroke(3.f, BasicStroke.CAP_SQUARE,
58                  BasicStroke.JOIN_ROUND, 10.f, dash3, 0.f);
59          strokes[2][1] = new BasicStroke(5.f, BasicStroke.CAP_SQUARE,
60                  BasicStroke.JOIN_ROUND, 10.f, dash3, 0.f);
61          strokes[2][2] = new BasicStroke(20.f, BasicStroke.CAP_SQUARE,
62                  BasicStroke.JOIN_ROUND, 10.f);
63      }
64  
65      public TestLineStyles(String[] args) throws Exception {
66          super(args);
67          setName("Line Styles");
68      }
69  
70      public void paintComponent(Graphics g) {
71  
72          if (g != null) {
73  
74              VectorGraphics vg = VectorGraphics.create(g);
75  
76              Dimension dim = getSize();
77              Insets insets = getInsets();
78  
79              vg.setColor(Color.white);
80              vg.fillRect(insets.left, insets.top, dim.width - insets.left
81                      - insets.right, dim.height - insets.top - insets.bottom);
82  
83              int dw = dim.width / 3;
84              int dh = dim.height / 3;
85  
86              int size = (Math.min(dw, dh) * 2 / 3) / 2;
87  
88              vg.setColor(Color.black);
89  
90              // thinnest line possible
91              vg.setStroke(new BasicStroke(0.0f));
92              vg.drawLine(0, dim.height / 40, dim.width, dim.height / 40);
93  
94              GeneralPath sshape = new GeneralPath();
95              sshape.moveTo(size, size);
96              sshape.lineTo(-size, size);
97              sshape.lineTo(-size, size / 2);
98              sshape.lineTo(size, size / 2);
99              sshape.lineTo(size, -size / 2);
100             sshape.lineTo(-size, -size / 2);
101             sshape.lineTo(-size, -size);
102             sshape.lineTo(size, -size);
103 
104             for (int iy = 0; iy < 3; iy++) {
105                 int y = iy * dh + dh / 2;
106                 for (int ix = 0; ix < 3; ix++) {
107                     int x = ix * dw + dw / 2;
108 
109                     vg.setStroke(strokes[ix][iy]);
110 
111                     AffineTransform xform = AffineTransform
112                             .getTranslateInstance((double) x, (double) y);
113                     vg.draw(sshape.createTransformedShape(xform));
114                 }
115             }
116 
117         }
118     }
119 
120     public static void main(String[] args) throws Exception {
121         new TestLineStyles(args).runTest();
122     }
123 }