flowLine = new Line();
flowLine.setStrokeWidth(3);
flowLine.setStroke(Color.RED);
flowGroup.getChildren().addAll(flowLine);
targetNodeItem.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mEvent) {
drawPoint = new Point2D(mEvent.getSceneX(), mEvent.getSceneY());
startX = drawPoint.getX();
startY = drawPoint.getY();
}
});
targetNodeItem.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mEvent) {
flowLine.setStartX(startX);
flowLine.setStartY(startY);
flowLine.setEndX(mEvent.getSceneX());
flowLine.setEndY(mEvent.getSceneY());
}
});
targetNodeItem是节点。
我想从该节点拖动并生成FlowLine,但生成为图像(http://2url.kr/2lfjlsfa)的FlowLine坐标不正确。
=============================================== ===================================
检查一下 - &gt;&gt;&gt; http://youtu.be/iZOL0Fs-umk
请帮助我答案 0 :(得分:0)
你的代码让我在没有坐标问题的情况下绘制一条线......
我用过:
public class MouseDrag extends Application
{
private Point2D drawPoint;
private double startX;
private double startY;
@Override
public void start(Stage primaryStage) throws Exception
{
Pane root = new Pane();
Scene scene = new Scene(root, 200, 200);
Group flowGroup = new Group();
final Line line = new Line();
line.setStrokeWidth(3);
line.setStroke(Color.RED);
Label nodeOne = new Label("One");
nodeOne.setOnMousePressed((event) ->
{
drawPoint = new Point2D(event.getSceneX(), event.getSceneY());
startX = drawPoint.getX();
startY = drawPoint.getY();
line.setStartX(startX);
line.setStartY(startY);
});
nodeOne.setOnMouseDragged((event) ->
{
line.setEndX(event.getSceneX());
line.setEndY(event.getSceneY());
System.out.println("X/Y: " + event.getSceneX() + "/" + event.getSceneY());
});
Label nodeTwo = new Label("Two");
nodeTwo.setLayoutX(100);
nodeTwo.setLayoutY(100);
flowGroup.getChildren().addAll(nodeOne, nodeTwo, line);
root.getChildren().addAll(flowGroup);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
{
launch(args);
}
}
答案 1 :(得分:0)
请注意,您可以从MouseEvent
中提取多个X和Y值。在这种情况下,您正在使用getSceneX()
和getSceneY()
,它会为您提供相对于包含Scene
的坐标。
另外:&#34; MouseDragged&#34;被多次调用,这会对setStartX(...)
+ setStartY(...)
产生大量不必要的调用。考虑将其移动到&#34; MousePressed&#34;,只调用一次(见下文)。 HTH
targetNodeItem.setOnMousePressed(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mEvent) {
flowLine.setStartX(mEvent.getX());
flowLine.setStartY(mEvent.getY());
}
});
targetNodeItem.setOnMouseDragged(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent mEvent) {
//System.out.println("X: " + mEvent.getX() + " Y: " + mEvent.getY());
flowLine.setEndX(mEvent.getX());
flowLine.setEndY(mEvent.getY());
}
});