Images.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. package image;
  2. import java.awt.Graphics2D;
  3. import java.awt.GraphicsConfiguration;
  4. import java.awt.GraphicsEnvironment;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import javax.imageio.ImageIO;
  10. import main.Main;
  11. public class Images {
  12. public static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
  13. public Images() {
  14. }
  15. static {
  16. try {
  17. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/player.png"))));
  18. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/player2.png"))));
  19. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/pressstart.png"))));
  20. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/colorstrike.png"))));
  21. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/background.png"))));
  22. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/aanwijzers4sho.png"))));
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. }
  26. }
  27. public static BufferedImage getImage(ImageType img) {
  28. return images.get(img.ordinal());
  29. }
  30. public enum ImageType {
  31. player,player2,pressstart,colorstrike,background,aanwijzers
  32. }
  33. public static BufferedImage readImage(File f) {
  34. BufferedImage bf = null;
  35. try {
  36. bf = ImageIO.read(f);
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. return bf;
  41. }
  42. public static BufferedImage toCompatibleImage(BufferedImage image)
  43. {
  44. // obtain the current system graphical settings
  45. GraphicsConfiguration gfx_config = GraphicsEnvironment.
  46. getLocalGraphicsEnvironment().getDefaultScreenDevice().
  47. getDefaultConfiguration();
  48. /*
  49. * if image is already compatible and optimized for current system
  50. * settings, simply return it
  51. */
  52. if (image.getColorModel().equals(gfx_config.getColorModel()))
  53. return image;
  54. // image is not optimized, so create a new image that is
  55. BufferedImage new_image = gfx_config.createCompatibleImage(
  56. image.getWidth(), image.getHeight(), image.getTransparency());
  57. // get the graphics context of the new image to draw the old image on
  58. Graphics2D g2d = (Graphics2D) new_image.getGraphics();
  59. // actually draw the image and dispose of context no longer needed
  60. g2d.drawImage(image, 0, 0, null);
  61. g2d.dispose();
  62. // return the new optimized image
  63. return new_image;
  64. }
  65. }