Gerard-Meier

Java Enum TileType

Mar 18th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. package Tile;
  2.  
  3. import java.awt.Color;
  4.  
  5. /**
  6.  *
  7.  * @author Gerjo Meier
  8.  * This is actually an test effort to experiment with "settings" stored in
  9.  * an enum, rather than the tile itself.
  10.  *
  11.  */
  12.  
  13. public enum TileState {
  14.     ERROR (Color.cyan,          '@'),
  15.     TESTING (Color.pink,        'T'), // walkable
  16.     WALKABLE (Color.magenta,    ' '), // walkable
  17.     NONWALKABLE (Color.black,   '#'),
  18.     START (Color.green,         'S'),
  19.     END (Color.red,             'E'),
  20.     PATH (Color.blue,           '+');
  21.  
  22.     private final Color backgroundColor;
  23.     private final char asciiRepresentation;
  24.    
  25.     TileState(Color backgroundColor, char asciiRepresentation) {
  26.         this.backgroundColor     = backgroundColor;
  27.         this.asciiRepresentation = asciiRepresentation;
  28.     }
  29.  
  30.     public final Color getBackgroundColor() {
  31.         return backgroundColor;
  32.     }
  33.    
  34.     public final char getAsciiRepresentation() {
  35.         return asciiRepresentation;
  36.     }
  37.  
  38.     public static TileState valueOf(char asciiRepresentation) {
  39.         for(TileState test : values()) {
  40.             if(test.getAsciiRepresentation() == asciiRepresentation) {
  41.                 return test;
  42.             }
  43.         }
  44.         return ERROR;
  45.     }
  46.  
  47.     public static TileState valueOf(Color backgroundColor) {
  48.         for(TileState test : values()) {
  49.             if(test.getBackgroundColor() == backgroundColor) {
  50.                 return test;
  51.             }
  52.         }
  53.         return ERROR;
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment