1
我想在扩展场景的类中绘制画布。当我在延伸Application
的类“GUI”中创建的场景上按下按钮时,应显示此场景。用JavaFX显示另一个场景[场景图形问题]
问题: 我不明白如何使用Scene构造函数中的“parent”参数。所以我无法显示场景的内容。
当我使用:
Scene gameScene = new GameScene(root,800,600);
我会得到一个错误,因为根已被设置为另一个场景根。
使用游戏而不是根目录所有编译都很好,并且场景不显示,但场景内容没有显示。
在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:Jeah!我在这个论坛上的第一个问题。你好,世界!