Permalink
Browse files

convert to compatible image before drawing

  • Loading branch information...
1 parent 8dc1baf commit c2478fcf8e5fd96d0ec89e25de240d38873078bd @master-lincoln master-lincoln committed Mar 9, 2012
Showing with 35 additions and 2 deletions.
  1. +35 −2 src/com/mojang/mojam/MojamComponent.java
@@ -7,7 +7,10 @@
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
+import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.Robot;
import java.awt.Toolkit;
@@ -510,11 +513,41 @@ private synchronized void render(Graphics g) {
// render mouse
renderMouse(screen, mouseButtons);
-
- g.drawImage(screen.image, 0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE, null);
+ BufferedImage image = toCompatibleImage(screen.image);
+ g.drawImage(image, 0, 0, GAME_WIDTH * SCALE, GAME_HEIGHT * SCALE, null);
}
}
+
+ private 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;
+ }
+
private void addHealthBar(Screen screen) {
int maxIndex = Art.panel_healthBar[0].length - 1;

0 comments on commit c2478fc

Please sign in to comment.