Images.java 3.5 KB

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