当父节点进行转换时拖动节点会导致节点消失

时间:2012-12-31 01:45:21

标签: java javafx-2

非常奇怪的问题,我终于设法将它提炼成一小段代码来证明这个问题。我有一个窗格,其中包含一个组,该组包含一个包含一些省略号的组。顶部组应用了旋转变换。椭圆是可拖动的。

尝试下面的示例,向下拖动一些椭圆(在组的边界之外),你会看到它们消失。如果您最大化窗口,它们会再次出现,但您不能再拖动它们,它们不再接收任何事件。

现在对于真正奇怪的部分,有三种方法可以解决问题:

  1. 不应用转换
  2. 删除一个椭圆(!?)(我试验得到这个数字,11)
  3. 启动ScenicView并与一起选择包含省略号的组,以便您可以看到该组的范围
  4. 我在这里完全失去了,完全被惊呆了。请问,有没有人知道为什么会出现这个问题以及如何解决它?

    代码(JavaFX 2.2.3和java 1.7.0_09 64位Windows 7):

    import javafx.application.Application;
    import javafx.event.EventHandler;
    import javafx.geometry.Point2D;
    import javafx.scene.Group;
    import javafx.scene.GroupBuilder;
    import javafx.scene.Node;
    import javafx.scene.Scene;
    import javafx.scene.SceneBuilder;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.Pane;
    import javafx.scene.shape.Ellipse;
    import javafx.scene.shape.EllipseBuilder;
    import javafx.scene.transform.Rotate;
    import javafx.scene.transform.RotateBuilder;
    import javafx.stage.Stage;
    
    public class DragProblem extends Application {
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        DrawingPane drawingPane = new DrawingPane();
        drawingPane.setStyle("-fx-background-color: darkgrey;");
    
        Scene scene = SceneBuilder.create().root(drawingPane).width(1280d).height(1024d).build();
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    
    public class DrawingPane extends Pane {
        private Group transformedGroup;
        private Group splinePoints;
    
        public DrawingPane() {
            transformedGroup = GroupBuilder.create().id("transformedGroup").build();
    
            getChildren().add(transformedGroup);
    
            addPoints();
            makePointsDraggable();
        }
    
        public void addPoints() {
            double[] coords = new double[] {
                    // comment any one the below x,y coordinates and the problem doesn't occur..
                    239.28353881835938, 488.2192687988281,
                    245.04466247558594, 505.30169677734375,
                    258.56671142578125, 539.49462890625,
                    267.2294006347656, 563.618408203125,
                    282.89141845703125, 587.84033203125,
                    309.6925048828125, 602.2174072265625,
                    327.4945068359375, 616.4683227539062,
                    345.25445556640625, 633.718994140625,
                    371.0416259765625, 649.0819702148438,
                    393.78704833984375, 667.402587890625,
                    442.67010498046875, 676.0886840820312 };
    
            splinePoints = GroupBuilder.create().build();
            for (int i = 0; i < coords.length; i += 2) {
                Ellipse ellipse = EllipseBuilder.create().radiusX(3).radiusY(3).centerX(coords[i]).centerY(coords[i + 1]).build();
                splinePoints.getChildren().add(ellipse);
            }
    
            transformedGroup.getChildren().add(splinePoints);
    
            Rotate rotateTransform = RotateBuilder.create().build();
            rotateTransform.setPivotX(224);
            rotateTransform.setPivotY(437);
            rotateTransform.setAngle(15);
    
            // ..or comment this line to prevent the problem occuring
            transformedGroup.getTransforms().add(rotateTransform);
        }
    
        public void makePointsDraggable() {
            for (final Node n : splinePoints.getChildren()) {
                Ellipse e = (Ellipse) n;
                final NodeDragHandler ellipseDragHandler = new NodeDragHandler(e, transformedGroup);
    
                e.setOnMousePressed(ellipseDragHandler);
                e.setOnMouseDragged(ellipseDragHandler);
            }
        }
    }
    
    public class NodeDragHandler implements EventHandler<MouseEvent> {
        protected final Ellipse node;
        private final Node transformedGroup;
    
        private double initialX;
        private double initialY;
        private Point2D initial;
        private boolean dragStarted = false;
    
        public NodeDragHandler(Ellipse node, Group transformedGroup) {
            this.node = node;
            this.transformedGroup = transformedGroup;
        }
    
        @Override
        public void handle(MouseEvent event) {
            if (!dragStarted) {
                initialX = event.getScreenX();
                initialY = event.getScreenY();
    
                initial = transformedGroup.localToParent(new Point2D(node.getCenterX(), node.getCenterY()));
    
                dragStarted = true;
            } else {
                double xDragged = event.getScreenX() - initialX;
                double yDragged = event.getScreenY() - initialY;
    
                Point2D newPos = new Point2D(initial.getX() + xDragged, initial.getY() + yDragged);
    
                Point2D p = transformedGroup.parentToLocal(newPos.getX(), newPos.getY());
    
                node.setCenterX(p.getX());
                node.setCenterY(p.getY());
            }
        }
    }
    }
    

1 个答案:

答案 0 :(得分:1)

它被认为是JavaFX中的一个错误,将在2.2.6中解决,请参阅here。我已经使用早期访问版本测试了它,我可以确认它已经解决了。