0

I have a GridPane (4x5), all it's cells have as child an AnchorPane which cointains an ImageView. I need to resize the image so it fully cover the cell as soon as the gridPane (and thus it's cells) change size.

I managed to resize the image correctly when the size of the cell grows, but when the cell gets tinier the image doesn't resize back. This leads into partially covering images of the confinant cells.

Can anyone explain what i'm doing wrong or give me the instruction to implement a proper resize?

This is my code:

ImageView image = new ImageView("/dice/" + draftList.get(i) + ".png");
AnchorPane pane = ((AnchorPane)(gridpane.getChildren().get(i)));
pane.getChildren().add(image);
fitToParent(image,pane);

//method in the same class
private void fitToParent(ImageView image, AnchorPane pane) {
    image.fitWidthProperty().bind(pane.widthProperty());
    image.fitHeightProperty().bind(pane.heightProperty());
}
Luca
  • 21
  • 2
  • I tried to color the AnchorPanes to see if they actually resize correctly and they do, but still the ImageView does not. – Luca Jun 21 '18 at 17:44

1 Answers1

0

You can try to use the setPreserveRatio(boolean) function of the ImageView class to true. This will allow you to keep the aspect ratio constant. Eg:

ImageView iv = new ImageView(/*file path*/);
iv.setPreserveRatio(true);

Src: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/ImageView.html

Other than this you can also try to limit the resizable property to false or set the min width and height so that the image is not partially covered

Src: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/layout/Region.html#resize-double-double-