JavaFX 圖像顯示

2021-08-25 15:43 更新

JavaFX 教程 - JavaFX 圖像顯示


JavaFX 可以在場景圖形上顯示標(biāo)準(zhǔn)圖像文件格式。

  • 使用javafx.scene.image.Image從本地文件系統(tǒng)或遠(yuǎn)程Web服務(wù)器加載圖像。
  • 使用javafx.scene.image.ImageView節(jié)點(diǎn)顯示圖像。

正在加載圖片

加載標(biāo)準(zhǔn)圖像文件格式JavaFX提供javafx.scene.image.Image API。Image類有很多很容易的構(gòu)造函數(shù),以便于不同的加載策略,如下面的列表所示:

Image(java.io.InputStream inputStream)

Image(java.io.InputStream is, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth)

Image(java.lang.String url)

Image(java.lang.String url, boolean backgroundLoading)

Image(java.lang.String url, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth)

Image(java.lang.String url, double requestedWidth, double requestedHeight, boolean preserveRatio, boolean smooth, boolean backgroundLoading)

可能介紹了每個參數(shù)。

參數(shù)數(shù)據(jù)類型/說明
輸入流java.io.InputStream
輸入流,如文件或網(wǎng)絡(luò)。
網(wǎng)址String
圖片的網(wǎng)址位置。
后臺加載中 
 boolean
從JavaFX應(yīng)用程序線程中加載背景中的圖片。
請求寬度double
指定圖像的調(diào)查框?qū)挾取?/td>
請求高度double
指定圖像的調(diào)查寬度。
玩游戲boolean
用于在邊框內(nèi)保持圖像的寬高比。
完整的boolean
真表示更,但更慢;否則渲染質(zhì)量較低但速度較快。

以下代碼加載兩個圖像,一個來自本地磁盤,另一個來自網(wǎng)絡(luò)。

import java.io.File;

import java.net.MalformedURLException;

import javafx.application.Application;

import javafx.scene.image.Image;

import javafx.stage.Stage;


public class Main extends Application {

    @Override

    public void start(Stage primaryStage) {

        try {

            File file = new File("C:/Users/abc/myphoto.jpg");

            String localUrl = file.toURI().toURL().toString();

            // don"t load in the background

            Image localImage = new Image(localUrl, false);

            String remoteUrl = "/attachments/jimg/Firefox.png";

            // load in the background

            Image remoteImage = new Image(remoteUrl, true);

            System.out.println(localUrl);

            System.out.println(remoteUrl);

        } catch (MalformedURLException ex) {

            // error

        }

    }

    public static void main(String[] args) {

        launch(args);

    }

}

顯示圖像

ImageView對象是一個可以顯示圖像的JavaFX Node對象。它可以有效果,執(zhí)行變換和縮放圖像。

當(dāng)ImageView節(jié)點(diǎn)應(yīng)用特殊效果(如圖像模糊)時,圖像的像素?cái)?shù)據(jù)被復(fù)制,計(jì)算并顯示在ImageView節(jié)點(diǎn)上。

下面代碼顯示如何創(chuàng)建ImageView對象。

Image  image  = new Image(url, true); 
ImageView imageView = new ImageView(image);

完整的源代碼

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        Group root = new Group();
        Scene scene = new Scene(root, 600, 330, Color.WHITE);
        
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);
        
        final ImageView imv = new ImageView();
        final Image image2 = new Image(Main.class.getResourceAsStream("button.png"));
        imv.setImage(image2);

        final HBox pictureRegion = new HBox();
        
        pictureRegion.getChildren().add(imv);
        gridpane.add(pictureRegion, 1, 1);
        
        
        root.getChildren().add(gridpane);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

旋轉(zhuǎn)ImageView

下面的代碼顯示了如何旋轉(zhuǎn)ImageView。

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage stage) {
        stage.setTitle("HTML");
        stage.setWidth(500);
        stage.setHeight(500);
        Scene scene = new Scene(new Group());
        VBox root = new VBox();    

        final ImageView selectedImage = new ImageView();   
        Image image1 = new Image(Main.class.getResourceAsStream("a.jpg"));
        selectedImage.setImage(image1);
        
        selectedImage.setRotate(90);
        
        root.getChildren().addAll(selectedImage);
        
        scene.setRoot(root);
 
        stage.setScene(scene);
        stage.show();
    }
 
    public static void main(String[] args) {
        launch(args);
    }
}

ImageView的setFitWidth

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        Group root = new Group();
        Scene scene = new Scene(root, 600, 330, Color.WHITE);
        
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);
        
        final ImageView imv = new ImageView();
        imv.setFitWidth(100);
        final Image image2 = new Image(Main.class.getResourceAsStream("button.png"));
        imv.setImage(image2);

        final HBox pictureRegion = new HBox();
        
        pictureRegion.getChildren().add(imv);
        gridpane.add(pictureRegion, 1, 1);
        
        
        root.getChildren().add(gridpane);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

setPreserveRatio for ImageView

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    public static void main(String[] args) {
        Application.launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Title");
        Group root = new Group();
        Scene scene = new Scene(root, 600, 330, Color.WHITE);
        
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);
        
        final ImageView imv = new ImageView();
        imv.setPreserveRatio(true);
        final Image image2 = new Image(Main.class.getResourceAsStream("button.png"));
        imv.setImage(image2);

        final HBox pictureRegion = new HBox();
        
        pictureRegion.getChildren().add(imv);
        gridpane.add(pictureRegion, 1, 1);
        
        
        root.getChildren().add(gridpane);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

JavaFX圖像縮放

import javafx.application.Application;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.ScrollEvent;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage stage) throws Exception {
    ImageView imageView = new ImageView();
    ScrollPane scrollPane = new ScrollPane();
    DoubleProperty zoomProperty = new SimpleDoubleProperty(200);

    zoomProperty.addListener(new InvalidationListener() {
      @Override
      public void invalidated(Observable arg0) {
        imageView.setFitWidth(zoomProperty.get() * 2);
        imageView.setFitHeight(zoomProperty.get() * 3);
      }
    });
    scrollPane.addEventFilter(ScrollEvent.ANY, new EventHandler<ScrollEvent>() {
      @Override
      public void handle(ScrollEvent event) {
        if (event.getDeltaY() > 0) {
          zoomProperty.set(zoomProperty.get() * 1.2);
        } else if (event.getDeltaY() < 0) {
          zoomProperty.set(zoomProperty.get() / 1.1);
        }
      }
    });
    imageView.setImage(new Image("http://yourImageURL"));
    imageView.preserveRatioProperty().set(true);
    scrollPane.setContent(imageView);
    stage.setScene(new Scene(scrollPane, 400, 300));
    stage.show();
  }

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


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號