-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLoader.java
More file actions
50 lines (41 loc) · 1.4 KB
/
ImageLoader.java
File metadata and controls
50 lines (41 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.awt.Image;
import java.util.*;
public class ImageLoader {
private static TreeMap<String, BufferedImage> cache = new TreeMap<String, BufferedImage>();
//loads an image that is ready for rendering
public static BufferedImage loadCompatibleImage(String path)
{
return getCompatibleImage(loadImage(path));
}
//loads raw image data for manipulation before rendering
public static BufferedImage loadImage(String path)
{
if(cache.containsKey(path))
return cache.get(path);
try {
java.io.InputStream stream = ImageLoader.class.getResourceAsStream(path);
BufferedImage img = javax.imageio.ImageIO.read(stream);
cache.put(path, img);
return img;
}
catch(Exception e) {
e.printStackTrace();
}
return null;
}
//creates a version of the image that is ready for rendering
private static BufferedImage getCompatibleImage(Image img)
{
BufferedImage buff = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage(img.getWidth(null), img.getHeight(null), Transparency.BITMASK);
Graphics g = buff.getGraphics();
g.drawImage(img, 0, 0, null);
g.dispose();
return buff;
}
}