0

I just started using JavaFX and I can't seem to solve this problem. My canvases do not seem to fill the complete space available to them. I set a size to my canvases, and they do have this size, but the pane around it becomes larger. This results in a white border on the right hand side and at the bottom of the Pane.

My code:

@Override
public void start(Stage primaryStage) throws Exception {
    primaryStage.setTitle("ShellShock");
    primaryStage.setResizable(false);

    final Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit().getScreenSize();

    Screen screen = new Screen(SCREEN_SIZE.getWidth() * 0.9, SCREEN_SIZE.getHeight() * 0.9);

    Scene scene = new Scene(screen);

    primaryStage.setScene(scene);
    primaryStage.show();
}

Screen class:

public class Screen extends Pane {

private Canvas[] canvases;

public Screen(double width, double height) {
    this.setPrefSize(width, height);

    this.canvases = new Canvas[2];

    Canvas background = new Canvas(width, height);
    GraphicsContext gcb = background.getGraphicsContext2D();
    gcb.setFill(new LinearGradient(0, 0, 0, 1, true, CycleMethod.REFLECT, new Stop(0.4, Color.BLUE.darker()),
            new Stop(1.0, Color.BLUE.brighter().brighter())));
    gcb.fillRect(0, 0, width, height);
    Canvas foreground = new Canvas(width, height);

    this.canvases[0] = background;
    this.canvases[1] = foreground;

    this.getChildren().add(background);
    this.getChildren().add(foreground);
}
}
Artorian
  • 1
  • 1
  • 4
  • 1
    Please edit your question to include the rest of the `Screen` class—in particular, the class declaration, so we can see its inheritance. – VGR Jun 26 '16 at 14:08
  • I editted, all code is here now. – Artorian Jun 26 '16 at 16:25
  • Possible duplicate of [*Automatically resize Canvas to fill the enclosing Parent*](http://stackoverflow.com/q/31761361/230513). – trashgod Jun 26 '16 at 17:41
  • I do not think this is a duplicate, since for me there is no resizing involved at all. I give the screen a size, this size is shared for the canvases, and is never changed. – Artorian Jun 26 '16 at 18:00
  • I don't see any white unless I resize the scene. I'd use [`CanvasPane`](http://stackoverflow.com/a/31761362/230513) rather than `setResizable(false)`. Instead of `Toolkit`, try `Screen`, as suggested in this possible duplicate: [*How to get current screen details in javafx?*](http://stackoverflow.com/q/20176340/230513) – trashgod Jun 26 '16 at 18:46
  • Settings resizable to false seems to be the problem. When i delete this line, the problem does not occur. thanks everyone. – Artorian Jun 26 '16 at 20:57

1 Answers1

0

Removing the setResizable(false); line seems to solve the problem.

Also placing primaryStage.sizeToScene(); after setResizable(false); seems to solve the problem.

DVarga
  • 21,311
  • 6
  • 55
  • 60
Artorian
  • 1
  • 1
  • 4