Images.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. images.add(toCompatibleImage(ImageIO.read(Main.class.getResource("/image/youwon.png"))));
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. public static BufferedImage getImage(ImageType img) {
  36. return images.get(img.ordinal());
  37. }
  38. public enum ImageType {
  39. pulse,cursor,pressstart,colorstrike,background,aanwijzers,kast,gameover, screenshot, help,youwon
  40. }
  41. public static BufferedImage readImage(File f) {
  42. BufferedImage bf = null;
  43. try {
  44. bf = ImageIO.read(f);
  45. } catch (IOException e) {
  46. e.printStackTrace();
  47. }
  48. return bf;
  49. }
  50. public static BufferedImage toCompatibleImage(BufferedImage image)
  51. {
  52. // obtain the current system graphical settings
  53. GraphicsConfiguration gfx_config = GraphicsEnvironment.
  54. getLocalGraphicsEnvironment().getDefaultScreenDevice().
  55. getDefaultConfiguration();
  56. /*
  57. * if image is already compatible and optimized for current system
  58. * settings, simply return it
  59. */
  60. if (image.getColorModel().equals(gfx_config.getColorModel()))
  61. return image;
  62. // image is not optimized, so create a new image that is
  63. BufferedImage new_image = gfx_config.createCompatibleImage(
  64. image.getWidth(), image.getHeight(), image.getTransparency());
  65. // get the graphics context of the new image to draw the old image on
  66. Graphics2D g2d = (Graphics2D) new_image.getGraphics();
  67. // actually draw the image and dispose of context no longer needed
  68. g2d.drawImage(image, 0, 0, null);
  69. g2d.dispose();
  70. // return the new optimized image
  71. new_image.setAccelerationPriority(1);
  72. new_image.flush();
  73. return new_image;
  74. }
  75. public static BufferedImage initBufferedImage(int width, int height){
  76. GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
  77. GraphicsDevice device = env.getDefaultScreenDevice();
  78. GraphicsConfiguration config = device.getDefaultConfiguration();
  79. return config.createCompatibleImage(width, height, Transparency.TRANSLUCENT);
  80. }
  81. public static VolatileImage initVolatileImage(int width, int height, int opc){
  82. GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  83. GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
  84. VolatileImage image = null;
  85. image = gc.createCompatibleVolatileImage(width, height, opc);
  86. int valid = image.validate(gc);
  87. if (valid == VolatileImage.IMAGE_INCOMPATIBLE)
  88. image = initVolatileImage(width, height, opc);
  89. return image;
  90. }
  91. }