-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.java
More file actions
48 lines (37 loc) · 1.3 KB
/
Copy pathUtil.java
File metadata and controls
48 lines (37 loc) · 1.3 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
package ui;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
public class Util {
private static final HashMap<String, BufferedImage> cache = new HashMap<>();
public static BufferedImage loadImage(String path) {
BufferedImage image = null;
if (cache.containsKey(path)) {
return cache.get(path);
}
try {
image = ImageIO.read(new File(path));
if (!cache.containsKey(path)) {
cache.put(path, image);
}
} catch (IOException e) {
e.printStackTrace();
}
return image;
}
public static BufferedImage scaleImage(Integer newWidth, Integer newHeight, BufferedImage oldImage) {
Image scaledImage = oldImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
BufferedImage newImage = new BufferedImage(
scaledImage.getWidth(null),
scaledImage.getHeight(null),
BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = newImage.createGraphics();
bGr.drawImage(scaledImage, 0, 0, null);
bGr.dispose();
return newImage;
}
}