code_junkie

Java2D Performance Issues

Nov 11th, 2011
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  1. private BufferedImage toCompatibleImage(BufferedImage image)
  2. {
  3. // obtain the current system graphical settings
  4. GraphicsConfiguration gfx_config = GraphicsEnvironment.
  5. getLocalGraphicsEnvironment().getDefaultScreenDevice().
  6. getDefaultConfiguration();
  7.  
  8. /*
  9. * if image is already compatible and optimized for current system
  10. * settings, simply return it
  11. */
  12. if (image.getColorModel().equals(gfx_config.getColorModel()))
  13. return image;
  14.  
  15. // image is not optimized, so create a new image that is
  16. BufferedImage new_image = gfx_config.createCompatibleImage(
  17. image.getWidth(), image.getHeight(), image.getTransparency());
  18.  
  19. // get the graphics context of the new image to draw the old image on
  20. Graphics2D g2d = (Graphics2D) new_image.getGraphics();
  21.  
  22. // actually draw the image and dispose of context no longer needed
  23. g2d.drawImage(image, 0, 0, null);
  24. g2d.dispose();
  25.  
  26. // return the new optimized image
  27. return new_image;
  28. }
Add Comment
Please, Sign In to add comment