Advertisement
MrPolywhirl

DrawShapes.java

Dec 14th, 2013
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. public class DrawShapes {
  2.    private enum Shape {
  3.       _1("100000001100000001100000001100000001100000001100000001111111111", 9, 7, 'X'),
  4.       _2("1000001010001000101000001000001010001000101000001", 7, 7, 'X'),
  5.       _3("100000001101000101100101001100010001100101001101000101100000001", 9, 7, 'X'),
  6.       _4("1000000000000101000000000010001000000001000001000000100000001000010000000001001111111111111", 13, 7, 'X'),
  7.       _5("0000000000011000000000111100000001111110000011111111000111111111101111111111111111111111111", 13, 7, 'X'),
  8.       _6("100000000001111000000100111110010000111111100000111111111000111111111100111111111111", 12, 7, 'X'),
  9.       _7("1000101010001000101010001", 5, 5, 'x'),
  10.       UNKNOWN("1010101010101010101010101010101010101010101010101", 7, 7, '?');
  11.      
  12.       private String bitSequence;
  13.       private int width, height;
  14.       private char ch;
  15.       private String filler;
  16.      
  17.       Shape(String bitSequence, int width, int height, char ch) {
  18.          this.bitSequence = bitSequence;
  19.          this.width = width;
  20.          this.height = height;
  21.          this.ch = ch;
  22.          
  23.          StringBuilder fill = new StringBuilder();
  24.          while (fill.length() < width)
  25.             fill.append(' ');
  26.          this.filler =  fill.toString();
  27.       }
  28.      
  29.       public static Shape getEnum(String value) {
  30.          for (Shape shape : values()) {
  31.             if (("_" + value).equals(shape.name())) {
  32.                return shape;
  33.             }
  34.          }
  35.          
  36.          return UNKNOWN;
  37.       }
  38.    }
  39.    
  40.    public DrawShapes() {
  41.       printShapes("12345678", "  ");
  42.    }
  43.    
  44.    public void printShapes(String shapeCodes, String spacing) {
  45.       Shape shape;
  46.       int rowIndex, colIndex;
  47.       int maxHeight = 0;
  48.      
  49.       // Find max height for all shapes.
  50.       for (Character c : shapeCodes.toCharArray()) {
  51.          shape = Shape.getEnum(c.toString());
  52.          if (shape.height > maxHeight)
  53.             maxHeight = shape.height;
  54.       }
  55.      
  56.       // Print out every shape on a new line using column-major order.
  57.       for (int row = 0; row < maxHeight; row++) {
  58.          for (Character ph : shapeCodes.toCharArray()) {
  59.             shape = Shape.getEnum(ph.toString());
  60.             rowIndex = row - (maxHeight - shape.height);
  61.             if (rowIndex < 0) {
  62.                System.out.print(shape.filler);
  63.             } else {
  64.                colIndex = rowIndex * shape.width;
  65.                String seq = shape.bitSequence.substring(
  66.                      colIndex, colIndex + shape.width);
  67.                for (Character ch : seq.toCharArray()) {
  68.                   System.out.printf("%c", ch == 48 ? 32 : shape.ch);
  69.                }
  70.             }
  71.             System.out.print(spacing);
  72.          }
  73.          System.out.println();
  74.       }
  75.    }
  76.    
  77.    public static void main(String[] args) {
  78.       new DrawShapes();
  79.    }
  80. }
  81.  
  82. // X       X  X     X  X       X  X                         XX  X          X         ? ? ? ?  
  83. // X       X   X   X   X X   X X  X X                     XXXX  XXX      X            ? ? ?  
  84. // X       X    X X    X  X X  X  X   X                 XXXXXX  XXXXX  X      x   x  ? ? ? ?  
  85. // X       X     X     X   X   X  X     X             XXXXXXXX  XXXXXXX        x x    ? ? ?  
  86. // X       X    X X    X  X X  X  X       X         XXXXXXXXXX  XXXXXXXXX       x    ? ? ? ?  
  87. // X       X   X   X   X X   X X  X         X     XXXXXXXXXXXX  XXXXXXXXXX     x x    ? ? ?  
  88. // XXXXXXXXX  X     X  X       X  XXXXXXXXXXXXX  XXXXXXXXXXXXX  XXXXXXXXXXXX  x   x  ? ? ? ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement