KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)Intersection.java 1.32 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  * @(#)Intersection.java 1.32 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.*;
46 import java.awt.font.FontRenderContext JavaDoc;
47 import java.awt.font.TextLayout JavaDoc;
48 import javax.swing.*;
49 import java2d.AnimatingControlsSurface;
50 import java2d.CustomControls;
51
52 import static java.awt.Color JavaDoc.*;
53
54
55
56 /**
57  * Animated intersection clipping of lines, an image and a textured rectangle.
58  */

59 public class Intersection extends AnimatingControlsSurface {
60
61     private static final int HEIGHT_DECREASE = 0;
62     private static final int HEIGHT_INCREASE = 1;
63     private static final int WIDTH_DECREASE = 2;
64     private static final int WIDTH_INCREASE = 3;
65
66     private int xx, yy, ww, hh;
67     private int direction = HEIGHT_DECREASE;
68     private int angdeg;
69     private Shape textshape;
70     private double sw, sh;
71     private GeneralPath ovals;
72     private Rectangle2D rectshape;
73     private DemoControls controls;
74     protected boolean doIntersection = true;
75     protected boolean doOvals = true;
76     protected boolean doText;
77     protected boolean threeSixty;
78
79
80     public Intersection() {
81         setBackground(WHITE);
82         setControls(new Component[] { new DemoControls(this) });
83     }
84
85
86     public void reset(int w, int h) {
87         xx = yy = 0;
88         ww = w-1; hh = h;
89         direction = HEIGHT_DECREASE;
90         angdeg = 0;
91         FontRenderContext JavaDoc frc = new FontRenderContext JavaDoc(null, true, false);
92         Font f = new Font("serif",Font.BOLD,32);
93         TextLayout JavaDoc tl = new TextLayout JavaDoc("J2D", f, frc);
94         sw = tl.getBounds().getWidth();
95         sh = tl.getBounds().getHeight();
96         int size = Math.min(w, h);
97         double sx = (size-40)/sw;
98         double sy = (size-100)/sh;
99         AffineTransform Tx = AffineTransform.getScaleInstance(sx, sy);
100         textshape = tl.getOutline(Tx);
101         rectshape = textshape.getBounds();
102         sw = rectshape.getWidth();
103         sh = rectshape.getHeight();
104         ovals = new GeneralPath();
105         ovals.append(new Ellipse2D.Double( 10, 10, 20, 20), false);
106         ovals.append(new Ellipse2D.Double(w-30, 10, 20, 20), false);
107         ovals.append(new Ellipse2D.Double( 10, h-30, 20, 20), false);
108         ovals.append(new Ellipse2D.Double(w-30, h-30, 20, 20), false);
109     }
110
111
112     public void step(int w, int h) {
113         if (direction == HEIGHT_DECREASE) {
114             yy+=2; hh-=4;
115             if (yy >= h/2) {
116                 direction = HEIGHT_INCREASE;
117             }
118         } else if (direction == HEIGHT_INCREASE) {
119             yy-=2; hh+=4;
120             if (yy <= 0) {
121                 direction = WIDTH_DECREASE;
122                 hh = h-1; yy = 0;
123             }
124         }
125         if (direction == WIDTH_DECREASE) {
126             xx+=2; ww-=4;
127             if (xx >= w/2) {
128                 direction = WIDTH_INCREASE;
129             }
130         } else if (direction == WIDTH_INCREASE) {
131             xx-=2; ww+=4;
132             if (xx <= 0) {
133                 direction = HEIGHT_DECREASE;
134                 ww = w-1; xx = 0;
135             }
136         }
137         if ((angdeg += 5) == 360) {
138             angdeg = 0;
139             threeSixty = true;
140         }
141     }
142
143
144     public void render(int w, int h, Graphics2D g2) {
145
146         Rectangle rect = new Rectangle(xx, yy, ww, hh);
147        
148         AffineTransform Tx = new AffineTransform();
149         Tx.rotate(Math.toRadians(angdeg),w/2,h/2);
150         Tx.translate(w/2-sw/2, sh+(h-sh)/2);
151
152         GeneralPath path = new GeneralPath();
153         if (doOvals) {
154             path.append(ovals, false);
155         }
156         if (doText) {
157             path.append(Tx.createTransformedShape(textshape), false);
158         } else {
159             path.append(Tx.createTransformedShape(rectshape), false);
160         }
161
162         if (doIntersection) {
163             g2.clip(rect);
164             g2.clip(path);
165         }
166
167         g2.setColor(GREEN);
168         g2.fill(rect);
169
170         g2.setClip(new Rectangle(0, 0, w, h));
171
172         g2.setColor(LIGHT_GRAY);
173         g2.draw(rect);
174         g2.setColor(BLACK);
175         g2.draw(path);
176     }
177
178
179     public static void main(String JavaDoc argv[]) {
180         createDemoFrame(new Intersection());
181     }
182
183
184     static class DemoControls extends CustomControls implements ActionListener {
185
186         Intersection demo;
187         JToolBar toolbar;
188
189         public DemoControls(Intersection demo) {
190             super(demo.name);
191             this.demo = demo;
192             add(toolbar = new JToolBar());
193             toolbar.setFloatable(false);
194             addTool("Intersect", true );
195             addTool("Text", false);
196             addTool("Ovals", true );
197         }
198
199
200         public void addTool(String JavaDoc str, boolean state) {
201             JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str));
202             b.setFocusPainted(false);
203             b.setSelected(state);
204             b.addActionListener(this);
205             int width = b.getPreferredSize().width;
206             Dimension prefSize = new Dimension(width, 21);
207             b.setPreferredSize(prefSize);
208             b.setMaximumSize( prefSize);
209             b.setMinimumSize( prefSize);
210         }
211
212
213         public void actionPerformed(ActionEvent e) {
214             JToggleButton b = (JToggleButton) e.getSource();
215             if (b.getText().equals("Intersect")) {
216                 demo.doIntersection = b.isSelected();
217             } else if (b.getText().equals("Ovals")) {
218                 demo.doOvals = b.isSelected();
219             } else if (b.getText().equals("Text")) {
220                 demo.doText = b.isSelected();
221             }
222             if (demo.animating.thread == null) {
223                 demo.repaint();
224             }
225         }
226
227         public Dimension getPreferredSize() {
228             return new Dimension(200,40);
229         }
230
231
232         public void run() {
233             Thread JavaDoc me = Thread.currentThread();
234             while (thread == me) {
235                 if (demo.threeSixty) {
236                     ((AbstractButton) toolbar.getComponentAtIndex(1)).doClick();
237                     demo.threeSixty = false;
238                 }
239                 try {
240                     thread.sleep(500);
241                 } catch (InterruptedException JavaDoc e) { return; }
242             }
243             thread = null;
244         }
245     } // End DemoControls
246
} // End Intersection
247
Popular Tags