I would like an explanation as to why some white space appears around my content when I set the stage's re-sizable property to false. I want to prevent the user from resizing the window, but keep the window so that there is no white space showing.
Things I have tried: I have tried setting the min and max height of every element, pane, grid, stage, etc... I have tried setting the margins and paddings and insets to zero. I also tried something weird I read online that involved setting the insets to -1. Some of these work, but they involve seemingly random values needing to be added or subtracted from the GAME_HEIGHT and GAME_WIDTH. For example, setting
Scene scene = new Scene(pane, GAME_WIDTH-12, GAME_HEIGHT-12);
works but seems too arbitrary and it feels wrong to hard-code random 12's in.
Here is the minimum code needed to recreate what is happening:
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
final int GAME_HEIGHT = 480;
final int GAME_WIDTH = 736;
Image img = new Image("file:test.gif");
GridPane grid = new GridPane();
Pane pane = new Pane();
for(int row=0; row<15; row++)
for(int column=0; column<23; column++) {
ImageView view = new ImageView(img);
view.setViewport(new Rectangle2D(0, 0, 32, 32));
grid.add(view, column, row);
}
// Imageview viewports are 32x32
// 15*32 = 480 = GAME_HEIGHT
// 23*32 = 736 = GAME_WIDTH
pane.getChildren().addAll(grid);
Scene scene = new Scene(pane, GAME_WIDTH, GAME_HEIGHT);
primaryStage.setScene(scene);
//primaryStage.setResizable(false); // commenting this out makes the game window the correct size
primaryStage.show();
}
1. Before setting the stage resizable to false.
2. After setting resizable white space appears on the right and bottom.
3. After setting the stages max height and width, it zoom cuts the content off.