Images.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package image;
  2. import java.awt.Graphics2D;
  3. import java.awt.GraphicsConfiguration;
  4. import java.awt.GraphicsDevice;
  5. import java.awt.GraphicsEnvironment;
  6. import java.awt.Transparency;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.VolatileImage;
  9. import java.io.File;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import javax.imageio.ImageIO;
  13. import main.Main;
  14. public class Images {
  15. public static ArrayList<BufferedImage> images = new ArrayList<BufferedImage>();
  16. public Images() {
  17. }
  18. static {
  19. try {
  20. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/pulse.png"))));
  21. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/cursor.png"))));
  22. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/pressstart.png"))));
  23. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/colorstrike.png"))));
  24. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/background.png"))));
  25. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/aanwijzers4sho.png"))));
  26. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/kast.png"))));
  27. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/gameover.png"))));
  28. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/screenshot.png"))));
  29. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/help.png"))));
  30. } catch (IOException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. public static BufferedImage getImage(ImageType img) {
  35. return images.get(img.ordinal());
  36. }
  37. public enum ImageType {
  38. pulse,cursor,pressstart,colorstrike,background,aanwijzers,kast,gameover, screenshot, help
  39. }
  40. public static BufferedImage readImage(File f) {
  41. BufferedImage bf = null;
  42. try {
  43. bf = ImageIO.read(f);
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. return bf;
  48. }
  49. public static BufferedImage toCompatibleImage(BufferedImage image)
  50. {
  51. // obtain the current system graphical settings
  52. GraphicsConfiguration gfx_config = GraphicsEnvironment.
  53. getLocalGraphicsEnvironment().getDefaultScreenDevice().
  54. getDefaultConfiguration();
  55. /*
  56. * if image is already compatible and optimized for current system
  57. * settings, simply return it
  58. */
  59. if (image.getColorModel().equals(gfx_config.getColorModel()))
  60. return image;
  61. // image is not optimized, so create a new image that is
  62. BufferedImage new_image = gfx_config.createCompatibleImage(
  63. image.getWidth(), image.getHeight(), image.getTransparency());
  64. // get the graphics context of the new image to draw the old image on
  65. Graphics2D g2d = (Graphics2D) new_image.getGraphics();
  66. // actually draw the image and dispose of context no longer needed
  67. g2d.drawImage(image, 0, 0, null);
  68. g2d.dispose();
  69. // return the new optimized image
  70. new_image.setAccelerationPriority(1);
  71. new_image.flush();
  72. return new_image;
  73. }
  74. public static BufferedImage initBufferedImage(int width, int height){
  75. GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  76. GraphicsDevice device = env.getDefaultScreenDevice();
  77. GraphicsConfiguration config = device.getDefaultConfiguration();
  78. return config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
  79. }
  80. public static VolatileImage initVolatileImage(int width, int height, int opc){
  81. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  82. GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
  83. VolatileImage image = null;
  84. image = gc.createCompatibleVolatileImage(width, height, opc);
  85. int valid = image.validate(gc);
  86. if (valid == VolatileImage.IMAGE_INCOMPATIBLE)
  87. image = initVolatileImage(width, height, opc);
  88. return image;
  89. }
  90. }