简体   繁体   中英

why does JavaFX automatically resize images?

I have a 200x200 image.

在此处输入图像描述

I load the above image into an ImageView but when I run the JavaFX application, the image appears larger. I took a screen capture and opened it in Paint.NET to confirm.

在此处输入图像描述

Why is the image now 348x348? I tried everything I could think of to restrain the width and height to 200x200 (sample code below). This Images automatically resized post is the closest thing I could find relating to my question but I am not developing for Android.

public class ImageTest extends Application {

    @Override
    public void start(Stage stage) throws Exception {
        Pane pane = new Pane();
        pane.setPrefHeight(200);
        pane.setPrefWidth(200);
        pane.setMaxHeight(200);
        pane.setMaxWidth(200);

        Image image = new Image(getClass().getClassLoader().getResourceAsStream("images/red square.png"));
        ImageView imageView = new ImageView( image );
        imageView.setImage(image);
        imageView.setFitHeight(200);
        imageView.setFitWidth(200);
        imageView.setPreserveRatio(true);
        imageView.setSmooth(false);
        pane.getChildren().add(imageView);

        Scene scene = new Scene( pane, 200, 200 );
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

With Image in loading

You can try loading the image with a predefined size. By default, it will not preserve the width:height ratio: you can make it do so with

Image image = new Image("images/red square.png", 200, 200, true, false);

you can see information about that builder in javadocs


with image view use:

ImageView imageView = new ImageView("images/red square.png");
imageView.setPreserveRatio(true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM