/** File: Symbol.java * This class contains the values for the enumerated type Symbol. Each symbol * has a payout value and image associated with it. * Class Invariant: All symbols must have a positive, even payout value * @version: 2/29/12 * @author: Ryan Bergeron */ package proj2; import java.awt.image.*; import java.io.IOException; import javax.imageio.ImageIO; public enum Symbol { SEVEN(12,"images/seven.jpg"), WATERMELON(10,"images/watermelon.jpg"), ORANGE(8,"images/orange.jpg"), PLUM(6,"images/plum.jpg"), LEMON(4,"images/lemon.jpg"), CHERRY(2,"images/cherry.jpg"); private int payout; // Symbol payout value private BufferedImage image; // Symbol image private String icon; // Symbol file name /** Constructor * Preconditions: Payout must be positive and even * File name must not be null * Postcondition: A symbol is constructed and initialized * @param payout - Symbol payout amount * @param icon - Symbol image file name */ Symbol(int payout, String icon){ if ((payout < 0) || (payout%2 != 0) || icon == null) throw new RuntimeException("Bad Symbols parameters"); this.payout = payout; this.icon = icon; loadImage(icon); } /** Copy Constructor * Preconditions: Symbol must not be null * Postcondition: Symbol is copied * @param s - A single symbol */ Symbol(Symbol s){ if (s == null) throw new RuntimeException("Symbol is null"); payout = s.payout; icon = s.icon; loadImage(icon); } /** Accessor for symbol payout amount * Preconditions: None * Postcondition: Symbol's payout is returned * @param - None * @return - Symbol's payout amount */ public int getPayout(){ return payout; } /** Accessor for symbol image * Precondition: None * Postcondition: Symbol image is returned * @param - None * @return - Returns symbol image */ public BufferedImage getImage(){ return image; } /** Compares two symbols for equality * Precondition: Symbol for comparison must not be null * Postcondition: Returns whether or not symbols are equal * @param s - A single symbol * @return - true if symbols are equal, else false */ public boolean equals(Symbol s){ if (s == null) throw new RuntimeException("Symbol is null"); if(icon.equals(s.icon)){ return true; } return false; } /** Loads a symbol's image * Precondition: Image file name is not null * Postcondition: Symbol image is loaded * @param icon - Symbol image file name */ private void loadImage(String icon){ if (icon == null) throw new RuntimeException("Symbol file name is null"); image = null; try{ image = ImageIO.read(getClass().getResource(icon)); }catch(IOException e){ System.out.println("Error: Could not load image: "+ icon); } } /** Generates symbol's state string * Preconditions: None * Postcondition: Symbols state string is returned * @param - None * @return - String containing icon file name */ public String toString(){ return icon; } }