📄 rotate.java
字号:
/* * @(#)Rotate.java 1.17 99/04/23 * * Copyright (c) 1998, 1999 by Sun Microsystems, Inc. All Rights Reserved. * * Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, * modify and redistribute this software in source and binary code form, * provided that i) this copyright notice and license appear on all copies of * the software; and ii) Licensee does not utilize the software in a manner * which is disparaging to Sun. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF * OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. * * This software is not designed or intended for use in on-line control of * aircraft, air traffic, aircraft navigation or aircraft communications; or in * the design, construction, operation or maintenance of any nuclear * facility. Licensee represents and warrants that it will not use or * redistribute the Software for such purposes. */package demos.Transforms;import java.awt.*;import java.awt.event.*;import java.awt.geom.Ellipse2D;import java.awt.geom.AffineTransform;import javax.swing.*;import DemoSurface;import DemoPanel;import CustomControls;/** * Rotate ellipses with controls for increment and emphasis. * Emphasis is defined as which ellipses have a darker color and thicker stroke. */public class Rotate extends DemoSurface implements CustomControls { protected double increment = 5.0; protected int emphasis = 9; private DemoControls controls; public Rotate() { setBackground(Color.white); controls = new DemoControls(this); } public String[] getCustomControlsConstraints() { return new String[] { BorderLayout.NORTH }; } public Component[] getCustomControls() { return new Component[] { (Component) controls }; } public void customControlsThread(int state) { if (state == CustomControls.START) { controls.start(); } else if (state == CustomControls.STOP) { controls.stop(); } } public void drawDemo(int w, int h, Graphics2D g2) { int size = Math.min(w, h); float ew = size/4; float eh = size-20; Ellipse2D ellipse = new Ellipse2D.Float(-ew/2, -eh/2, ew, eh); for (double angdeg = 0; angdeg < 360; angdeg+=increment) { if (angdeg % emphasis == 0) { g2.setColor(Color.gray); g2.setStroke(new BasicStroke(2.0f)); } else { g2.setColor(Color.lightGray); g2.setStroke(new BasicStroke(0.5f)); } AffineTransform at = AffineTransform.getTranslateInstance(w/2, h/2); at.rotate(Math.toRadians(angdeg)); g2.draw(at.createTransformedShape(ellipse)); } g2.setColor(Color.blue); ellipse.setFrame(w/2-10,h/2-10,20,20); g2.fill(ellipse); g2.setColor(Color.gray); g2.setStroke(new BasicStroke(6)); g2.draw(ellipse); g2.setColor(Color.yellow); g2.setStroke(new BasicStroke(4)); g2.draw(ellipse); g2.setColor(Color.black); g2.drawString("Rotate", 5, 15); } public static void main(String s[]) { Frame f = new Frame("Java2D Demo - Rotate"); f.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0);} }); f.add("Center", new DemoPanel(new Rotate())); f.pack(); f.setSize(new Dimension(400,300)); f.show(); } static class DemoControls extends JPanel implements ActionListener, Runnable { Rotate demo; JTextField tf1, tf2; Thread thread; public DemoControls(Rotate demo) { this.demo = demo; setBackground(Color.gray); JLabel l = new JLabel("Increment:"); l.setForeground(Color.black); add(l); add(tf1 = new JTextField("5.0")); tf1.setPreferredSize(new Dimension(30,24)); tf1.addActionListener(this); add(l = new JLabel(" Emphasis:")); l.setForeground(Color.black); add(tf2 = new JTextField("9")); tf2.setPreferredSize(new Dimension(30,24)); tf2.addActionListener(this); addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { if (thread == null) start(); else stop(); } }); } public void actionPerformed(ActionEvent e) { try { if (e.getSource().equals(tf1)) { demo.increment = Double.parseDouble(tf1.getText().trim()); if (demo.increment < 1.0) { demo.increment = 1.0; } } else { demo.emphasis = Integer.parseInt(tf2.getText().trim()); } demo.repaint(); } catch (Exception ex) {} } public Dimension getPreferredSize() { return new Dimension(200,36); } public void start() { if (thread != null) { return; } thread = new Thread(this); thread.setPriority(Thread.MIN_PRIORITY); thread.setName("Transforms.Rotate DemoControls Thread"); thread.start(); } public synchronized void stop() { if (thread != null) { thread.interrupt(); } thread = null; notifyAll(); } public void run() { Thread me = Thread.currentThread(); while (thread == me) { for (int i = 3; i < 13; i+=3) { try { thread.sleep(4444); } catch (InterruptedException e) { return; } tf1.setText(String.valueOf(i)); demo.increment = i; demo.repaint(); } } thread = null; } } // End DemoControls class} // End Rotate class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -