-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageManager.java
More file actions
61 lines (50 loc) · 1.61 KB
/
Copy pathImageManager.java
File metadata and controls
61 lines (50 loc) · 1.61 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
import javax.swing.ImageIcon;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.Graphics2D;
/**
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();
}
/**
* Converts an Image to a BufferedImage.
* @param image The Image to convert
* @return A BufferedImage representation of the image
*/
public static BufferedImage toBufferedImage(Image image) {
if (image instanceof BufferedImage) {
return (BufferedImage) image;
}
// Create a buffered image with transparency
BufferedImage bufferedImage = new BufferedImage(
image.getWidth(null),
image.getHeight(null),
BufferedImage.TYPE_INT_ARGB
);
// Draw the image on to the buffered image
Graphics2D g2d = bufferedImage.createGraphics();
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
return bufferedImage;
}
/**
* Flips an image vertically (upside down).
* Used to flip enemy ship images for the alien invaders.
* @param image The image to flip
* @return A new Image that is flipped vertically
*/
public static Image flipImageVertically(Image image) {
BufferedImage buffered = toBufferedImage(image);
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -buffered.getHeight(null));
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
return op.filter(buffered, null);
}
}