1 package net.sf.openrocket.gui.components.compass;
3 import java.awt.event.MouseAdapter;
4 import java.awt.event.MouseEvent;
6 import net.sf.openrocket.gui.adaptors.DoubleModel;
7 import net.sf.openrocket.util.MathUtil;
10 * Component that allows selecting a compass direction on a CompassSelector.
12 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
14 public class CompassSelector extends CompassPointer {
16 private final DoubleModel model;
18 public CompassSelector(DoubleModel model) {
22 MouseAdapter mouse = new MouseAdapter() {
23 private boolean dragging = false;
26 public void mousePressed(MouseEvent e) {
27 if (!isWithinCircle(e))
29 if (e.getButton() != MouseEvent.BUTTON1)
36 public void mouseReleased(MouseEvent e) {
37 if (e.getButton() != MouseEvent.BUTTON1)
44 public void mouseDragged(MouseEvent e) {
50 this.addMouseListener(mouse);
51 this.addMouseMotionListener(mouse);
55 private boolean isWithinCircle(MouseEvent e) {
56 if (mid < 0 || width < 0) {
60 int x = e.getX() - mid;
61 int y = e.getY() - mid;
63 double distance = Math.hypot(x, y);
64 return distance < width / 2;
67 private void clicked(MouseEvent e) {
69 if (mid < 0 || width < 0) {
73 int x = e.getX() - mid;
74 int y = e.getY() - mid;
76 double distance = Math.hypot(x, y);
78 double theta = Math.atan2(y, x);
79 theta = MathUtil.reduce360(theta + Math.PI / 2);
81 // Round the value appropriately
82 theta = Math.toDegrees(theta);
85 theta = Math.round(theta);
86 } else if (distance > 10) {
87 theta = 5 * Math.round(theta / 5);
89 // Do nothing if too close to center
92 theta = Math.toRadians(theta);
94 model.setValue(theta);