使用javafx从目录加载随机图像

使用javafx从目录加载随机图像,java,random,javafx,Java,Random,Javafx,如何使用javafx从目录加载随机图像(位于目录中的文件具有不同的名称) 目前,我正在使用以下命令加载特定文件 String image = JavaFXApplication4.class.getResource("/Images/Blue-Wallpaper.jpg").toExternalForm(); 我能想到的最佳解决方案是将路径(例如“/Images/Blue wallpar.jpg”)保存为数组中的字符串,然后使用如下所示的随机化器加载随机字符串: String[] paths;

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

目前,我正在使用以下命令加载特定文件

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

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

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

也许这会帮助你:

这是我的控制器类

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

}

多亏了帕特里克,我想出了一个简短的版本:

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);
images=newarraylist();
目录=新文件(“/”);
File[]files=目录.listFiles();
用于(文件f:文件)
{images.add(f.getName());}
System.out.println(图像);
int countImages=images.size();
int imageNumber=(int)(Math.random()*countImages);
字符串image=images.get(imageNumber);
System.out.println(图像);
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();
    }
}
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);