final/src/BufferedImageWrapper.java

77 lines
2.5 KiB
Java
Raw Normal View History

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.IndexColorModel;
import java.awt.image.WritableRaster;
import java.io.*;
import java.util.Hashtable;
public class BufferedImageWrapper implements Serializable {
2022-06-16 03:08:35 +01:00
transient public BufferedImage image;
public String imageString;
public Boolean flipImage = false;
public BufferedImageWrapper(int width, int height, int imageType) {
image = new BufferedImage(width, height, imageType);
}
public BufferedImageWrapper(int width, int height, int imageType, IndexColorModel cm) {
image = new BufferedImage(width, height, imageType, cm);
}
public BufferedImageWrapper(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?, ?> properties) {
image = new BufferedImage(cm, raster, isRasterPremultiplied, properties);
}
public BufferedImageWrapper(BufferedImage image) {
this.image = image;
}
2022-06-16 03:08:35 +01:00
public BufferedImageWrapper(String imageString) throws IOException {
image = GamePanel.getImage(imageString);
this.imageString = imageString;
}
public BufferedImageWrapper(String imageString, boolean flip) throws IOException {
BufferedImage temporaryImage = GamePanel.getImage(imageString);
if (flip) {
image = GamePanel.flipImageHorizontally(temporaryImage);
} else {
image = temporaryImage;
}
flipImage = flip;
this.imageString = imageString;
}
public BufferedImageWrapper() {}
@Serial
private void writeObject(ObjectOutputStream out) throws IOException {
2022-06-16 03:08:35 +01:00
out.writeObject(flipImage);
if (imageString != null) {
out.writeObject(imageString);
} else {
ImageIO.write(image, "png", out); // png is lossless
}
}
@Serial
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
2022-06-16 03:08:35 +01:00
Object o;
flipImage = (Boolean)in.readObject();
o = in.readObject();
if (o instanceof String) {
BufferedImage temporaryImage;
2022-06-16 03:08:35 +01:00
this.imageString = (String)o;
temporaryImage = GamePanel.getImage(imageString);
if (flipImage) {
image = GamePanel.flipImageHorizontally(temporaryImage);
} else {
image = temporaryImage;
}
2022-06-16 03:08:35 +01:00
} else {
image = ImageIO.read(in);
}
}
}