/* a simple program to create and display a series of lines read from a file.
   Must be run as an application.  Data file format is
   num_vertices num_edges
   coord0.x coord0.y
   coord1.x coord1.y
   ....
   edge0.start edge0.end
   edge1.start edge1.end
   ....
   Everything is in integers.  File reading assumes a valid data file (i.e.,
   I didn't check for errors!)  Filename is passed as first argument to
   application - written by Matt Ward */

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class FileTest extends Applet {

    static final int width = 400; // constant dimensions for frame
    static final int height = 400;
    private Color blue;		// drawing color
    private int pt_cnt, vec_cnt;	// number of vertices and edges
    private int[][] points, edges;	// arrays to hold vertices and edges
    private DataInputStream inStream;	// stream from file

// just create a color to draw in
    public void init()  {
        blue = Color.blue;
        }  //end init()

// draw the lines, using indices into vertex list
    public void paint(Graphics g) {
        int x1, x2, y1, y2;
        g.setColor(blue);
        for(int i = 0;i < vec_cnt;i++)  {
	    x1 = points[edges[i][0]][0];
	    y1 = points[edges[i][0]][1];
	    x2 = points[edges[i][1]][0];
	    y2 = points[edges[i][1]][1];
            g.drawLine(x1, y1, x2, y2);
	    }
        }  // end paint()

//  the entry point for the application - just create a frame and insert applet
    public static void main(String args[]) {
	FileFrame f = new FileFrame("FileTest", args[0]);
        f.setSize(width, height);
	f.show();
        }  // end main()

//  read in the data file, converting tokens to integers
    public void ReadFile(String fileName)  {
//  check to insure filename is valid, then start tokenizing
        try  {
	    InputStreamReader is = 
	        new InputStreamReader(new FileInputStream(fileName));
	    Reader r = new BufferedReader(is);
	    StreamTokenizer tok = new StreamTokenizer(r);
//  get number of vertices and edges, and set array sizes accordingly
	    tok.nextToken();
	    pt_cnt = (int)tok.nval;	// assume token type is correct
	    tok.nextToken();
	    vec_cnt = (int)tok.nval;
	    points = new int[pt_cnt][2];
	    edges = new int[vec_cnt][2];
//  read in vertices
	    for(int i = 0; i < pt_cnt;i++)	{
	        tok.nextToken();
	        points[i][0] = (int)tok.nval;
	        tok.nextToken();
	        points[i][1] = (int)tok.nval;
	        }
//  read in edges (which are start and stop indices into the vertex list)
	    for(int i = 0; i < vec_cnt;i++)	{
	        tok.nextToken();
	        edges[i][0] = (int)tok.nval;
	        tok.nextToken();
	        edges[i][1] = (int)tok.nval;
	        }
	    }

//  if problem with opening file for stream input, terminate
        catch (Exception e)  {
	    System.err.println("Error in ReadFile: "+ e.toString());
	    System.exit(1);
	    }
	}  // end ReadFile()

    }  // end FileTest class

// extend Frame class to allow graceful window closing (copied from 
// Graphic Java book)
class FileFrame extends Frame {
    public FileFrame(String frameTitle, String fileName)  {
        super(frameTitle);
        FileTest app = new FileTest();
        app.init();
	app.ReadFile(fileName);
        app.start();
        add(app, "Center");
        addWindowListener(new WindowAdapter()   {
           public void windowClosing(WindowEvent event) {
              dispose();
              System.exit(0);
              }
              });
        }
    }  // end FileFrame class

