【发布时间】:2016-12-16 16:36:31
【问题描述】:
public static void saveMap(String fileName){
ArrayList<byte[]> mapData = new ArrayList<>();
for(int i = 0; i < DrawPanel.cells.size(); i++){
try {
if(DrawPanel.cells.get(i).image != null){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageIO.write(DrawPanel.cells.get(i).image,"png",byteArrayOutputStream);
byte[] bytes = byteArrayOutputStream.toByteArray();
mapData.add(i,bytes);
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(new File("res/Saved Maps/"+fileName+".map"));
for(int i = 0; i < mapData.size(); i++){
fileOutputStream.write(mapData.get(i));
}
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
它从每个单元格(JPanel)中获取图像,将其转换为字节并将其添加到数组列表中。然后它将数组列表写入文件。 我的问题是,我该如何扭转这种局面?这样我就可以将每个图像放入其各自的单元格中。
【问题讨论】:
-
请注意,当您从 .jar 文件运行时,写入应用程序资源会失败,因为资源是 .jar 存档中的条目,根本不是文件。
-
是我,还是这个问题闻起来像XY Problem?
标签: java image swing arraylist byte