KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Transforms > SelectTx


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

40
41 package java2d.demos.Transforms;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import java.awt.event.*;
46 import java.awt.font.TextLayout JavaDoc;
47 import java.awt.font.FontRenderContext JavaDoc;
48 import java.awt.geom.Rectangle2D JavaDoc;
49 import java.awt.image.*;
50 import javax.swing.*;
51 import java2d.AnimatingControlsSurface;
52 import java2d.CustomControls;
53
54
55 /**
56  * Scaling or Shearing or Rotating an image & rectangle.
57  */

58 public class SelectTx extends AnimatingControlsSurface {
59
60     protected static final int RIGHT = 0;
61     private static final int LEFT = 1;
62     private static final int XMIDDLE = 2;
63     private static final int DOWN = 3;
64     private static final int UP = 4;
65     private static final int YMIDDLE = 5;
66     private static final int XupYup = 6;
67     private static final int XdownYdown = 7;
68     private static final String JavaDoc[] title = { "Scale" , "Shear", "Rotate" };
69     protected static final int SCALE = 0;
70     protected static final int SHEAR = 1;
71     protected static final int ROTATE = 2;
72     private Image img, original;
73     private int iw, ih;
74     protected int transformType = SHEAR;
75     protected double sx, sy;
76     protected double angdeg;
77     protected int direction = RIGHT;
78     protected int transformToggle;
79
80
81     public SelectTx() {
82         setBackground(WHITE);
83         original = getImage("painting.gif");
84         iw = original.getWidth(this);
85         ih = original.getHeight(this);
86         setControls(new Component[] { new DemoControls(this) });
87     }
88
89
90     public void reset(int w, int h) {
91
92         iw = w > 3 ? w/3 : 1;
93         ih = h > 3 ? h/3 : 1;
94
95         img = createImage(iw, ih);
96         Graphics big = img.getGraphics();
97         big.drawImage(original, 0, 0, iw, ih, ORANGE, null);
98         if (transformType == SCALE) {
99             direction = RIGHT;
100             sx = sy = 1.0;
101         } else if (transformType == SHEAR) {
102             direction = RIGHT;
103             sx = sy = 0;
104         } else {
105             angdeg = 0;
106         }
107     }
108
109
110     public void step(int w, int h) {
111         int rw = iw + 10;
112         int rh = ih + 10;
113
114         if (transformType == SCALE && direction == RIGHT) {
115             sx += .05;
116             if (w * .5 - iw * .5 + rw * sx + 10 > w) {
117                 direction = DOWN;
118             }
119         } else if (transformType == SCALE && direction == DOWN) {
120            sy += .05;
121            if (h * .5 - ih * .5 + rh * sy + 20 > h) {
122                direction = LEFT;
123             }
124         } else if (transformType == SCALE && direction == LEFT) {
125             sx -= .05;
126             if (rw * sx - 10 <= -(w * .5 - iw * .5)) {
127                 direction = UP;
128             }
129         } else if (transformType == SCALE && direction == UP) {
130             sy -= .05;
131             if (rh * sy - 20 <= -(h * .5 - ih * .5)) {
132                 direction = RIGHT;
133                 transformToggle = SHEAR;
134             }
135         }
136
137         if (transformType == SHEAR && direction == RIGHT) {
138             sx += .05;
139             if (rw + 2 * rh * sx + 20 > w) {
140                 direction = LEFT;
141                 sx -= .1;
142             }
143         } else if (transformType == SHEAR && direction == LEFT) {
144             sx -= .05;
145             if (rw - 2 * rh * sx + 20 > w) {
146                 direction = XMIDDLE;
147             }
148         } else if (transformType == SHEAR && direction == XMIDDLE) {
149             sx += .05;
150             if (sx > 0) {
151                 direction = DOWN;
152                 sx = 0;
153             }
154         } else if (transformType == SHEAR && direction == DOWN) {
155             sy -= .05;
156             if (rh - 2 * rw * sy + 20 > h) {
157                 direction = UP;
158                 sy += .1;
159             }
160         } else if (transformType == SHEAR && direction == UP) {
161             sy += .05;
162             if (rh + 2 * rw * sy + 20 > h) {
163                 direction = YMIDDLE;
164             }
165         } else if (transformType == SHEAR && direction == YMIDDLE) {
166             sy -= .05;
167             if (sy < 0) {
168                 direction = XupYup;
169                 sy = 0;
170             }
171         } else if (transformType == SHEAR && direction == XupYup) {
172             sx += .05; sy += .05;
173             if (rw + 2 * rh * sx + 30 > w || rh + 2 * rw * sy + 30 > h) {
174                 direction = XdownYdown;
175             }
176         } else if (transformType == SHEAR && direction == XdownYdown) {
177             sy -= .05; sx -= .05;
178             if (sy < 0) {
179                 direction = RIGHT;
180                 sx = sy = 0.0;
181                 transformToggle = ROTATE;
182             }
183         }
184
185         if (transformType == ROTATE) {
186             angdeg += 5;
187             if (angdeg == 360) {
188                 angdeg = 0;
189                 transformToggle = SCALE;
190             }
191         }
192     }
193
194
195     public void render(int w, int h, Graphics2D g2) {
196
197         Font font = g2.getFont();
198         FontRenderContext JavaDoc frc = g2.getFontRenderContext();
199         TextLayout JavaDoc tl = new TextLayout JavaDoc(title[transformType], font, frc);
200         g2.setColor(BLACK);
201         tl.draw(g2, (float) (w/2-tl.getBounds().getWidth()/2),
202             (float) (tl.getAscent()+tl.getDescent()));
203
204         if (transformType == ROTATE) {
205             String JavaDoc s = Double.toString(angdeg);
206             g2.drawString("angdeg=" + s, 2, h-4);
207         } else {
208             String JavaDoc s = Double.toString(sx);
209             s = (s.length() < 5) ? s : s.substring(0,5);
210             TextLayout JavaDoc tlsx = new TextLayout JavaDoc("sx=" + s, font, frc);
211             tlsx.draw(g2, 2, h-4);
212
213             s = Double.toString(sy);
214             s = (s.length() < 5) ? s : s.substring(0,5);
215             g2.drawString("sy=" + s,(int)(tlsx.getBounds().getWidth()+4), h-4);
216         }
217
218         if (transformType == SCALE) {
219             g2.translate(w/2-iw/2, h/2-ih/2);
220             g2.scale(sx, sy);
221         } else if (transformType == SHEAR) {
222             g2.translate(w/2-iw/2,h/2-ih/2);
223             g2.shear(sx, sy);
224         } else {
225             g2.rotate(Math.toRadians(angdeg),w/2,h/2);
226             g2.translate(w/2-iw/2,h/2-ih/2);
227         }
228         
229         g2.setColor(ORANGE);
230         g2.fillRect(0, 0, iw+10, ih+10);
231         g2.drawImage(img, 5, 5, this);
232     }
233
234
235     public static void main(String JavaDoc argv[]) {
236         createDemoFrame(new SelectTx());
237     }
238
239
240     static class DemoControls extends CustomControls implements ActionListener {
241
242         SelectTx demo;
243         JToolBar toolbar;
244
245         public DemoControls(SelectTx demo) {
246             super(demo.name);
247             this.demo = demo;
248             add(toolbar = new JToolBar());
249             toolbar.setFloatable(false);
250             addTool("Scale", false);
251             addTool("Shear", true);
252             addTool("Rotate", false);
253         }
254
255         public void addTool(String JavaDoc str, boolean state) {
256             JToggleButton b = (JToggleButton) toolbar.add(new JToggleButton(str));
257             b.setFocusPainted(false);
258             b.setSelected(state);
259             b.addActionListener(this);
260             int width = b.getPreferredSize().width;
261             Dimension prefSize = new Dimension(width, 21);
262             b.setPreferredSize(prefSize);
263             b.setMaximumSize(prefSize);
264             b.setMinimumSize(prefSize);
265         }
266
267
268         public void actionPerformed(ActionEvent e) {
269             for (int i = 0; i < toolbar.getComponentCount(); i++) {
270                 JToggleButton b = (JToggleButton) toolbar.getComponentAtIndex(i);
271                 b.setSelected(false);
272             }
273             JToggleButton b = (JToggleButton) e.getSource();
274             b.setSelected(true);
275             if (b.getText().equals("Scale")) {
276                 demo.transformType = demo.SCALE;
277                 demo.direction = demo.RIGHT;
278                 demo.sx = demo.sy = 1;
279             } else if (b.getText().equals("Shear")) {
280                 demo.transformType = demo.SHEAR;
281                 demo.direction = demo.RIGHT;
282                 demo.sx = demo.sy = 0;
283             } else if (b.getText().equals("Rotate")) {
284                 demo.transformType = demo.ROTATE;
285                 demo.angdeg = 0;
286             }
287         }
288
289         public Dimension getPreferredSize() {
290             return new Dimension(200,39);
291         }
292
293
294         public void run() {
295             Thread JavaDoc me = Thread.currentThread();
296             demo.transformToggle = demo.transformType;
297             while (thread == me) {
298                 try {
299                     thread.sleep(222);
300                 } catch (InterruptedException JavaDoc e) { return; }
301                 if (demo.transformToggle != demo.transformType) {
302                     ((AbstractButton) toolbar.getComponentAtIndex(demo.transformToggle)).doClick();
303                 }
304             }
305             thread = null;
306         }
307     } // End DemoControls class
308
} // End SelectTx class
309
Popular Tags