-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageManager.java
More file actions
119 lines (94 loc) · 4.36 KB
/
ImageManager.java
File metadata and controls
119 lines (94 loc) · 4.36 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
// Image loading and processing with BufferedImage support.
public class ImageManager {
public ImageManager() {
}
public static Image loadImage(String fileName) {
return new ImageIcon(fileName).getImage();
}
public static BufferedImage loadBufferedImage(String filename) {
BufferedImage bi = null;
File file = new File(filename);
try {
bi = ImageIO.read(file);
} catch (IOException ioe) {
System.out.println("Error opening file " + filename + ": " + ioe);
}
return bi;
}
public static BufferedImage copyImage(BufferedImage src) {
if (src == null)
return null;
int imWidth = src.getWidth();
int imHeight = src.getHeight();
BufferedImage copy = new BufferedImage(imWidth, imHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = copy.createGraphics();
g2d.drawImage(src, 0, 0, null);
g2d.dispose();
return copy;
}
// Scale a BufferedImage to the specified width and height.
private static BufferedImage scaleImage(BufferedImage src, int newWidth, int newHeight) {
if (src == null) return null;
BufferedImage scaled = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = scaled.createGraphics();
g2.drawImage(src, 0, 0, newWidth, newHeight, null);
g2.dispose();
return scaled;
}
// Scale a BufferedImage to a specific height while maintaining aspect ratio.
public static BufferedImage scaleImageToHeight(BufferedImage src, int targetHeight) {
if (src == null) return null;
int newWidth = (int) (src.getWidth() * ((double) targetHeight / src.getHeight()));
return scaleImage(src, newWidth, targetHeight);
}
// Scale a BufferedImage to a specific width while maintaining aspect ratio.
public static BufferedImage scaleImageToWidth(BufferedImage src, int targetWidth) {
if (src == null) return null;
int newHeight = (int) (src.getHeight() * ((double) targetWidth / src.getWidth()));
return scaleImage(src, targetWidth, newHeight);
}
// Load and scale tree images to max height.
public static BufferedImage[] loadTreeImages(int targetHeight) {
BufferedImage[] trees = new BufferedImage[6];
BufferedImage[] originals = new BufferedImage[6];
originals[0] = loadBufferedImage("images/trees/Tree1.png");
originals[1] = loadBufferedImage("images/trees/Tree2.png");
originals[2] = loadBufferedImage("images/trees/Tree3.png");
originals[3] = loadBufferedImage("images/trees/Tree4.png");
originals[4] = loadBufferedImage("images/trees/Tree5.png");
originals[5] = loadBufferedImage("images/trees/Tree6.png");
for (int i = 0; i < originals.length; i++) {
if (originals[i] != null) {
trees[i] = scaleImageToHeight(originals[i], targetHeight);
}
}
return trees;
}
// Load and scale rock images to max width.
public static BufferedImage[] loadRockImages(int targetWidth) {
BufferedImage[] rocks = new BufferedImage[9];
BufferedImage[] originals = new BufferedImage[9];
originals[0] = loadBufferedImage("images/rocks/Rock1.png");
originals[1] = loadBufferedImage("images/rocks/Rock2.png");
originals[2] = loadBufferedImage("images/rocks/Rock3.png");
originals[3] = loadBufferedImage("images/rocks/Rock4.png");
originals[4] = loadBufferedImage("images/rocks/Rock5.png");
originals[5] = loadBufferedImage("images/rocks/Rock6.png");
originals[6] = loadBufferedImage("images/rocks/Rock7.png");
originals[7] = loadBufferedImage("images/rocks/Rock8.png");
originals[8] = loadBufferedImage("images/rocks/Rock9.png");
for (int i = 0; i < originals.length; i++) {
if (originals[i] != null) {
rocks[i] = scaleImageToWidth(originals[i], targetWidth);
}
}
return rocks;
}
}