1 18 package org.apache.batik.swing.gvt; 19 20 import java.awt.Dimension ; 21 import java.awt.event.MouseEvent ; 22 import java.awt.geom.AffineTransform ; 23 24 32 public class AbstractRotateInteractor extends InteractorAdapter { 33 34 37 protected boolean finished; 38 39 42 protected double initialRotation; 43 44 47 public boolean endInteraction() { 48 return finished; 49 } 50 51 53 56 public void mousePressed(MouseEvent e) { 57 finished = false; 58 JGVTComponent c = (JGVTComponent)e.getSource(); 59 60 Dimension d = c.getSize(); 61 double dx = e.getX() - d.width / 2; 62 double dy = e.getY() - d.height / 2; 63 double cos = -dy / Math.sqrt(dx * dx + dy * dy); 64 initialRotation = (dx > 0) ? Math.acos(cos) : -Math.acos(cos); 65 } 66 67 70 public void mouseReleased(MouseEvent e) { 71 finished = true; 72 JGVTComponent c = (JGVTComponent)e.getSource(); 73 74 AffineTransform at = rotateTransform(c.getSize(), e.getX(), e.getY()); 75 at.concatenate(c.getRenderingTransform()); 76 c.setRenderingTransform(at); 77 } 78 79 82 public void mouseExited(MouseEvent e) { 83 finished = true; 84 JGVTComponent c = (JGVTComponent)e.getSource(); 85 c.setPaintingTransform(null); 86 } 87 88 90 97 public void mouseDragged(MouseEvent e) { 98 JGVTComponent c = (JGVTComponent)e.getSource(); 99 100 c.setPaintingTransform(rotateTransform(c.getSize(), e.getX(), e.getY())); 101 } 102 103 106 protected AffineTransform rotateTransform(Dimension d, int x, int y) { 107 double dx = x - d.width / 2; 108 double dy = y - d.height / 2; 109 double cos = -dy / Math.sqrt(dx * dx + dy * dy); 110 double angle = (dx > 0) ? Math.acos(cos) : -Math.acos(cos); 111 112 angle -= initialRotation; 113 114 return AffineTransform.getRotateInstance(angle, d.width / 2, d.height / 2); 115 } 116 } 117 | Popular Tags |