import java.applet.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import Vertex2D;
import Triangle;
import ppmDecoder;

public class myTest extends Applet {
    static int CHUNKSIZE = 100;
    Raster reference, raster;
    Drawable triList[];
    int triangles;
    Color background;
    Image screen;
    int state;

    public void init( ) {
        // Read in the triangle file
        triList = new Triangle[CHUNKSIZE];
        triangles = 0;
        InputStream is = null;
        try {
            is = new URL(getDocumentBase(), getParameter("datafile")).openStream();
            ReadInput(is);
            is.close();
        } catch (IOException e) {
            showStatus("Error reading "+getParameter("datafile"));
        }

        // Create a raster for rendering
        raster = new Raster(400, 400);
	raster.fill(Color.white);

	for(int i = 0; i<triangles; ++i)
	  triList[i].Draw(raster);
	screen = raster.toImage();
    }

    // parse a floting point number
    private float getNumber(StreamTokenizer st) throws IOException {
        st.nextToken();
        return (Float.valueOf(st.sval).floatValue());
    }

    // parse a packed integer in hex, octal, or decimal
    private int getColor(StreamTokenizer st) throws IOException {
        st.nextToken();
        int pix;
        if (st.sval.startsWith("0x") || st.sval.startsWith("0X")) {
	        pix = (int) Long.parseLong(st.sval.substring(2), 16);
	    } else
	    if (st.sval.startsWith("0") || st.sval.length() > 1) {
	        pix = (int) Long.parseLong(st.sval.substring(1), 8);
	    } else
	        pix = Integer.parseInt(st.sval);
        return pix;
    }

    // extend the triangle array if needed
    private void growList() {
        Drawable newList[] = new Drawable[triList.length+CHUNKSIZE];
        System.arraycopy(triList, 0, newList, 0, triList.length);
        triList = newList;
        CHUNKSIZE = (3*CHUNKSIZE)/2;
    }
    
    // define legal characters for our parser
    // The remainder of a line after a '#' is ignored
    // you might find this helpful for debugging
    private void configureParser(StreamTokenizer st) {
	    st.ordinaryChars(0, 255);
	    st.wordChars('a', 'f');
	    st.wordChars('A', 'F');
	    st.wordChars('0', '9');
	    st.wordChars('-', '-');
	    st.wordChars('.', '.');
	    st.wordChars('x', 'x');
	    st.wordChars('X', 'X');
	    st.whitespaceChars(' ', ' ');
	    st.whitespaceChars('\t', '\t');
	    st.whitespaceChars('\n', '\n');
	    st.whitespaceChars('\r', '\r');
    	st.eolIsSignificant(false);
    	st.commentChar('#');
    }

    // Scan the triangle file
    public void ReadInput(InputStream is) throws IOException {
        Vertex2D v1, v2, v3;
        int currentvert = 0;
        float x, y;
        int pix;

	    StreamTokenizer st = new StreamTokenizer(is);
	    configureParser(st);
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            st.pushBack();
            x = getNumber(st);
            y = getNumber(st);
            pix = getColor(st);
            v1 = new Vertex2D(x, y, pix);
            x = getNumber(st);
            y = getNumber(st);
            pix = getColor(st);
            v2 = new Vertex2D(x, y, pix);
            x = getNumber(st);
            y = getNumber(st);
            pix = getColor(st);
            v3 = new Vertex2D(x, y, pix);
            if (triangles == triList.length) growList();
            triList[triangles++] = new Triangle(v1, v2, v3);
	    }
	}
	
	
  //	String message[] = { "Reference", "Rendered", "Difference" };

    public void paint(Graphics g) {
      g.drawImage(screen, 0, 0, this);
    }

    public void update(Graphics g) {
        paint(g);
    }
}
