Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class DrawShapes {
- private enum Shape {
- _1("100000001100000001100000001100000001100000001100000001111111111", 9, 7, 'X'),
- _2("1000001010001000101000001000001010001000101000001", 7, 7, 'X'),
- _3("100000001101000101100101001100010001100101001101000101100000001", 9, 7, 'X'),
- _4("1000000000000101000000000010001000000001000001000000100000001000010000000001001111111111111", 13, 7, 'X'),
- _5("0000000000011000000000111100000001111110000011111111000111111111101111111111111111111111111", 13, 7, 'X'),
- _6("100000000001111000000100111110010000111111100000111111111000111111111100111111111111", 12, 7, 'X'),
- _7("1000101010001000101010001", 5, 5, 'x'),
- UNKNOWN("1010101010101010101010101010101010101010101010101", 7, 7, '?');
- private String bitSequence;
- private int width, height;
- private char ch;
- private String filler;
- Shape(String bitSequence, int width, int height, char ch) {
- this.bitSequence = bitSequence;
- this.width = width;
- this.height = height;
- this.ch = ch;
- StringBuilder fill = new StringBuilder();
- while (fill.length() < width)
- fill.append(' ');
- this.filler = fill.toString();
- }
- public static Shape getEnum(String value) {
- for (Shape shape : values()) {
- if (("_" + value).equals(shape.name())) {
- return shape;
- }
- }
- return UNKNOWN;
- }
- }
- public DrawShapes() {
- printShapes("12345678", " ");
- }
- public void printShapes(String shapeCodes, String spacing) {
- Shape shape;
- int rowIndex, colIndex;
- int maxHeight = 0;
- // Find max height for all shapes.
- for (Character c : shapeCodes.toCharArray()) {
- shape = Shape.getEnum(c.toString());
- if (shape.height > maxHeight)
- maxHeight = shape.height;
- }
- // Print out every shape on a new line using column-major order.
- for (int row = 0; row < maxHeight; row++) {
- for (Character ph : shapeCodes.toCharArray()) {
- shape = Shape.getEnum(ph.toString());
- rowIndex = row - (maxHeight - shape.height);
- if (rowIndex < 0) {
- System.out.print(shape.filler);
- } else {
- colIndex = rowIndex * shape.width;
- String seq = shape.bitSequence.substring(
- colIndex, colIndex + shape.width);
- for (Character ch : seq.toCharArray()) {
- System.out.printf("%c", ch == 48 ? 32 : shape.ch);
- }
- }
- System.out.print(spacing);
- }
- System.out.println();
- }
- }
- public static void main(String[] args) {
- new DrawShapes();
- }
- }
- // X X X X X X X XX X X ? ? ? ?
- // X X X X X X X X X X XXXX XXX X ? ? ?
- // X X X X X X X X X X XXXXXX XXXXX X x x ? ? ? ?
- // X X X X X X X X XXXXXXXX XXXXXXX x x ? ? ?
- // X X X X X X X X X X XXXXXXXXXX XXXXXXXXX x ? ? ? ?
- // X X X X X X X X X X XXXXXXXXXXXX XXXXXXXXXX x x ? ? ?
- // XXXXXXXXX X X X X XXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXX x x ? ? ? ?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement