2012-12-31 79 views
6

非常奇怪的問題,我最終設法將它提取成一小段代碼來演示問題。我有一個窗格,其中包含1組,該組包含一個包含一些省略號的組。頂部組有一個應用於它的旋轉變換。橢圓形是可拖動的。當父節點發生變形時拖動節點會導致節點消失

嘗試下面的示例,向下拖動一些省略號(在組的邊界外),您將看到它們消失。如果你最大化窗口,他們再次出現,但你不能拖動他們,他們不再收到任何事件。

現在的真是奇怪的一部分,有三種方式,我可以讓問題消失:(!?)

  1. 不適用變換
  2. 刪除一個橢圓(我嘗試讓這個數字,11)
  3. 開始ScenicView一起選擇包含橢圓組,所以你可以看到該組的邊界

我在這裏一敗塗地,完全愣住了。請問,有沒有人知道爲什麼會出現這個問題,以及如何解決它?

代碼(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()); 
     } 
    } 
} 
} 
+0

如果它被認爲是一個錯誤,你還在尋找解決方法嗎?否則,你可以回答你自己的問題並關閉它。 – JoshDM

+0

我嘗試關閉它,但是無法找到一個很好的匹配原因。另外,我不確定開放獎金會發生什麼情況,所以我等待它首先到期。 –

+0

發佈您自己的問題答案,引用錯誤確認。然後檢查你的答案是否被接受。你得到報銷。 – JoshDM

回答

1

它被公認爲JavaFX的一個錯誤,將在2.2.6解決了,看到here。我已經用早期的訪問版本進行了測試,我可以確認它已經解決了。