【问题标题】:Load random Image from a directory using javafx使用 javafx 从目录加载随机图像
【发布时间】:2014-02-21 01:51:03
【问题描述】:

如何使用 javafx 从目录中加载随机图像,(位于目录中的文件具有不同的名称)

目前我正在使用以下内容加载特定文件

String image = JavaFXApplication4.class.getResource("/Images/Blue-Wallpaper.jpg").toExternalForm();

【问题讨论】:

    标签: java random javafx


    【解决方案1】:

    我能想到的最佳解决方案是将路径(例如“/Images/Blue-Wallpaper.jpg”)保存为数组中的字符串,然后使用如下所示的随机器加载随机字符串:

    String[] paths;
    int index = Math.random()*paths.length;
    String image = JavaFXApplication4.class.getResource(paths[index]).toExternalForm();
    

    【讨论】:

      【解决方案2】:

      赫吉·奥萨马,

      也许这会对你有所帮助:

      这是我的控制器类

      package de.professional_webworkx.jfx.controller;
      
      import java.io.File;
      import java.net.URL;
      import java.util.ArrayList;
      import java.util.List;
      import java.util.ResourceBundle;
      
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.fxml.FXML;
      import javafx.fxml.Initializable;
      import javafx.scene.control.Button;
      import javafx.scene.image.Image;
      import javafx.scene.image.ImageView;
      
      public class MainController implements Initializable {
      
          @FXML
          private ImageView imgView;
      
          @FXML
          private Button loadImg;
      
          private List<String> images;
      
          public void initialize(URL location, ResourceBundle bundle) {
      
              loadImages(new File("images"));
              loadImg.setOnAction(new EventHandler<ActionEvent>() {
      
                  public void handle(ActionEvent ae) {
                      imgView.setImage(loadRandomImages());
                  }
              });
          }
      
          private Image loadRandomImages() {
              int countImages = images.size();
              int imageNumber = (int) (Math.random() * countImages);
      
              String image = images.get(imageNumber);
              return new Image(image);
          }
      
          private void loadImages(final File directory) {
              if(images == null) {
                  images = new ArrayList<String>();
              } else {
                  images.clear();
              }
      
              File[] files = directory.listFiles();
              for(File f : files) {
                  if(f.isDirectory()) {
                      loadImages(f);
                  } else {
                      images.add(f.getName());
                  }
              }
          }
      
      }
      

      我使用 Scene Builder 来绘制小 GUI。

      <?xml version="1.0" encoding="UTF-8"?>
      
      <?import java.lang.*?>
      <?import javafx.scene.control.*?>
      <?import javafx.scene.image.*?>
      <?import javafx.scene.layout.*?>
      
      <AnchorPane fx:id="mainPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="de.professional_webworkx.jfx.controller.MainController">
      <children><SplitPane dividerPositions="0.8417085427135679" focusTraversable="true" orientation="VERTICAL" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <items>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
      <children><ImageView fx:id="imgView" fitHeight="331.0" fitWidth="598.0" pickOnBounds="true" preserveRatio="true" />
      </children></AnchorPane>
          <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
      <children><Button fx:id="loadImg" layoutX="268.2275390625" layoutY="16.5" mnemonicParsing="false" text="Load" />
      </children></AnchorPane>
        </items>
      </SplitPane>
      </children></AnchorPane>
      

      最后是 App.class

      package de.professional_webworkx.jfx;
      
      import javafx.application.Application;
      import javafx.fxml.FXMLLoader;
      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.stage.Stage;
      
      public class App extends Application
      {
          public static void main( String[] args )
          {
              Application.launch(args);
          }
      
          @Override
          public void start(Stage stage) throws Exception {
              stage.setTitle("Imageviewer");
              Parent parent = FXMLLoader.load(getClass().getResource("images.fxml"));
              Scene scene = new Scene(parent);
              stage.setScene(scene);
              stage.show();
          }
      }
      

      【讨论】:

        【解决方案3】:

        感谢 Patrick,我想出了一个简短的版本:

        images = new ArrayList<String>();
        directory = new File("/");
        
        File[] files = directory.listFiles();
        for(File f : files) 
        {images.add(f.getName());}   
        System.out.println(images);
        int countImages = images.size();
        int imageNumber = (int) (Math.random() * countImages);
        String image = images.get(imageNumber);
        System.out.println(image);
        

        【讨论】:

        • 查看answer 了解从给定文件夹加载随机文件的简单方法。
        猜你喜欢
        • 2012-02-09
        • 1970-01-01
        • 2015-03-13
        • 1970-01-01
        • 2012-08-06
        • 1970-01-01
        • 2021-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多