[英]Displaying another Scene with JavaFX [Scene Graph issues]
我想在擴展Scene的類中繪制畫布。 當我在擴展Application
的類“ GUI”中創建的場景上按下按鈕時,應該顯示該場景。
問題:我不知道如何使用Scene構造函數中的“ parent”參數。 因此,我無法顯示場景的內容。
當我使用時:
Scene gameScene = new GameScene(root,800,600);
我會得到一個錯誤,因為root已經被設置為另一個場景的root。
使用游戲而不是root可以正常編譯所有內容,並顯示Scene ist,但是沒有顯示Scene的內容。
在GameScene類中,我嘗試使用兩種不同類型的顯示內容。
ImageView mapView
設置圖像(我不知道我要將ImageView作為子項添加到哪個節點{諸如parent.getChildren().add(mapView)
不起作用)) GUI
類:
public class GUI extends Application {
public static void main(String[] args) { launch(args); }
public void start(Stage primaryStage){
StackPane root = new StackPane();
Group game = new Group();
root.getChildren().add(game);
Scene gameScene = new GameScene(game,800,600);
Button btn = new Button();
btn.setText("Start Game");
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
primaryStage.setScene(gameScene);
primaryStage.show();
}
});
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 650,450));
primaryStage.show();
}
}
和GameScene
類:
public class GameScene extends Scene {
private final double WIDTH;
private final double HEIGHT;
public Canvas mapCanvas;
private Map map;
public GameScene(Parent parent, double x, double y) {
super(parent, x, y);
ImageView mapView = new ImageView();
WIDTH = x;
HEIGHT = y;
this.map = new Map((int)x,(int)y);
mapCanvas = new Canvas(WIDTH,HEIGHT);
mapView.setImage(map.getImage());
GraphicsContext graphicsContext = mapCanvas.getGraphicsContext2D();
draw(graphicsContext);
}
public void draw(GraphicsContext gc){
gc.fillRect(10, 10, 100, 50);
}
}
PS:耶! 我在這個論壇上的第一個問題。 你好,世界!
一年后...
將“父”替換為“組”,或執行:(組)父項
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.