1 /* 2 * 3 * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * - Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * - Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * - Neither the name of Oracle nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 21 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 package java2d.demos.Transforms; 33 34 35 import static java.awt.Color.BLACK; 36 import static java.awt.Color.BLUE; 37 import static java.awt.Color.GRAY; 38 import static java.awt.Color.LIGHT_GRAY; 39 import static java.awt.Color.WHITE; 40 import static java.awt.Color.YELLOW; 41 import java.awt.BasicStroke; 42 import java.awt.Component; 43 import java.awt.Dimension; 44 import java.awt.Graphics2D; 45 import java.awt.event.ActionEvent; 46 import java.awt.event.ActionListener; 47 import java.awt.geom.AffineTransform; 48 import java.awt.geom.Ellipse2D; 49 import java2d.ControlsSurface; 50 import java2d.CustomControls; 51 import javax.swing.JLabel; 52 import javax.swing.JTextField; 53 54 55 /** 56 * Rotate ellipses with controls for increment and emphasis. 57 * Emphasis is defined as which ellipses have a darker color and thicker stroke. 58 */ 59 @SuppressWarnings("serial") 60 public class Rotate extends ControlsSurface { 61 62 protected double increment = 5.0; 63 protected int emphasis = 9; 64 65 public Rotate() { 66 setBackground(WHITE); 67 setControls(new Component[] { new DemoControls(this) }); 68 } 69 70 @Override 71 public void render(int w, int h, Graphics2D g2) { 72 int size = Math.min(w, h); 73 float ew = size / 4; 74 float eh = size - 20; 75 Ellipse2D ellipse = new Ellipse2D.Float(-ew / 2, -eh / 2, ew, eh); 76 for (double angdeg = 0; angdeg < 360; angdeg += increment) { 77 if (angdeg % emphasis == 0) { 78 g2.setColor(GRAY); 79 g2.setStroke(new BasicStroke(2.0f)); 80 } else { 81 g2.setColor(LIGHT_GRAY); 82 g2.setStroke(new BasicStroke(0.5f)); 83 } 84 AffineTransform at = AffineTransform.getTranslateInstance(w / 2, h 85 / 2); 86 at.rotate(Math.toRadians(angdeg)); 87 g2.draw(at.createTransformedShape(ellipse)); 88 } 89 g2.setColor(BLUE); 90 ellipse.setFrame(w / 2 - 10, h / 2 - 10, 20, 20); 91 g2.fill(ellipse); 92 g2.setColor(GRAY); 93 g2.setStroke(new BasicStroke(6)); 94 g2.draw(ellipse); 95 g2.setColor(YELLOW); 96 g2.setStroke(new BasicStroke(4)); 97 g2.draw(ellipse); 98 g2.setColor(BLACK); 99 g2.drawString("Rotate", 5, 15); 100 } 101 102 public static void main(String s[]) { 103 createDemoFrame(new Rotate()); 104 } 105 106 107 static class DemoControls extends CustomControls implements ActionListener { 108 109 Rotate demo; 110 JTextField tf1, tf2; 111 112 @SuppressWarnings("LeakingThisInConstructor") 113 public DemoControls(Rotate demo) { 114 super(demo.name); 115 this.demo = demo; 116 JLabel l = new JLabel("Increment:"); 117 l.setForeground(BLACK); 118 add(l); 119 add(tf1 = new JTextField("5.0")); 120 tf1.setPreferredSize(new Dimension(30, 24)); 121 tf1.addActionListener(this); 122 add(l = new JLabel(" Emphasis:")); 123 l.setForeground(BLACK); 124 add(tf2 = new JTextField("9")); 125 tf2.setPreferredSize(new Dimension(30, 24)); 126 tf2.addActionListener(this); 127 } 128 129 @Override 130 public void actionPerformed(ActionEvent e) { 131 try { 132 if (e.getSource().equals(tf1)) { 133 demo.increment = Double.parseDouble(tf1.getText().trim()); 134 if (demo.increment < 1.0) { 135 demo.increment = 1.0; 136 } 137 } else { 138 demo.emphasis = Integer.parseInt(tf2.getText().trim()); 139 } 140 demo.repaint(); 141 } catch (Exception ex) { 142 } 143 } 144 145 @Override 146 public Dimension getPreferredSize() { 147 return new Dimension(200, 39); 148 } 149 150 @Override 151 @SuppressWarnings("SleepWhileHoldingLock") 152 public void run() { 153 Thread me = Thread.currentThread(); 154 while (thread == me) { 155 for (int i = 3; i < 13; i += 3) { 156 try { 157 Thread.sleep(4444); 158 } catch (InterruptedException e) { 159 return; 160 } 161 tf1.setText(String.valueOf(i)); 162 demo.increment = i; 163 demo.repaint(); 164 } 165 } 166 thread = null; 167 } 168 } // End DemoControls class 169 } // End Rotate class 170