Optimized code, everything is buffered in a buffered image now

This commit is contained in:
jancoow
2015-06-07 14:22:56 +02:00
parent 396769317c
commit 750f525c2d
15 changed files with 237 additions and 233 deletions
+38 -6
View File
@@ -1,5 +1,8 @@
package image;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
@@ -19,12 +22,12 @@ public class Images {
static {
try {
images.add(ImageIO.read(Main.class.getResource("/image/player.png")));
images.add(ImageIO.read(Main.class.getResource("/image/player2.png")));
images.add(ImageIO.read(Main.class.getResource("/image/pressstart.png")));
images.add(ImageIO.read(Main.class.getResource("/image/colorstrike.png")));
images.add(ImageIO.read(Main.class.getResource("/image/background.png")));
images.add(ImageIO.read(Main.class.getResource("/image/aanwijzers4sho.png")));
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/player.png"))));
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/player2.png"))));
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/pressstart.png"))));
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/colorstrike.png"))));
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/background.png"))));
images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/aanwijzers4sho.png"))));
} catch (IOException e) {
e.printStackTrace();
}
@@ -48,4 +51,33 @@ public class Images {
return bf;
}
public static BufferedImage toCompatibleImage(BufferedImage image)
{
// obtain the current system graphical settings
GraphicsConfiguration gfx_config = GraphicsEnvironment.
getLocalGraphicsEnvironment().getDefaultScreenDevice().
getDefaultConfiguration();
/*
* if image is already compatible and optimized for current system
* settings, simply return it
*/
if (image.getColorModel().equals(gfx_config.getColorModel()))
return image;
// image is not optimized, so create a new image that is
BufferedImage new_image = gfx_config.createCompatibleImage(
image.getWidth(), image.getHeight(), image.getTransparency());
// get the graphics context of the new image to draw the old image on
Graphics2D g2d = (Graphics2D) new_image.getGraphics();
// actually draw the image and dispose of context no longer needed
g2d.drawImage(image, 0, 0, null);
g2d.dispose();
// return the new optimized image
return new_image;
}
}