Images.java 3.7 KB

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