package Targa;
/**
* TargaView is a simple GUI for displaying an instance of TargaPicture. Displays
* the image in a frame, and also draws boundary or lines associated with the
* TargaPicture instance.
*/

import java.io.* ;
import java.awt.* ;
import java.awt.image.*;
import java.awt.event.*;
import java.util.Vector ;
/**
* TargaCanvas is the actual display that is added to the TargaView frame. Should
* not really need to instantiate this class ever...
*/

class TargaCanvas extends Canvas {
	TargaPicture t;
	Image i;
	Vector v ;
	
	
	TargaCanvas (String file, boolean isGreyScale) throws IOException {
		v = new Vector() ;
		t = new TargaPicture(file) ;
		//if (isGreyScale) t.readGrey() ; else 
		t.read() ;
		setSize(t.getWidth(), t.getHeight()) ;
		setBackground(Color.black) ;
		setForeground(Color.white) ;
		i= createImage(new MemoryImageSource(t.getWidth(),t.getHeight(),t.getImageData(),0,t.getWidth()));
	}


	TargaCanvas(TargaPicture t) {
		this.t = t;
		setSize(t.getWidth(), t.getHeight()) ;
		setBackground(Color.black) ;
		setForeground(Color.white) ;
		i = createImage(new MemoryImageSource(t.getWidth(),t.getHeight(),t.getImageData(),0,t.getWidth()));
	}


	public void paint(Graphics g) {
		int w2,h2;
		int x1,y1,x2,y2;
		Vector boundary = t.getBoundaryVector() ;
		Vector lines = t.getLinesVector();
    Vector colours = t.getColoursVector();

		w2 = t.getWidth() ;
		h2 = t.getHeight() ;
		g.drawImage(i,0,0,w2,h2,this) ;



		if (boundary != null) {
		g.setColor(Color.yellow);
    	for (int i = 0; i < boundary.size(); i++) {
				if (boundary.elementAt(i) != null) {
					x1 = ((Point)boundary.elementAt(i)).x;
					y1 = ((Point)boundary.elementAt(i)).y;

					g.fillRect(x1,y1,2,2) ;
				}
			}
		}

		if (lines != null) {
      g.setColor(Color.blue);
			for (int i = 0; i < lines.size()-1; i+=2) {
        if (colours != null) {
          g.setColor((Color)colours.elementAt(i/2));
        }
				if(lines.elementAt(i) != null) {
					x1 = ((Point)lines.elementAt(i)).x ;
					y1 = ((Point)lines.elementAt(i)).y ;
					x2 = ((Point)lines.elementAt(i+1)).x ;
					y2 = ((Point)lines.elementAt(i+1)).y ;
					g.drawLine(x1,y1,x2,y2);
				}
			}
		}
	}
}

/**
* TargaView is the display frame. Normal method of using it is to create a targapicture tp,
* and new TargaView(tp). This will open a frame and display the targapicture
*/

public class TargaView extends Frame implements WindowListener {
	TargaPicture t ;
	ScrollPane sp ;
	Canvas c;

	public TargaView(String file, boolean isGreyScale) {
		try {
		c = new TargaCanvas(file, isGreyScale) ;
		sp = new ScrollPane();
		sp.add(c) ;
		add(sp, BorderLayout.CENTER) ;
		this.setSize(500,500) ;
		this.setVisible(true) ;
		} catch (IOException e) {
System.out.println("Error") ;
		}
		addWindowListener(this) ;
	}
	
	
	public TargaView(TargaPicture t) {
		this.t = t ;
		c = new TargaCanvas(t) ;
		sp = new ScrollPane();
		sp.add(c) ;
		add(sp, BorderLayout.CENTER) ;
		this.setSize(500,500) ;
		this.setVisible(true) ;
		addWindowListener(this) ;

	}

	public void windowActivated(WindowEvent e) {}
	public void windowClosed(WindowEvent e) {}
	public void windowClosing(WindowEvent e) {
System.exit(1);
}
	public void windowDeactivated(WindowEvent e) {}
	public void windowDeiconified(WindowEvent e) {}
	public void windowIconified(WindowEvent e) {}
	public void windowOpened(WindowEvent e) {}

	
	
	public void paint(Graphics g) {
		super.paint(g) ;
	}

	
	public static void main(String [] args) {
		new TargaView(args[0],false) ;
	}
		
}







