This commit is contained in:
ngb
2022-07-01 13:05:52 +02:00
parent a31ec502d4
commit 62dc4190a1

View File

@@ -1,43 +1,46 @@
package schule.ngb.zm.util; package schule.ngb.zm.util;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class ImageLoader { public class ImageLoader {
public static boolean cacheing = true; public static boolean caching = true;
private static HashMap<String, BufferedImage> imageCache = new HashMap<>(); private static Map<String, BufferedImage> imageCache = new ConcurrentHashMap<>();
public static BufferedImage loadImage( String source ) { public static BufferedImage loadImage( String source ) {
return loadImage(source, cacheing); return loadImage(source, caching);
} }
/** /**
* Läadt ein Bild von der angegebenen Quelle <var>source</var> und gibt das * Läadt ein Bild von der angegebenen Quelle <var>source</var> und gibt das
* Bild zurück oder <code>null</code>, wenn das Bild nicht geladen werden * Bild zurück oder <code>null</code>, wenn das Bild nicht geladen werden
* konnte. Ist ein Bild mit der angegebenen Quelle im Cache, wird das * konnte. Ist ein Bild mit der angegebenen Quelle im Cache, wird das
* gechachete Bild zurückgegeben. Dies kann mit <code>cacheing = false</code> * gechachete Bild zurückgegeben. Dies kann mit <code>caching =
* verhindert werden. * false</code> verhindert werden.
* <p> * <p>
* Wurde chacheing global deaktiviert, kann mit <code>cacheing = true</code> * Wurde chacheing global deaktiviert, kann mit <code>caching = true</code>
* das Bild trotzdem aus dem Cache geladen werden, wenn es vorhanden ist. * das Bild trotzdem aus dem Cache geladen werden, wenn es vorhanden ist.
* *
* @param source * @param source
* @param cacheing * @param caching
* @return * @return
*/ */
public static BufferedImage loadImage( String source, boolean cacheing ) { public static BufferedImage loadImage( String source, boolean caching ) {
if( source == null || source.length() == 0 ) if( source == null || source.length() == 0 )
throw new IllegalArgumentException("Image source may not be null or empty."); throw new IllegalArgumentException("Image source may not be null or empty.");
if( cacheing && imageCache.containsKey(source) ) { if( caching && imageCache.containsKey(source) ) {
BufferedImage cachedImage = imageCache.get(source); BufferedImage cachedImage = imageCache.get(source);
if( cachedImage != null ) { if( cachedImage != null ) {
return cachedImage; return cachedImage;
@@ -68,12 +71,13 @@ public class ImageLoader {
img = ImageIO.read(url); img = ImageIO.read(url);
} }
if( cacheing && img != null ) { if( caching && img != null ) {
imageCache.put(source, img); imageCache.put(source, img);
} }
return img; return img;
} catch( IOException ioe ) { } catch( IOException ioe ) {
// TODO: Log error!
return null; return null;
} }
} }
@@ -88,7 +92,7 @@ public class ImageLoader {
*/ */
public static boolean preloadImage( String name, String source ) { public static boolean preloadImage( String name, String source ) {
BufferedImage img = loadImage(source, true); BufferedImage img = loadImage(source, true);
if( cacheing && img != null ) { if( caching && img != null ) {
imageCache.put(name, img); imageCache.put(name, img);
return true; return true;
} }
@@ -133,11 +137,11 @@ public class ImageLoader {
} }
public static void enableCaching() { public static void enableCaching() {
cacheing = true; caching = true;
} }
public static void disableCaching() { public static void disableCaching() {
cacheing = false; caching = false;
} }
@@ -194,4 +198,6 @@ public class ImageLoader {
.createCompatibleImage(width, height, type); .createCompatibleImage(width, height, type);
} }
private ImageLoader() {}
} }