/*
 * Created on Apr 3, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ps6;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JPanel;

/**
 * @author knorton
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class VisualGraph extends JPanel implements Runnable {
	private boolean 		active = true;
	private ArrayList	probes = null;
	private Graph		graph = null;
	
	public VisualGraph(int width,int height) {
		this.probes = new ArrayList();
		this.graph = new Graph();
		this.setPreferredSize(new Dimension(width,height));
		this.setBackground(Color.WHITE);
	}
	
	public Graph getGraph() {
		return this.graph;
	}
	
	public void run() {
		try {
			while (this.active) {
				this.graph.step();
				this.repaint();
				Thread.sleep(100);
			}
		}
		catch (InterruptedException e) {
		}
	}
	
	public void stop() {
		this.active = false;
	}
	
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		Graphics2D gg = (Graphics2D)g;
		for (Iterator i=this.probes.iterator();i.hasNext();)
			((VisualProbe)i.next()).draw(gg);
	}
	
	private VisualProbe addProbe(VisualProbe p) {
		this.probes.add(p);
		return p;
	}
	
	public VisualProbe createProbe(Gate input, int x, int y, int width, int height) {
		return (VisualProbe)this.addProbe(new VisualProbe(input,x,y,width,height));
	}
}
