/* * Umass Amherst * CS377 Operating System * Labx: xxxx * Author: AHF * Date: 9/10/2 * * Compile : javac -d . TestParse.java * * The commend line interface is : * java TestParse * */ import javax.swing.*; import java.awt.event.*; import java.io.*; import java.awt.*; import java.lang.*; // class ShowCanvas extends Canvas // // class ShowCanvas extends Canvas { int r; // public ShowCanvas(String fname)throws IOException // // - Create the ShowCanvas // - Acquire the sphere radius from the configuration file // public ShowCanvas(String fname)throws IOException{ String curLine,preLine; try { // Open the file BufferedReader input=new BufferedReader(new FileReader(fname)); // Read the first line curLine=input.readLine(); // Check contents of first line if(curLine.indexOf("radius=")==-1) { System.out.println("Parse error: missing radius="); System.exit(1); }; // Figure out where in the string to grab the integer int start=curLine.indexOf("radius="); String sub = curLine.substring(start+7, curLine.length()); // Extract the integer try { r = Integer.parseInt(sub); } catch(NumberFormatException e) { // Catch parse errors System.out.println("Parse error: missing integer"); System.exit(1); }; }catch (FileNotFoundException e) { // Deal with file error System.out.println("File not found: " + fname); System.exit(1); } } // Draw the canvas, which consists of a sphere public void paint(Graphics g) { g.setColor(Color.red); g.fillOval(250-r/2, 250-r/2, r, r); } } // public class TestParse extends JFrame // // public class TestParse extends JFrame { // public TestParse(String fname) throws IOException // // Create the top-level object // public TestParse(String fname) throws IOException { // Respond to the window close event by dying addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.out.println("Exit when Closed event"); System.exit(0); } } ); // Set window title setTitle("Test Parse "); // Do not allow window size changes setResizable(false); // Force window size setSize(500, 500); // Do the work of getting the configuration file and creating the // window. try{ ShowCanvas can=new ShowCanvas(fname); // Attach the canvas to the window getContentPane().add(can, BorderLayout.CENTER); } catch(IOException e){ System.out.println("I/O error "); } } // public static void main(String[] args) // // Program start // public static void main(String[] args) { // Check the number of arguments if (args.length!=1) { System.err.println("usage: java TestParse "); return; } try{ // Create the primary object instance TestParse tp = new TestParse(args[0]); // Draw the window tp.show(); } catch (IOException e){ System.out.println("I/O error "); } } }