package plot;

import java.awt.*;
import java.io.*;

class FileHandler implements OutputReader {
	String fn;
	FileOutputStream f;
	PrintStream out;
	
	public FileHandler(String fn) {
		this.fn = fn;
	}
	
	public void read(String s) {
		try {
			f = new FileOutputStream(fn);
			out = new PrintStream(f);
		} catch (FileNotFoundException e) {
			System.out.println(e);
			System.exit(1);		
		} catch (IOException e) {
			System.out.println(e);
			System.exit(1);		
		}
		System.out.println("Writing to " + fn);
		out.print(s);
		out.close();
	}
}

class StreamHandler implements OutputReader {
	PrintStream out;
	public StreamHandler(PrintStream out) {
		this.out = out;
	}
	
	public void read(String s) {
		out.print(s);
	}
}



public class DrawTool {
	
	public static void main(String arg[]) {
		OutputReader out = null;
		if (arg.length > 0) {
			try {
				out = getOutput(arg);
			} catch (IOException e) { 
				System.out.println("" + e);
			}
		} 
		if (out == null) {
			System.out.println("Setting output to standard output");
			out = new StreamHandler(System.out);
		}
		int w, h;
		w = 300; h = 300;
		DrawPanel dp = new DrawPanel(w, h, out);
		Frame f = new StandardFrame("Drawing tool");
		f.add(dp);
		f.pack();
		f.show();
	}

	static OutputReader getOutput(String arg[]) throws IOException {
		if (arg.length == 1) {
				FileHandler f = new FileHandler(arg[0]);
				return(f);
		} else return(null);
	}
	
}
