The post "
Get width and height of javafx.scene.image.Image" demonstrate how to get width and height of Image by calling Image's getWidth() and getHeight() methods. Another post "
Set fitWidth and fitHeight properties dynamically" demonstrate how to set ImageView auto-fit by calling ImageView's setFitWidth() and setFitHeight(). After auto-fited, we cannot get width and height by calling Image's getWidth() and getHeight() methods. Both of them return the original values, not the scaled values.
javafx.scene.image.ImageView provide getBoundsInLocal() and getBoundsInParent() methods to return the rectangular bounds of this Node.
- getBoundsInLocal(): Gets the value of the property boundsInLocal.
The rectangular bounds of this Node in the node's untransformed local coordinate space. For nodes that extend Shape, the local bounds will also include space required for a non-zero stroke that may fall outside the shape's geometry that is defined by position and size attributes. The local bounds will also include any clipping set with clip as well as effects set with effect.
- getBoundsInParent(): Gets the value of the property boundsInParent.
The rectangular bounds of this Node which include its transforms. boundsInParent is calculated by taking the local bounds (defined by boundsInLocal) and applying the transform created by setting the following additional variables:
- transforms ObservableList
- scaleX, scaleY
- rotate
- layoutX, layoutY
- translateX, translateY
Via the rectangular bounds, we can get the re-sized width and height by calling getWidth() and get Height() methods.
package javafxpixel;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.ScrollPane.ScrollBarPolicy;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javax.imageio.ImageIO;
/**
*
* @web http://java-buddy.blogspot.com/
*/
public class JavaFXPixel extends Application {
Label label;
ImageView myImageView;
ScrollPane scrollPane;
Boolean autoFix = true;
Image myImage;
@Override
public void start(Stage primaryStage) {
Button btnLoad = new Button("Load");
btnLoad.setOnAction(btnLoadEventListener);
Button btnAutoFix = new Button("Auto Fix");
btnAutoFix.setOnAction(btnAutoFixEventListener);
Button btnNoFix = new Button("No Fix");
btnNoFix.setOnAction(btnNoFixEventListener);
label = new Label();
myImageView = new ImageView();
scrollPane = new ScrollPane();
scrollPane.setPrefSize(400, 400);
scrollPane.setVbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setHbarPolicy(ScrollBarPolicy.AS_NEEDED);
scrollPane.setContent(myImageView);
HBox buttonBox = new HBox();
buttonBox.getChildren().addAll(btnLoad, btnAutoFix, btnNoFix);
VBox rootBox = new VBox();
rootBox.getChildren().addAll(buttonBox, scrollPane, label);
Scene scene = new Scene(rootBox, 400, 500);
primaryStage.setTitle("java-buddy.blogspot.com");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
EventHandler<ActionEvent> btnLoadEventListener
= new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(file);
myImage = SwingFXUtils.toFXImage(bufferedImage, null);
setFix();
} catch (IOException ex) {
Logger.getLogger(JavaFXPixel.class.getName()).log(Level.SEVERE, null, ex);
}
}
};
EventHandler<ActionEvent> btnAutoFixEventListener
= new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
autoFix = true;
setFix();
}
};
EventHandler<ActionEvent> btnNoFixEventListener
= new EventHandler<ActionEvent>(){
@Override
public void handle(ActionEvent t) {
autoFix = false;
setFix();
}
};
private void setFix(){
if(autoFix){
myImageView.setFitWidth(400);
myImageView.setFitHeight(400);
}else{
myImageView.setFitWidth(0);
myImageView.setFitHeight(0);
}
myImageView.setPreserveRatio(true);
myImageView.setSmooth(true);
myImageView.setCache(true);
myImageView.setImage(myImage);
scrollPane.setContent(null);
scrollPane.setContent(myImageView);
//int boundWidth = (int)myImageView.getBoundsInLocal().getWidth();
//int boundHeight = (int)myImageView.getBoundsInLocal().getHeight();
int boundWidth = (int)myImageView.getBoundsInParent().getWidth();
int boundHeight = (int)myImageView.getBoundsInParent().getHeight();
label.setText("width: " + boundWidth
+ " x height: " + boundHeight);
}
}