// Read a file containing rectangle paint commands // and draw them in a Drawing Panel // See RectanglePainter.java for final solution // Step 5: Use a Scanner to parse a line with the // rectangle description and draw (using RED) import java.util.*; import java.io.*; import java.awt.*; public class Paint5 { 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(); g.setColor(Color.RED); int x = lineScan.nextInt(); int y = lineScan.nextInt(); int w = lineScan.nextInt(); int h = lineScan.nextInt(); g.fillRect(x, y, w, h); } }