2016-10-09 18 views
0

我想獲得一個網格的等距視圖。網格只是主場景的一部分,所以我創建了一個子場景,並且我想爲它添加一個相機。我希望能夠在所有這些操作過程中縮放相機,並保持視角。如何在SubScene中設置和變換攝像機?

這是我有沒有攝像頭:

public class MyApp extends Application { 

    @Override 
    public void start(Stage stage) throws Exception { 

     Button actionButton = new Button("Placeholder\t\t\t\t\t\t\t\t\t\t\t\t"); 
     HBox hbox = new HBox(actionButton); 

     BorderPane mainPane = new BorderPane(new MyView(), null, null, hbox, null); 

     Scene scene = new Scene(mainPane); 
     stage.setScene(scene); 
     stage.show(); 
    } 

    private class MyView extends Group { 

     MyView() { 

      super(); 

      GridPane grid = new GridPane(); 
      for (int i = 0; i < 64; i++) { 
       Rectangle tile = new Rectangle(30, 30, Color.GREEN); 
       BorderPane pane = new BorderPane(tile); 
       pane.setBorder(new Border(new BorderStroke(null, BorderStrokeStyle.SOLID, 
                  null, null, null))); 
       grid.add(pane, i/8, i % 8); 
      } 

      Group root = new Group(); 
      root.getChildren().add(grid); 

      SubScene scene = new SubScene(root, 300, 300, true, SceneAntialiasing.BALANCED); 
      scene.setFill(Color.DARKCYAN); // just to see the area 

      getChildren().add(scene); 
     } 
    } 

    public static void main(String[] args) throws Exception { 

     launch(args); 
    } 
} 

enter image description here

現在我的MyView的構造函數中添加的攝像頭是這樣的:

Camera camera = new PerspectiveCamera(true); 
scene.setCamera(camera); 

和電網消失。

enter image description here

我甚至沒有做任何改造,但(我會做camera.getTransforms().addAll(new Rotate(-15, Rotate.Y_AXIS));)。我究竟做錯了什麼?

另外,我怎麼能告訴子領域採取任何可用的空間?我不想指定具體的大小,因爲程序可以在各種屏幕上運行。

回答

0

您的相機與Group的座標位置相同z座標。然而,你必須確保它在farClipnearClip之間的距離:

PerspectiveCamera camera = new PerspectiveCamera(true); 
camera.setTranslateZ(-100); 
camera.setFieldOfView(120); 

而且對等角視圖透視相機是錯誤的Camera使用。使用ParallelCamera代替:

Camera camera = new ParallelCamera(); 
//camera.setRotationAxis(new Point3D(1, 1, 0)); 
//camera.setRotate(30); 
scene.setCamera(camera); 

也是,我怎麼能告訴子場景採取一切可用空間?

變化通過MyView擴展到東西是可調整大小和SubScene的大小結合的MyView大小類型:

private class MyView extends Pane { 

    MyView() { 
     ... 
     setPrefSize(300, 300); 
     scene.widthProperty().bind(widthProperty()); 
     scene.heightProperty().bind(heightProperty()); 
    } 
相關問題