Saturday, January 19, 2013

Set fitWidth and fitHeight properties dynamically

The article "Auto fit JavaFX 2 ImageView" demonstrate how to set ImageView auto-fix by setting fitWidth and fitHeight properties. To remove the auto-fix function, we can call setFitWidth() and setFitHeight(), then the intrinsic height of the image will be used as the fitHeight. Such that we can set/remove auto-fix at run-time.

Set fitWidth and fitHeight properties dynamically


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.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;
    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();
        
        HBox buttonBox = new HBox();
        buttonBox.getChildren().addAll(btnLoad, btnAutoFix, btnNoFix);
 
        VBox rootBox = new VBox();
        rootBox.getChildren().addAll(buttonBox, label, myImageView);
        
        Scene scene = new Scene(rootBox, 500, 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);
                myImageView.setImage(myImage);

                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);
    }
}



Next: Set fitWidth and fitHeight properties dynamically, with scroll bar.

2 comments:

  1. Hi,

    Nice example.
    Do you know if we can get the width and height of the resized image inside the ImageView control ?
    imageView.getImage().getWidth() give the original size.

    ReplyDelete