/** * @(#)Castle.java * * Castle application * * @author * @version 1.00 2011/9/9 */ import apcslib.*; import java.awt.*; import java.awt.Color; import java.util.*; import java.io.*; public class Castle { //======= Main Method ============================= public static void main(String[] args) { //======= Variable Initialization ================= DrawingTool pencil; SketchPad paper = new SketchPad(500, 500); pencil = new DrawingTool(paper); Scanner input = new Scanner(System.in); //================================================= System.out.println("enter file path:"); // gets file path from the user String path = input.nextLine(); int[][] blocks = readFile(path); // reads file data into blocks array drawCastle(paper, blocks); // displays information held in file } //================================================= //======= Brick Drawing Method ==================== // REQUIRES: java.awt.Color, SketchPad, DrawingTool // AUTHOR: Nicky DiCarlo public static SketchPad drawBrick(SketchPad p, double x, double y, double width, double height){ DrawingTool g = new DrawingTool(p); Color gray = new Color(100,100,100); g.up(); g.move(x + (width / 2), y - (height / 2)); // moves to center of the rectangle g.down(); g.drawRect(width, height); g.setColor(gray); g.move(x + (width / 2), y - (height / 2) - 1); // moves to adjusted center and fills in brick g.fillRect(width - 1, height - 1); g.setColor(Color.black); return p; } //================================================= //======= Door Drawing Method ===================== // REQUIRES: java.awt.Color, SketchPad, DrawingTool // AUTHOR: Nicky DiCarlo public static SketchPad drawDoor(SketchPad p, double x, double y, double width, double height){ DrawingTool g = new DrawingTool(p); Color brown = new Color(139,69,19); g.up(); g.move(x + (width / 2), y - (height / 2)); // moves to center of the door g.down(); g.drawRect(width, height); // draws frame g.setColor(brown); g.move(x + (width / 2), y - (height / 2) - 1); // fills in door with brown g.fillRect(width - 1, height - 1); g.move(x + (width * .75), y - height / 2); g.setColor(Color.yellow); g.fillOval((width / 6), (width / 6)); // creates doorknob g.setColor(Color.black); return p; } //================================================= public static SketchPad drawWindow(SketchPad p, double x, double y, double width, double height){ DrawingTool g = new DrawingTool(p); Color brown = new Color(139, 69, 19); g.up(); g.move(x + width / 2, y - height / 2); g.down(); g.drawRect(width / 6, height); // moves to center of pane and draws framess g.drawRect(width, width / 6); g.setColor(brown); g.fillRect((width) - 1, (height) / 6 - 1); // adjusts center and fills in frames g.move(x + (width / 2) + 1, y - (height / 2) - 1); g.fillRect((width) / 6 - 1, height - 1); g.setColor(Color.black); return p; } public static int[][] readFile(String filename){ int[][] blocks = new int[1028][3]; try { BufferedReader in = new BufferedReader(new FileReader(filename)); String str; int count = 0; int written = 0; String number = ""; while ((str = in.readLine()) != null) { if (written > 0){ count ++; } written = 0; char[] temp = str.toCharArray(); // creates an array to hold data from current line int i = 0; int ri = 0; boolean reading = false; boolean negative = false; int c = 0; int d = 0; while (i < temp.length){ if (temp[i] == '['){ reading = true; i ++; } if (temp[i] == ']'){ reading = false; int tempint = Integer.parseInt(number); blocks[count][c] = tempint; c ++; System.out.println(number); number = ""; written ++;; } if (reading){ number += temp[i]; } i ++; } blocks[0][0] = count; } in.close(); } catch (IOException e) { } return blocks; } public static void drawCastle(SketchPad p, int[][] blocks){ int i = 1; while (i <= blocks[0][0]){ if (blocks[i][0] == 1){ drawBrick(p, blocks[i][1], blocks[i][2], 20, 10); } if (blocks[i][0] == 2){ drawBrick(p, blocks[i][1], blocks[i][2], 10, 10); } if (blocks[i][0] == 3){ drawWindow(p, blocks[i][1], blocks[i][2], 30, 30); } if (blocks[i][0] == 4){ drawDoor(p, blocks[i][1], blocks[i][2], 40, 60); } i++; System.out.println(blocks[i][0] + " " + blocks[i][1] + " " + blocks[i][2]); } } }