Глюк Javafx при не максимизации окна

У меня проблема с моей программой. Когда я максимизирую окно и затем сразу же максимизирую его, мой ярлык не имеет нужного размера, вот mvce:

public class Mvce extends Application {

    private Scene scene;
    // will be the root of our scene
    private AnchorPane anchor;
    // this stackPane will contain the grid and the winLabel
    private GridPane gPane;
    // will contain the lines we draw (not in this example)
    private Pane linesPane;
    // will contain the circles we draw
    private Pane symbolesPane;
    // will contain the grid, the lines, the circle and the win label
    private StackPane stack;
    private Label winLabel;

    // will keep track of the lines, not used in this example
    private HashMap<String, ArrayList<Line>> lines;
    // will keep track of the circles and images drawn over the grid
    // we only draw circles in this example
    private HashMap<Integer, Node> symboles;


    // number of columns and rows, from which we calculate the binding cellSize
    private IntegerProperty nbC;
    private IntegerProperty nbR;
    private NumberBinding cellSize;

    private final int WINDOW_SIZE=720;

    public Node getNodeByRowColumnIndex(GridPane gridPane, final int c, final int r){
        Node result = null;
        ObservableList<Node> childrens = gridPane.getChildren();

        for (Node node : childrens){
            if(GridPane.getRowIndex(node) == r && GridPane.getColumnIndex(node) == c){
                result = node;
                break;
            }
        }

        return result;
    }

    public void addWinLabel(){
        DoubleProperty fontSize = new SimpleDoubleProperty();
        fontSize.bind(cellSize.multiply(nbC.getValue()).divide(15));

        winLabel = new Label("Congratulations, you won :)");
        winLabel.prefWidthProperty().bind(stack.widthProperty());
        winLabel.prefHeightProperty().bind(stack.heightProperty());

        winLabel.setTextFill(Color.web("#3099AA"));
        winLabel.setAlignment(Pos.CENTER);

        winLabel.styleProperty().bind(Bindings.concat("-fx-font-family: Lora;",
                                                      "-fx-font-style: italic;",
                                                      "-fx-font-size: ", fontSize.asString(), ";",
                                                      "-fx-background-color: rgba(200, 200, 200, 0.85);"));

        anchor.getChildren().add(winLabel);
        anchor.setTopAnchor(winLabel, 75.0);
        anchor.setLeftAnchor(winLabel, 75.0);
    }

    public void createPanes(){
        anchor.getChildren().remove(stack);
        stack = new StackPane();
        gPane = new GridPane();
        linesPane = new Pane();
        linesPane.setMouseTransparent(true);
        symbolesPane = new Pane();
        symbolesPane.setMouseTransparent(true);
        cellSize = Bindings.min(scene.widthProperty().subtract(150).divide(nbC.getValue()), scene.heightProperty().subtract(150).divide(nbR.getValue()));

        for(int c=0; c<nbC.getValue(); c++){            
            for(int r=0; r<nbR.getValue(); r++){
                Pane pane = new StackPane();

                pane.minWidthProperty().bind(cellSize);
                pane.minHeightProperty().bind(cellSize);
                pane.maxWidthProperty().bind(cellSize);
                pane.maxHeightProperty().bind(cellSize);

                pane.setStyle("-fx-background-color: #FFFFFF");
                gPane.add(pane,c,r);
            }
        }

        gPane.setGridLinesVisible(true);
        stack.getChildren().addAll(gPane, linesPane, symbolesPane);
        anchor.getChildren().add(stack);
        anchor.setTopAnchor(stack, 75.0);
        anchor.setLeftAnchor(stack, 75.0);

        addWinLabel();

        lines = new HashMap<>();
        symboles = new HashMap<>();
    }

    public void drawCircle(int c, int r){
        Pane pane = (Pane)getNodeByRowColumnIndex(gPane, c, r);

        Circle symbCircle = new Circle();

        symbCircle.radiusProperty().bind(cellSize.multiply(0.45));
        symbCircle.setFill(Color.web("#2e579e"));
        symbCircle.setSmooth(true);

        InvalidationListener listener = obj -> {
            Bounds boundsInScene = pane.getBoundsInParent();
            double x = (boundsInScene.getMinX() + boundsInScene.getMaxX())/2;
            double y = (boundsInScene.getMinY() + boundsInScene.getMaxY())/2;

            symbCircle.setCenterX(x);
            symbCircle.setCenterY(y);
        };

        // listen to location & size changes of panes in the GridPane
        pane.boundsInParentProperty().addListener(listener);

        // initial refresh
        listener.invalidated(null);

        symbCircle.setMouseTransparent(true);

        symbolesPane.getChildren().add(symbCircle);
        symboles.put(hashCell(c, r), symbCircle);
    }

    // renvoie un hash pour stocker l'index du symbole depuis ses coordonnées
    public int hashCell(int c, int r){
        return c*nbR.getValue() + r;
    }

    public void display(){
        // these 3 doesn't cause the glitch
        drawCircle(0,3);
        drawCircle(1,0);
        drawCircle(2,0);
        // this one alone causes the glitch
        drawCircle(0,4);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setMinWidth(720.0);
        primaryStage.setMinHeight(720.0);

        nbC = new SimpleIntegerProperty(7);
        nbR = new SimpleIntegerProperty(7);

        // création des Panel principaux
        anchor = new AnchorPane();
        scene = new Scene(anchor, WINDOW_SIZE, WINDOW_SIZE, Color.LIGHTBLUE);

        createPanes();
        display();


        primaryStage.setTitle("Flow");
        primaryStage.setScene(scene);

        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
}

Это прекрасно работает, если я не рисую 2 круга внутри панелей, так что это действительно связано с тем, как я их обрабатываю. Я положил круги в symbolesPane (а не в GridPane) и строки в linesPanes так что у меня могут быть линии за "символами" (здесь есть круги, но могут быть только изображения, поэтому я хочу нарисовать линии за изображениями).

Еще одна странная вещь: глюк появляется только с 4-м кружком в примере. Ребята, вы понимаете, в чем проблема?

0 ответов

Другие вопросы по тегам