eclipse 如何将URI替换为输入流,在更新功能时按顺序访问图像

vs3odd8k  于 2023-02-22  发布在  Eclipse
关注(0)|答案(1)|浏览(119)
public static BufferedImage[] GetAllLevels() {

    URL url = LoadSave.class.getResource("/lvls");
    File file = null;

    try {
        file = new File(url.toURI());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    File[] files = file.listFiles();
    File[] filesSorted = new File[files.length];

    for (int i = 0; i < filesSorted.length; i++)
        for (int j = 0; j < files.length; j++) {
            if (files[j].getName().equals((i + 1) + ".png"))
                filesSorted[i] = files[j];

        }

    BufferedImage[] imgs = new BufferedImage[filesSorted.length];

    for (int i = 0; i < imgs.length; i++)
        try {
            imgs[i] = ImageIO.read(filesSorted[i]);
        } catch (IOException e) {
            e.printStackTrace();
        }

    return imgs;
}

我刚开始,所以我从某个地方取了代码,但找不到替代方法
以下是一些错误:

Exception in thread "main" java.lang.reflect. InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImp1.invoke®(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImp1. invoke (NativeMethodAccessorImp1. java:77)
    at java.base/jdk.internal.reflect .DelegatingMethodAccessorImp1.invoke (DelegatingMethodAccessorImp1. java:43)
    at java.base/java.lang.reflect.Method. invoke (Method. java: 568)
    at org.eclipse.jdt.internal.jarinjarloader. JarRsrcLoader.main(JarRsrcLoader.java: 61)

    > Caused by: java. lang. IllegalArgumentException: URI is not hierarchical
    > at java.base/java.io.File.‹init›(File. java: 420)
    > at utilz. LoadSave.GetAllLevels (LoadSave. java:77)
    > at levels.LevelManager.buildAllLevels(LevelManager.java:41)
    > at levels. LevelManager.<init›(LevelManager.java:23) at gamestates.Playing.initClasses (Playing. java: 130) 
    > at gamestates.Playing.<init>(Playing.java:64) 
    > at main.Game.initClasses(Game. java: 54) at main. Game.<init> (Game. java: 40) 
    > at main.MainClass.main(Mainclass.lava:6)
ubof19bj

ubof19bj1#

final int MAX_IMAGES = 10;
    for(int i = 0;i < MAX_IMAGES;i++) {  
        String fileName = String.format("%02d.png", i);
        InputStream in = getClass().getResourceAsStream("/images/" + fileName);
        if (in == null) {
            break;
        }
        else {
            // Process
        }
    }

可以作为一种策略

相关问题