Images.java 3.4 KB

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