KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Lines > Dash


1 /*
2  * @(#)Dash.java 1.29 06/08/29
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)Dash.java 1.29 06/08/29
39  */

40
41 package java2d.demos.Lines;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.geom.*;
46 import java.awt.font.TextLayout JavaDoc;
47 import java.awt.font.FontRenderContext JavaDoc;
48 import java2d.Surface;
49
50
51 /**
52  * Various shapes stroked with a dashing pattern.
53  */

54 public class Dash extends Surface {
55
56     public Dash() {
57         setBackground(WHITE);
58     }
59
60
61     public void render(int w, int h, Graphics2D g2) {
62         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
63         Font font = g2.getFont();
64         TextLayout JavaDoc tl = new TextLayout JavaDoc("Dashes", font, frc);
65         float sw = (float) tl.getBounds().getWidth();
66         float sh = (float) tl.getAscent() + tl.getDescent();
67         g2.setColor(BLACK);
68         tl.draw(g2, (float) (w/2-sw/2), sh+5);
69
70         BasicStroke dotted = new BasicStroke(3, BasicStroke.CAP_ROUND,
71                      BasicStroke.JOIN_ROUND, 0, new float[]{0,6,0,6}, 0);
72         g2.setStroke(dotted);
73         g2.drawRect(3,3,w-6,h-6);
74
75         int x = 0; int y = h-34;
76         BasicStroke bs[] = new BasicStroke[6];
77
78         float j = 1.1f;
79         for (int i = 0; i < bs.length; i++, j += 1.0f) {
80             float dash[] = { j };
81             BasicStroke b = new BasicStroke(1.0f, BasicStroke.CAP_BUTT,
82                                 BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
83             g2.setStroke(b);
84             g2.drawLine(20, y, w-20, y);
85             bs[i] = new BasicStroke(3.0f, BasicStroke.CAP_BUTT,
86                                 BasicStroke.JOIN_MITER, 10.0f, dash, 0.0f);
87             y += 5;
88         }
89
90         Shape shape = null;
91         y = 0;
92         for (int i = 0; i < 6; i++) {
93             x = (i == 0 || i == 3) ? (w/3-w/5)/2 : x + w/3;
94             y = (i <= 2) ? (int) sh+h/12 : h/2;
95
96             g2.setStroke(bs[i]);
97             g2.translate(x, y);
98             switch (i) {
99                 case 0 : shape = new Arc2D.Float(0.0f, 0.0f, w/5, h/4, 45, 270, Arc2D.PIE);
100                          break;
101                 case 1 : shape = new Ellipse2D.Float(0.0f, 0.0f, w/5, h/4);
102                          break;
103                 case 2 : shape = new RoundRectangle2D.Float(0.0f, 0.0f, w/5, h/4, 10.0f, 10.0f);
104                          break;
105                 case 3 : shape = new Rectangle2D.Float(0.0f, 0.0f, w/5, h/4);
106                          break;
107                 case 4 : shape = new QuadCurve2D.Float(0.0f,0.0f,w/10, h/2,w/5,0.0f);
108                          break;
109                 case 5 : shape = new CubicCurve2D.Float(0.0f,0.0f,w/15,h/2, w/10,h/4,w/5,0.0f);
110                          break;
111             }
112
113             g2.draw(shape);
114             g2.translate(-x, -y);
115         }
116     }
117
118
119     public static void main(String JavaDoc argv[]) {
120         createDemoFrame(new Dash());
121     }
122 }
123
Popular Tags