KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)Rotate.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  * @(#)Rotate.java 1.29 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.geom.Ellipse2D JavaDoc;
47 import java.awt.geom.AffineTransform JavaDoc;
48 import javax.swing.*;
49 import java2d.ControlsSurface;
50 import java2d.CustomControls;
51
52
53 /**
54  * Rotate ellipses with controls for increment and emphasis.
55  * Emphasis is defined as which ellipses have a darker color and thicker stroke.
56  */

57 public class Rotate extends ControlsSurface {
58
59
60     protected double increment = 5.0;
61     protected int emphasis = 9;
62
63
64     public Rotate() {
65         setBackground(WHITE);
66         setControls(new Component[] { new DemoControls(this) });
67     }
68
69
70     public void render(int w, int h, Graphics2D g2) {
71         int size = Math.min(w, h);
72         float ew = size/4;
73         float eh = size-20;
74         Ellipse2D JavaDoc ellipse = new Ellipse2D.Float JavaDoc(-ew/2, -eh/2, ew, eh);
75         for (double angdeg = 0; angdeg < 360; angdeg+=increment) {
76             if (angdeg % emphasis == 0) {
77                 g2.setColor(GRAY);
78                 g2.setStroke(new BasicStroke(2.0f));
79             } else {
80                 g2.setColor(LIGHT_GRAY);
81                 g2.setStroke(new BasicStroke(0.5f));
82             }
83             AffineTransform JavaDoc at = AffineTransform.getTranslateInstance(w/2, h/2);
84             at.rotate(Math.toRadians(angdeg));
85             g2.draw(at.createTransformedShape(ellipse));
86         }
87         g2.setColor(BLUE);
88         ellipse.setFrame(w/2-10,h/2-10,20,20);
89         g2.fill(ellipse);
90         g2.setColor(GRAY);
91         g2.setStroke(new BasicStroke(6));
92         g2.draw(ellipse);
93         g2.setColor(YELLOW);
94         g2.setStroke(new BasicStroke(4));
95         g2.draw(ellipse);
96         g2.setColor(BLACK);
97         g2.drawString("Rotate", 5, 15);
98     }
99
100
101     public static void main(String JavaDoc s[]) {
102         createDemoFrame(new Rotate());
103     }
104
105
106     static class DemoControls extends CustomControls implements ActionListener {
107
108         Rotate demo;
109         JTextField tf1, tf2;
110
111         public DemoControls(Rotate demo) {
112             super(demo.name);
113             this.demo = demo;
114             JLabel l = new JLabel("Increment:");
115             l.setForeground(BLACK);
116             add(l);
117             add(tf1 = new JTextField("5.0"));
118             tf1.setPreferredSize(new Dimension(30,24));
119             tf1.addActionListener(this);
120             add(l = new JLabel(" Emphasis:"));
121             l.setForeground(BLACK);
122             add(tf2 = new JTextField("9"));
123             tf2.setPreferredSize(new Dimension(30,24));
124             tf2.addActionListener(this);
125         }
126
127
128         public void actionPerformed(ActionEvent e) {
129             try {
130                 if (e.getSource().equals(tf1)) {
131                     demo.increment = Double.parseDouble(tf1.getText().trim());
132                     if (demo.increment < 1.0) {
133                         demo.increment = 1.0;
134                     }
135                 } else {
136                     demo.emphasis = Integer.parseInt(tf2.getText().trim());
137                 }
138                 demo.repaint();
139             } catch (Exception JavaDoc ex) {}
140         }
141
142         public Dimension getPreferredSize() {
143             return new Dimension(200,39);
144         }
145
146         public void run() {
147             Thread JavaDoc me = Thread.currentThread();
148             while (thread == me) {
149                 for (int i = 3; i < 13; i+=3) {
150                     try {
151                         thread.sleep(4444);
152                     } catch (InterruptedException JavaDoc e) { return; }
153                     tf1.setText(String.valueOf(i));
154                     demo.increment = i;
155                     demo.repaint();
156                 }
157             }
158             thread = null;
159         }
160     } // End DemoControls class
161
} // End Rotate class
162
Popular Tags