-1

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)

enter image description here

enter image description here

Matt
  • 3,052
  • 1
  • 17
  • 30
tranesend
  • 47
  • 6
  • Can you add your "application.css" file to your question? Thanks – Dustin Feb 01 '19 at 14:54
  • it is empty. I have not used it. @DustinR – tranesend Feb 01 '19 at 15:27
  • 1
    See: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – SedJ601 Feb 01 '19 at 16:36
  • Some part(s) of your code is missing. You're just instantiating a label which doesn't get added to your scene at all and therefore wouldn't change a thing. Please follow @Sedricks advice, and while doing it, you sometimes spot the problem yourself as you need to touch all relevant parts of your code in some way. – Ignatiamus Feb 01 '19 at 16:41
  • @Ignatiamus the problem is that. Also when the Label is not added to the scene it changes the background color. It is this the strange thing. I did not the label to ths scsne in order to show that the problem exists anyway, when I only have instantiated the label. – tranesend Feb 01 '19 at 16:45
  • This sounds like some extremely freak error. Still, please try to get the problem down to a MCVE, see ↑. – Ignatiamus Feb 01 '19 at 16:47

1 Answers1

0

This is a JavaFx bug (see this answer).

What you see in your second image is the root node's (BorderPane) background color (which is defined in javafx default theme and it's not transparent). This is the correct state - the first image shows the bug described in the answer linked above.

You can fix your problem easily:

  1. remove the background color from the root node, so the scene's background color becomes visible:

    root.setBackground(Background.EMPTY);
    
  2. or, set the root node's background color to black:

    root.setBackground(new Background(new BackgroundFill(Color.BLACK, null, null)));
    
Guest 21
  • 672
  • 1
  • 6
  • 10
  • I solved that issue and now the scree remains black but why can't I see the label anyway? – tranesend Feb 02 '19 at 17:28
  • Have you added the label to the scene (`getChildren().add(l)`) and have you changed its text color to any color visible on black background? – Guest 21 Feb 02 '19 at 23:15