KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Arcs_ > Curves


1 /*
2  * @(#)Curves.java 1.28 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  * @(#)Curves.java 1.28 06/08/29
39  */

40
41 package java2d.demos.Arcs_Curves;
42
43 import java.awt.*;
44 import java.awt.geom.Ellipse2D JavaDoc;
45 import java.awt.geom.Rectangle2D JavaDoc;
46 import java.awt.geom.QuadCurve2D JavaDoc;
47 import java.awt.geom.CubicCurve2D JavaDoc;
48 import java.awt.geom.PathIterator JavaDoc;
49 import java.awt.geom.FlatteningPathIterator JavaDoc;
50 import java.awt.font.TextLayout JavaDoc;
51 import java.awt.font.FontRenderContext JavaDoc;
52 import java2d.Surface;
53
54 import static java.awt.Color JavaDoc.*;
55 import static java.awt.geom.PathIterator JavaDoc.*;
56
57
58 /**
59  * CubicCurve2D & QuadCurve2D curves includes FlattenPathIterator example.
60  */

61 public class Curves extends Surface {
62
63     private static Color JavaDoc colors[] = { BLUE, GREEN, RED };
64
65     public Curves() {
66         setBackground(WHITE);
67     }
68
69
70     public void render(int w, int h, Graphics2D g2) {
71
72         int y = 0;
73         g2.setColor(BLACK);
74         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
75         TextLayout JavaDoc tl = new TextLayout JavaDoc("QuadCurve2D", g2.getFont(), frc);
76         float xx = (float) (w*.5-tl.getBounds().getWidth()/2);
77         tl.draw(g2, xx, tl.getAscent());
78
79         tl = new TextLayout JavaDoc("CubicCurve2D", g2.getFont(), frc);
80         xx = (float) (w*.5-tl.getBounds().getWidth()/2);
81         tl.draw(g2, xx, h*.5f);
82         g2.setStroke(new BasicStroke(5.0f));
83
84         float yy = 20;
85
86         for (int i = 0; i < 2; i++) {
87             for (int j = 0; j < 3; j++) {
88                 Shape shape = null;
89
90                 if (i == 0) {
91                      shape = new QuadCurve2D.Float JavaDoc(w*.1f,yy,w*.5f,50,w*.9f,yy);
92                 } else {
93                      shape = new CubicCurve2D.Float JavaDoc(w*.1f,yy,w*.4f,yy-15,
94                                             w*.6f,yy+15,w*.9f,yy);
95                 }
96                 g2.setColor(colors[j]);
97                 if (j != 2)
98                     g2.draw(shape);
99
100                 if (j == 1 ) {
101                     g2.setColor(LIGHT_GRAY);
102                     PathIterator JavaDoc f = shape.getPathIterator(null);
103                     while ( !f.isDone() ) {
104                         float[] pts = new float[6];
105                         switch ( f.currentSegment(pts) ) {
106                             case SEG_MOVETO:
107                             case SEG_LINETO:
108                                 g2.fill(new Rectangle2D.Float JavaDoc(pts[0], pts[1], 5, 5));
109                                 break;
110                             case SEG_CUBICTO:
111                             case SEG_QUADTO:
112                                 g2.fill(new Rectangle2D.Float JavaDoc(pts[0], pts[1], 5, 5));
113                                 if (pts[2] != 0) {
114                                     g2.fill(new Rectangle2D.Float JavaDoc(pts[2], pts[3], 5, 5));
115                                 }
116                                 if (pts[4] != 0) {
117                                     g2.fill(new Rectangle2D.Float JavaDoc(pts[4], pts[5], 5, 5));
118                                 }
119                         }
120                         f.next();
121                     }
122                 } else if (j == 2) {
123                     PathIterator JavaDoc p = shape.getPathIterator(null);
124                     FlatteningPathIterator JavaDoc f = new FlatteningPathIterator JavaDoc(p,0.1);
125                     while ( !f.isDone() ) {
126                         float[] pts = new float[6];
127                         switch ( f.currentSegment(pts) ) {
128                             case SEG_MOVETO:
129                             case SEG_LINETO:
130                                 g2.fill(new Ellipse2D.Float JavaDoc(pts[0], pts[1],3,3));
131                         }
132                         f.next();
133                     }
134                 }
135                 yy += h/6;
136             }
137             yy = h/2+15;
138         }
139     }
140
141
142     public static void main(String JavaDoc argv[]) {
143         createDemoFrame(new Curves());
144     }
145 }
146
Popular Tags