简体   繁体   English

在JAR文件内的Res-Folder中创建数据

[英]Creating Data In A Res-Folder inside a JAR-File

for a project of mine i found it necessary to safe a given int[] of colorvalues as a png-file. 对于我的一个项目,我发现有必要将给定的int [] colorvalues保护为png文件。 The png will represent a map of tiles, which are represented by the colorvalues. png将代表一幅地图,这些地图由colorvalues表示。 The generation of the BufferedImage and the process of saving works fine already. BufferedImage的生成和保存过程已经可以正常工作了。

public BufferedImage getTileImage() {
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            image.setRGB(j, i, tiles[j+i*width].getType());
        }
    }
    return image;
}

public void safeMap(String mapname) {
    File file = new File("res/maps/"+mapname+".png");
    try {
        ImageIO.write(getTileImage(), "png",file );
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

The problem occurs, when i export it as an executeable JAR-File, since the ImageIO.write()-method doesnt seem to be able to work properly with relative pathes inside the JAR-File. 当我将其导出为可执行的JAR文件时,会出现问题,因为ImageIO.write()方法似乎无法正确使用JAR文件中的相对路径。 (My guess would be that you can only read inside the structure, not write) (我的猜测是您只能在结构内部读取,而不能写入)

What would be a good solution for this problem? 什么是解决这个问题的好方法? Is there a way to implement this, without using an external ressource-folder? 有没有不使用外部资源文件夹的情况下实现此方法的方法?

Dont save it to a JAR file. 不要将其保存到JAR文件中。 I also have a game that I need to create files that will go into the JAR file. 我还有一个游戏,需要创建要放入JAR文件的文件。 I create those files by executing code in my IDE. 我通过在IDE中执行代码来创建这些文件。 Those files go into the resource directories that will eventually get put into my JAR. 这些文件进入资源目录,这些目录最终将放入我的JAR。 In my case the directories are in /src/. 就我而言,目录位于/ src /中。 But they could easily have gone into src/main/resources if you are using Maven with standard dir layout. 但是,如果您使用标准目录布局的Maven,则它们很容易进入src / main / resources。

If you are executing this outside of your IDE, not part of your development effort, then you can store it in a tempt folder or a folder off of user.home. 如果您是在IDE外部执行此操作,而不是在开发工作中进行,则可以将其存储在tempt文件夹或user.home以外的文件夹中。 To get a temp file that goes into the system's temp folder you can do something like this... 要获得进入系统临时文件夹的临时文件,您可以执行以下操作...

File colorsFile = File.createTempFile("color_generator", ".png"); 

If the image is an asset of the game (generated by you for all players), generate it in the resources folder of your project, and include it when generating your jar file. 如果图像是游戏的资产(由您为所有玩家生成),则将其生成在项目的resources文件夹中,并在生成jar文件时将其包括在内。

If the image is generated dynamically (by a player), you can't put it in the jar, because you are not supposed to change a jar dynamically (and other players wouldn't see it, either) : you'll have to put it on the server / in your database and build a webservice to share it. 如果图像是由玩家动态生成的,则不能将其放入jar中,因为您不应该动态更改jar(其他玩家也不会看到):您必须将其放在服务器上/数据库中,并构建一个网络服务来共享它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM