// Read a file containing rectangle paint commands // and draw them in a Drawing Panel // See RectanglePainter.java for final solution // Step 6: Convert color string to a Color.COLOR import java.util.*; import java.io.*; import java.awt.*; public class Paint6 { public static void main(String[] args) throws FileNotFoundException { String fileName = getFileName(); processFile(fileName); } public static String getFileName() { Scanner console = new Scanner(System.in); System.out.print("File name: "); return console.next(); } public static void processFile(String fileName) throws FileNotFoundException { Scanner fileScan = new Scanner(new File(fileName)); String line = fileScan.nextLine(); Scanner lineScan = new Scanner(line); int width = lineScan.nextInt(); int height = lineScan.nextInt(); System.out.println("width: " + width + ", " + height); DrawingPanel p = new DrawingPanel(width, height); Graphics g = p.getGraphics(); while (fileScan.hasNextLine()) { line = fileScan.nextLine(); processLine(line, g); } } public static void processLine(String line, Graphics g) { System.out.println(line); Scanner lineScan = new Scanner(line); String color = lineScan.next(); color = color.toUpperCase(); if (color.equals("BLACK")) { g.setColor(Color.BLACK); } else if (color.equals("RED")) { g.setColor(Color.RED); } else if (color.equals("GREEN")) { g.setColor(Color.GREEN); } else if (color.equals("BLUE")) { g.setColor(Color.BLUE); } else if (color.equals("YELLOW")) { g.setColor(Color.YELLOW); } else { // make white be the default color g.setColor(Color.WHITE); } int x = lineScan.nextInt(); int y = lineScan.nextInt(); int w = lineScan.nextInt(); int h = lineScan.nextInt(); g.fillRect(x, y, w, h); } }