-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageManager.java
100 lines (64 loc) · 1.97 KB
/
ImageManager.java
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
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.Graphics2D;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
/**
The ImageManager class manages the loading and processing of images.
*/
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;
}
// make a copy of the BufferedImage src
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();
// copy image
g2d.drawImage(src, 0, 0, null);
g2d.dispose();
return copy;
}
public static BufferedImage vFlipImage(BufferedImage src) {
int imWidth = src.getWidth();
int imHeight = src.getHeight();
BufferedImage dest = new BufferedImage (imWidth, imHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dest.createGraphics();
// Perform vertical flip
g2d.drawImage(src, imWidth, 0, 0,imHeight,
0, 0, imWidth, imHeight, null);
return dest;
}
public static BufferedImage hFlipImage(BufferedImage src) {
int imWidth = src.getWidth();
int imHeight = src.getHeight();
BufferedImage dest = new BufferedImage (imWidth, imHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = dest.createGraphics();
// Perform vertical flip
g2d.drawImage(src, 0, imHeight, imWidth, 0,
0, 0, imWidth, imHeight, null);
return dest;
}
}