import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.awt.geom.*;


public class Output {
    FileOutputStream FS;
    String S;


    public Output(String S) {
	this.S=S;
	try {FS=new FileOutputStream(S);}
	catch(IOException e) {}
    }



    public void write(String S) {
	PrintStream PS=new PrintStream(FS);
	PS.print(S);
    }

    public void writeln(String S) {
	PrintStream PS=new PrintStream(FS);
	PS.println(S);
    }


    public void tclBgSet(Color C) {
	PrintStream PS=new PrintStream(FS);
        String S=tclConvert(C);
	PS.print(".plot itemconfigure bg -fill "+S);
    }

    /**outline of a polygon*/

    public void polyOutlineWrite(Path2D.Double X,Color C,double w) {
	System.out.println("used");
	PrintStream PS=new PrintStream(FS);
	AffineTransform A=AffineTransform.getTranslateInstance(0,0);
	PathIterator P=X.getPathIterator(A);
	double[] coords=new double[6];
	double x1=0;
	double y1=0;
	double x2=0;
	double y2=0;
	double x0=0;
	double y0=0;

	int count=0;
	while(P.isDone()==false) {
	    P.currentSegment(coords);
	    x2=coords[0];
	    y2=coords[1];

	    if(count>0) {
		segmentWrite(x1,y1,x2,y2,C,w);
	        x1=x2;
	        y1=y2;
	    }
	    if(count==0) {
		x1=coords[0];
		y1=coords[1];
		x0=coords[0];
		y0=coords[1];
	    }
	    P.next();
	    ++count;
	}
	segmentWrite(x0,y0,x2,y2,C,w);
        PS.println("");
    }


    public void segmentWrite(double x1,double y1,double x2,double y2,Color C,double w) {	
         PrintStream PS=new PrintStream(FS);
 	 String S=tclConvert(C);
	 Double W=Double.valueOf(w);
	 String SS=W.toString();
         Double X1=Double.valueOf(x1);
	 Double Y1=Double.valueOf(y1);
	 Double X2=Double.valueOf(x2);
	 Double Y2=Double.valueOf(y2);

         PS.print(".plot create line ");
	 PS.print(X1.toString()+" ");
	 PS.print(Y1.toString()+" ");
	 PS.print(X2.toString()+" ");
	 PS.print(Y2.toString()+" ");
         PS.print(" -fill "+S);
         PS.print(" -width "+SS);
         PS.print(" -tag X");
	 PS.println("");
    }


    

    public String tclConvert(Color C) {
	int r=C.getRed();
	int g=C.getGreen();
	int b=C.getBlue();
	r=r/16;
	g=g/16;
	b=b/16;
	String[] hex={"a","b","c","d","e","f"};
	Integer R=Integer.valueOf(r);
	Integer G=Integer.valueOf(g);
	Integer B=Integer.valueOf(b);
	String RR=R.toString();
	String GG=G.toString();
	String BB=B.toString();
	if(r>9) RR=hex[r-10];
	if(g>9) GG=hex[g-10];
	if(b>9) BB=hex[b-10];
	String S="#"+RR+GG+BB;
	return(S);
    }



    public void polyWrite(Path2D.Double X,Color C1,Color C2,double w) {
	PrintStream PS=new PrintStream(FS);
	AffineTransform A=AffineTransform.getTranslateInstance(0,0);
	String S1=tclConvert(C1);	
        String S2=tclConvert(C2);
	Double W=Double.valueOf(w);
	String S3=W.toString();
	PathIterator P=X.getPathIterator(A);
	double[] coords=new double[6];
	double x1=0;
	double y1=0;

        PS.print(".plot create polygon ");
	while(P.isDone()==false) {
	    P.currentSegment(coords);
	    x1=coords[0];
	    y1=coords[1];
	    Double X1=Double.valueOf(x1);
	    Double Y1=Double.valueOf(y1);
	    PS.print(X1.toString()+" ");
	    PS.print(Y1.toString()+" ");
            P.next();
	}
        PS.print("-fill "+S1);
        PS.print(" -outline "+S2);
        PS.print(" -width "+S3);
        PS.print(" -tag X");
	PS.println("");
        PS.println("");
    }







}
