001/* 002 * Image.java 003 */ 004package minesweeper; 005 006import java.awt.*; 007import java.awt.image.*; 008import java.io.*; 009import javax.imageio.*; 010import org.apache.logging.log4j.*; 011 012/** 013 * {@link Images} is an <code>enum</code> that encodes 12 {@link Minesweeper} 014 * images. 015 * 016 * @author 2017-2018 APCS 017 * @author <a href="https://github.com/wps-dpetty">David C. Petty</a> 018 */ 019public enum Images { 020 021 /** The enum associated with a covered square. */ 022 COVER("/images/minesweeper-cover.svg-600x600.png"), 023 /** The enum associated with a blank square. */ 024 BLANK("/images/minesweeper-blank.svg-600x600.png"), 025 /** The enum associated with a flagged square. */ 026 FLAG("/images/minesweeper-flag.svg-600x600.png"), 027 /** The enum associated with a mined square. */ 028 MINE("/images/minesweeper-mine.svg-600x600.png"), 029 /** The enum associated with a square with 1 mine touching it. */ 030 NUMBER1("/images/1.svg-600x600.png"), 031 /** The enum associated with a square with 2 mines touching it. */ 032 NUMBER2("/images/2.svg-600x600.png"), 033 /** The enum associated with a square with 3 mines touching it. */ 034 NUMBER3("/images/3.svg-600x600.png"), 035 /** The enum associated with a square with 4 mines touching it. */ 036 NUMBER4("/images/4.svg-600x600.png"), 037 /** The enum associated with a square with 5 mines touching it. */ 038 NUMBER5("/images/5.svg-600x600.png"), 039 /** The enum associated with a square with 6 mines touching it. */ 040 NUMBER6("/images/6.svg-600x600.png"), 041 /** The enum associated with a square with 7 mines touching it. */ 042 NUMBER7("/images/7.svg-600x600.png"), 043 /** The enum associated with a square with 8 mines touching it. */ 044 NUMBER8("/images/8.svg-600x600.png"); 045 046 //////////////////////////////// FIELDS //////////////////////////////// 047 048 /** 049 * The pathname for {@link Image} to be displayed. 050 */ 051 private final String path; 052 /** 053 * The {@link Image} to be displayed. 054 */ 055 private final Image image; 056 057 ///////////////////////////// CONSTRUCTORS ///////////////////////////// 058 059 /** 060 * Construct enum and {@link Image} to be displayed associated with 061 * <code>path</code>. 062 * 063 * @param path pathname for {@link Image} to be displayed 064 * @pre.cond <code>path</code> is not <code>null</code> 065 * @pre.cond <code>path</code> is a valid pathname for an image file 066 */ 067 private Images(String path) { 068 this.path = path; 069 this.image = getImage(path); 070 } 071 072 //////////////////////////////// METHODS /////////////////////////////// 073 074 /** 075 * Return pathname for {@link Image} to be displayed. 076 * 077 * @return pathname for {@link Image} to be displayed 078 */ 079 public String path() { 080 return path; 081 } 082 083 /** 084 * Return {@link Image} to be displayed. 085 * 086 * @return {@link Image} to be displayed 087 */ 088 public Image image() { 089 return image; 090 } 091 092 /** 093 * Return {@link Image} associated with <code>path</code>. 094 * 095 * @param path pathname for {@link Image} 096 * @return {@link Image} associated with <code>path</code> 097 * @pre.cond <code>path</code> is not <code>null</code> 098 * @pre.cond <code>path</code> is a valid pathname for an image file 099 */ 100 public static Image getImage(String path) { 101 // log4j Logger cannot be a field of this enum. 102 Logger logger = LogManager.getLogger(Minesweeper.SHORT); 103 BufferedImage image = null; 104 try { 105 InputStream is = Images.class.getResourceAsStream(path); 106 image = ImageIO.read(is); 107 } catch (IOException e) { 108 logger.error("{}: {}", Images.class, e); 109 } 110 if (image != null) 111 logger.info("{}: {} ({},{})", 112 Images.class, path, image.getWidth(), image.getHeight()); 113 return image; 114 } 115}