举报投诉联系我们 手机版 热门标签 VUE中文网
您的位置:VUE中文网 > javafx 显示 JavaFX 像显示

javafx 显示 JavaFX 像显示

2023-03-04 06:17 Java教程

javafx 显示 JavaFX 像显示

javafx 显示

JavaFX是一种用于开发和部署跨平台的桌面应用程序、移动应用程序和嵌入式设备的Java软件开发工具包。它是一个完整的GUI工具包,提供了一个可以使用Java语言创建图形界面的框架。它还提供了一个可以使用JavaFX Script语言创建图形界面的框架。

JavaFX有助于开发者快速创建出吸引人的图形界面,而不必耗费大量时间去学习新的语言或者API。它还能够帮助开发者在不同平台上部署相同的应用,而不必重新编写代码。

JavaFX有一个内置的图形界面工具包(GUI),它能够帮助开发者快速创建出吸引人的图形界面。GUI工具包中包含了大量的封装好的UI元素(如文本框、标签、滚动条、对话框、表格、树形视图、日历选择器、时间选择器、进度条以及很多其他UI元素),这些UI元素能够帮助开发者快速创建出吸引人的图形界面。

此外,JavaFX还有一个内置的2D/3D图形API(Application Programming Interface)(也就是Graphics API) : JavaFX 2D/3D Graphics API 。它能够帮助开发者在应用中使用2D/3D图形来显示信息或者衍生出特效来吸引用户注意力。

// 加载 JavaFX 类 
import javafx.application.Application; 
import javafx.stage.Stage; 
// 继承 Application 类 
public class MyApp extends Application { 

    // 重写 start 方法 
    @Override 
    public void start(Stage primaryStage) { 

        // 在此处显示你想要显示地内容  

    }  

    public static void main(String[] args) {  

        launch(args);  

    }  
}

JavaFX 图像显示

JavaFX 教程 - JavaFX 图像显示


JavaFX 可以在场景图形上显示标准图像文件格式。

  • 使用javafx.scene.image.Image从本地文件系统或远程Web服务器加载图像。
  • 使用javafx.scene.image.ImageView节点显示图像。

正在加载图片

加载标准图像文件格式JavaFX提供javafx.scene.image.Image API。Image类有很多很容易的构造函数,以便于不同的加载策略,如下面的列表所示:

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)

可能介绍了每个参数。

参数数据类型/说明
输入流java.io.InputStream
输入流,如文件或网络。
网址String
图片的网址位置。
后台加载中 
 boolean
从JavaFX应用程序线程中加载背景中的图片。
请求宽度double
指定图像的调查框宽度。
请求高度double
指定图像的调查宽度。
玩游戏boolean
用于在边框内保持图像的宽高比。
完整的boolean
真表示更,但更慢;否则渲染质量较低但速度较快。

以下代码加载两个图像,一个来自本地磁盘,另一个来自网络。

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对象。它可以有效果,执行变换和缩放图像。

当ImageView节点应用特殊效果(如图像模糊)时,图像的像素数据被复制,计算并显示在ImageView节点上。

下面代码显示如何创建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();
    }
}

旋转ImageView

下面的代码显示了如何旋转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);
  }
}


阅读全文
以上是VUE中文网为你收集整理的javafx 显示 JavaFX 像显示全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 VUE中文网 vue88.com 版权所有 联系我们