I'm approaching to develope a Snake Game with JavaFX (I'm benniger). In the main method I have set scene.setFill(Color.BLACK) and so the background is totally black. When I add a Label, when I try to instantiate it, the background disappear and the Label is not shown on the screen. It does not happen if I add Rectangle or Circle, but only with Labels, Buttons ecc.
How can I do to solve this? I think it is a simple question but I can not solve it by myself.
/Main.java
GamePanel g = new GamePanel();
BorderPane root = new BorderPane();
//Scene scene = new Scene(root,400,400, Color.BLACK);
Scene scene = new Scene(root,400,400);
scene.setFill(Color.BLACK);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.setTitle("SnakeFX");
primaryStage.show();
root.getChildren().add(g);
g.requestFocus();
/GamePanel.java (I put only a part of the function initialize() that is the first function of the constructor and that initialize the gamegraphics and that must contain my label)
void initialize() {
/* IF I PUT THIS LINE THE BLACK COLOR ON THE BACKGROUND DISAPPEAR
AND LABEL DOES NOT APPEAR ON THE SCREEN.
WITHOUT THIS LINE THE GAME RUNS PERFECTLY B BUT I NEED THIS LINE TO ADD
THE SCORE EHEH */
Label l = new Label("TEST"); //this is the line
for(int i=0;i<WIDTH;i=i+WIDTH/40) {
Line line = new Line();
line.setStartX((double)i);
line.setStartY(0.0);
line.setEndX((double)i);
line.setEndY((double)HEIGHT);
line.setVisible(true);
super.getChildren().add(line);
}
for(int j=0;j<HEIGHT;j=j+HEIGHT/40) {
Line line = new Line();
line.setStartY((double)j);
line.setStartX(0.0);
line.setEndY((double)j);
line.setEndX((double)WIDTH);
line.setVisible(true);
super.getChildren().add(line);
}
....
...
. ..
}
First image is without the instantiation of the label. Second image is with the instantiation of the label (The background color is not shown and the label is not shown)