我在使用鼠标拖动事件操作 javafx 中的弧形时遇到问题
我有一个使用以下参数定义的弧:
- radiusX 和 radiusY :该圆弧是其部分截面的完整椭圆的水平和垂直半径
- centerX,centerY:圆弧的中心点
- startAngle:圆弧的起始角度(相对于水平轴)
- 长度:弧的角度范围(以度为单位)
所以我需要的是在拖动时使圆弧的起始角度“跟随”鼠标移动:
当用户按下圆弧的起点(位于 startAngle 上)并拖动鼠标时,我需要计算由水平轴和从中心到鼠标位置的线形成的新起始角度
基本上,问题是计算给定点(鼠标位置)和其他给定参数(中心、长轴和短轴)的弧的起始角度
我实际上正在做的是使用 Math.atan2 函数计算角度
newStartAngle = atan2(xMouse, yMouse)(假设 x, y 中心位于 0,0)
但仅当圆弧为圆形时才有效(radiusX = radiusY)
另一种说法是:我需要圆弧的起点始终位于从中心到鼠标位置的线上(因此我需要不断更新起始角度以使其跟随鼠标旋转移动)。 (我希望我已经说得清楚了)
这是示例的完整来源
import javafx.stage.Stage;
...
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Pane pane = new Pane();
Group designer = createDesigner();
designer.setLayoutX(100);
designer.setLayoutY(200);
pane.getChildren().add(designer);
Scene sc = new Scene(pane, 600, 600);
primaryStage.setScene(sc);
primaryStage.show();
}
public static final double RX = 100;
public static final double RY = 50;
public static final double S_ANGLE = 45;
public static final double ARC_LENGTH = 90;
private Arc arc;
private Circle handle;
private Line connection;
double xMouse,yMouse;
public Group createDesigner() {
arc = new Arc();
arc.setRadiusX(RX);
arc.setRadiusY(RY);
arc.setStartAngle(S_ANGLE);
arc.setLength(ARC_LENGTH);
arc.setFill(Color.LIGHTBLUE);
arc.setType(ArcType.ROUND);
handle = new Circle();
handle.setRadius(5);
handle.setStroke(Color.BLACK);
handle.setFill(Color.TRANSPARENT);
handle.setCenterX(
RX * Math.cos(Math.toRadians(S_ANGLE))
);
handle.setCenterY(
-RY * Math.cos(Math.toRadians(S_ANGLE))
);
connection = new Line();
connection.startXProperty().bind(arc.centerXProperty());
connection.startYProperty().bind(arc.centerYProperty());
connection.endXProperty().bind(handle.centerXProperty());
connection.endYProperty().bind(handle.centerYProperty());
handle.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
xMouse = event.getX();
yMouse = event.getY();
handle.setCenterX(xMouse);
handle.setCenterY(yMouse);
double angleInRadians = Math.atan2(-yMouse, xMouse);
arc.setStartAngle(Math.toDegrees(angleInRadians));
}
});
return new Group(arc, connection, handle);
}
public static void main(String[] args) {
launch(args);
}
}
提前谢谢
最佳答案
好吧,我解决了它,只需将 xMouse 和 yMouse 除以大轴和小轴即可使其工作
newStartAngle = Math.atan2(-yMouse/radiusY, xMouse/radiusX)
//assume center x, y at 0,0
关于java - 用鼠标操作 Arc (JavaFX 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10850064/