1
2 package hep.wired.interaction;
3
4 import java.awt.event.*;
5 import java.awt.geom.*;
6 import javax.swing.*;
7
8 import org.freehep.application.Application;
9
10 import hep.wired.edit.Rotate;
11 import hep.wired.services.GraphicsPanel;
12 import hep.wired.services.RecordPlot;
13 import hep.wired.image.WiredBaseImage;
14
15 /***
16 * Drag mouse to rotate plot in Z-plane.
17 *
18 * @author Mark Donszelmann
19 * @version $Id: DragToRotate.java 682 2005-03-14 02:24:52Z duns $
20 */
21 public class DragToRotate extends DefaultInteractionHandler {
22
23 private int xp, yp;
24
25 /***
26 * Create a drag handler to generate rotate edits.
27 */
28 public DragToRotate() {
29 super("Drag to Rotate in Z");
30 }
31
32 public Icon getIcon(int size) {
33 return WiredBaseImage.getIcon("Rotate%w", size);
34 }
35
36 public String getDescription() {
37 return "Drag around center to rotate in the Z plane.";
38 }
39
40 public boolean isSupportedBy(GraphicsPanel panel) {
41 return new Rotate().isSupportedBy(panel);
42 }
43
44 public void changeCursor(RecordPlot plot, InputEvent event) {
45 plot.setCursor(WiredBaseImage.getBestCursor("RotateCursor%w", 32, 32));
46 }
47
48 public void reset(RecordPlot plot, InputEvent event) {
49 Application.getApplication().setStatusMessage(getDescription());
50 }
51
52 public void mouseEntered(RecordPlot plot, MouseEvent event) {
53 plot.requestFocusInWindow();
54 changeCursor(plot, event);
55 }
56
57 public void mouseButton1DragStarted(RecordPlot plot, MouseEvent event) {
58 xp = event.getX();
59 yp = event.getY();
60 plot.beginUpdate();
61 plot.getGraphicsPanel().setFastMode(true);
62 }
63
64 public void mouseButton1Dragged(RecordPlot plot, MouseEvent event) {
65 int xd = event.getX();
66 int yd = event.getY();
67
68
69 int x1 = xd - plot.getWidth()/2;
70 int y1 = yd - plot.getHeight()/2;
71 int x2 = xp - plot.getWidth()/2;
72 int y2 = yp - plot.getHeight()/2;
73 double angle = Math.atan2(x1, y1) - Math.atan2(x2, y2);
74 plot.postEdit(new Rotate(angle, 0, 0, 1));
75
76 xp = xd;
77 yp = yd;
78 }
79
80 public void mouseButton1DragEnded(RecordPlot plot, MouseEvent event) {
81 plot.getGraphicsPanel().setFastMode(false);
82 plot.endUpdate();
83 plot.repaint();
84 }
85
86 public String toString() {
87 return "Drag to Rotate in Z-Plane";
88 }
89 }