KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Clipping > Areas


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

40
41 package java2d.demos.Clipping;
42
43 import java.awt.*;
44 import java.awt.event.*;
45 import java.awt.geom.Area JavaDoc;
46 import java.awt.geom.Ellipse2D JavaDoc;
47 import java.awt.geom.GeneralPath JavaDoc;
48 import javax.swing.*;
49 import java2d.ControlsSurface;
50 import java2d.CustomControls;
51
52 import static java.awt.Color JavaDoc.*;
53
54
55 /**
56  * The Areas class demonstrates the CAG (Constructive Area Geometry)
57  * operations: Add(union), Subtract, Intersect, and ExclusiveOR.
58  */

59 public class Areas extends ControlsSurface {
60
61     protected String JavaDoc areaType = "nop";
62
63
64     public Areas() {
65         setBackground(WHITE);
66         setControls(new Component[] { new DemoControls(this) });
67     }
68
69
70     public void render(int w, int h, Graphics2D g2) {
71         GeneralPath JavaDoc p1 = new GeneralPath JavaDoc();
72         p1.moveTo( w * .25f, 0.0f);
73         p1.lineTo( w * .75f, h * .5f);
74         p1.lineTo( w * .25f, (float) h);
75         p1.lineTo( 0.0f, h * .5f);
76         p1.closePath();
77
78         GeneralPath JavaDoc p2 = new GeneralPath JavaDoc();
79         p2.moveTo( w * .75f, 0.0f);
80         p2.lineTo( (float) w, h * .5f);
81         p2.lineTo( w * .75f, (float) h);
82         p2.lineTo( w * .25f, h * .5f);
83         p2.closePath();
84
85
86         Area JavaDoc area = new Area JavaDoc(p1);
87         g2.setColor(YELLOW);
88         if (areaType.equals("nop")) {
89             g2.fill(p1);
90             g2.fill(p2);
91             g2.setColor(RED);
92             g2.draw(p1);
93             g2.draw(p2);
94             return;
95         } else if (areaType.equals("add" )) { area.add (new Area JavaDoc(p2));
96         } else if (areaType.equals("sub" )) { area.subtract (new Area JavaDoc(p2));
97         } else if (areaType.equals("xor" )) { area.exclusiveOr(new Area JavaDoc(p2));
98         } else if (areaType.equals("int" )) { area.intersect (new Area JavaDoc(p2));
99         } else if (areaType.equals("pear")) {
100
101             double sx = w/100;
102             double sy = h/140;
103             g2.scale(sx, sy);
104             double x = w/sx/2;
105             double y = h/sy/2;
106
107             // Creates the first leaf by filling the intersection of two Area
108
// objects created from an ellipse.
109
Ellipse2D JavaDoc leaf = new Ellipse2D.Double JavaDoc(x-16, y-29, 15.0, 15.0);
110             Area JavaDoc leaf1 = new Area JavaDoc(leaf);
111             leaf.setFrame(x-14, y-47, 30.0, 30.0);
112             Area JavaDoc leaf2 = new Area JavaDoc(leaf);
113             leaf1.intersect(leaf2);
114             g2.setColor(GREEN);
115             g2.fill(leaf1);
116
117             // Creates the second leaf.
118
leaf.setFrame(x+1, y-29, 15.0, 15.0);
119             leaf1 = new Area JavaDoc(leaf);
120             leaf2.intersect(leaf1);
121             g2.fill(leaf2);
122
123             // Creates the stem by filling the Area resulting from the
124
// subtraction of two Area objects created from an ellipse.
125
Ellipse2D JavaDoc stem = new Ellipse2D.Double JavaDoc(x, y-42, 40.0, 40.0);
126             Area JavaDoc st1 = new Area JavaDoc(stem);
127             stem.setFrame(x+3, y-47, 50.0, 50.0);
128             st1.subtract(new Area JavaDoc(stem));
129             g2.setColor(BLACK);
130             g2.fill(st1);
131
132             // Creates the pear itself by filling the Area resulting from the
133
// union of two Area objects created by two different ellipses.
134
Ellipse2D JavaDoc circle = new Ellipse2D.Double JavaDoc(x-25, y, 50.0, 50.0);
135             Ellipse2D JavaDoc oval = new Ellipse2D.Double JavaDoc(x-19, y-20, 40.0, 70.0);
136             Area JavaDoc circ = new Area JavaDoc(circle);
137             circ.add(new Area JavaDoc(oval));
138
139             g2.setColor(YELLOW);
140             g2.fill(circ);
141             return;
142         }
143         
144         g2.fill(area);
145         g2.setColor(RED);
146         g2.draw(area);
147     }
148
149
150     public static void main(String JavaDoc argv[]) {
151         createDemoFrame(new Areas());
152     }
153
154
155
156     static class DemoControls extends CustomControls implements ActionListener {
157
158         Areas demo;
159         JToolBar toolbar;
160         JComboBox combo;
161
162         public DemoControls(Areas demo) {
163             super(demo.name);
164             this.demo = demo;
165             add(toolbar = new JToolBar());
166             toolbar.setFloatable(false);
167             addTool("nop", "no area operation", true );
168             addTool("add", "add", false);
169             addTool("sub", "subtract", false);
170             addTool("xor", "exclusiveOr", false);
171             addTool("int", "intersection", false);
172             addTool("pear", "pear", false);
173         }
174
175
176         public void addTool(String JavaDoc str, String JavaDoc tooltip, boolean state) {
177             JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str));
178             b.setFocusPainted(false);
179             b.setToolTipText(tooltip);
180             b.setSelected(state);
181             b.addActionListener(this);
182             int width = (int) b.getPreferredSize().width;
183             Dimension prefSize = new Dimension(width, 21);
184             b.setPreferredSize(prefSize);
185             b.setMaximumSize(prefSize);
186             b.setMinimumSize(prefSize);
187         }
188
189
190         public void actionPerformed(ActionEvent e) {
191             for (Component comp : toolbar.getComponents()) {
192                 ((JToggleButton) comp).setSelected(false);
193             }
194             JToggleButton b = (JToggleButton) e.getSource();
195             b.setSelected(true);
196             demo.areaType = b.getText();
197             demo.repaint();
198         }
199
200         public Dimension getPreferredSize() {
201             return new Dimension(200,40);
202         }
203
204         public void run() {
205             try { thread.sleep(1111); } catch (Exception JavaDoc e) { return; }
206             Thread JavaDoc me = Thread.currentThread();
207             while (thread == me) {
208                 for (Component comp : toolbar.getComponents()) {
209                     ((AbstractButton) comp).doClick();
210                     try {
211                         thread.sleep(4444);
212                     } catch (InterruptedException JavaDoc e) { return; }
213                 }
214             }
215             thread = null;
216         }
217     } // End DemoControls
218
} // End Areas
219
Popular Tags