Images.java 3.3 KB

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