From a7a530a9aa2a5a473f1d64d510113c50fd9d4131 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjist=C3=B3f?= Date: Sat, 14 May 2016 20:46:55 +0200 Subject: [PATCH] added ImageStorage to precache images to remove IO bottleneck --- cicaprojekt/ImageStorage.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 cicaprojekt/ImageStorage.java diff --git a/cicaprojekt/ImageStorage.java b/cicaprojekt/ImageStorage.java new file mode 100644 index 0000000..8b980af --- /dev/null +++ b/cicaprojekt/ImageStorage.java @@ -0,0 +1,31 @@ +package cicaprojekt; + +import javax.imageio.ImageIO; +import javax.swing.filechooser.FileNameExtensionFilter; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + + +public class ImageStorage { + private Map images; + + public ImageStorage() throws IOException { + images = new HashMap<>(); + + File dir = new File(System.getProperty("user.dir")); + FileNameExtensionFilter filter = new FileNameExtensionFilter("N/A", "png"); + + for (File f : dir.listFiles()) { + if (filter.accept(f) && f.isFile()){ + images.put(f.getName(), ImageIO.read(f)); + } + } + } + + public BufferedImage getImage(String filename) { + return images.get(filename); + } +}