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

时间:2014-01-29 10:19:23

标签: java random javafx

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

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

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

3 个答案:

答案 0 :(得分:1)

Hej Ossama,

也许这会对你有所帮助:

这是我的Controller类

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

感谢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);