import java.awt.*;
import java.util.*;
import java.awt.event.*;
import java.applet.Applet;

public class proj1 extends java.applet.Applet 
{
    DrawPanel panel;
	ShowPanel showp;
    Controls control;
	Label staten;
	Label status;

    public void init() {		
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints c = new GridBagConstraints();
		setFont(new Font("Helvetica", Font.PLAIN, 14));
		setLayout(gridbag);
		c.fill = GridBagConstraints.BOTH;
		c.insets=new Insets (5,5,5,5);
        c.weightx = 1.0;
        c.weighty = 1.0;
 		c.gridheight = 1;
 		c.gridwidth = GridBagConstraints.RELATIVE;	   	 
        panel = new DrawPanel();
		staten=new Label ("E=N/A               ");
		status=new Label ("Draw object in the right panel.         ");
		showp=new ShowPanel(panel,staten,status);
		c.anchor =GridBagConstraints.NORTHWEST;
		gridbag.setConstraints(showp, c);
		add(showp);
        c.weightx = 2.0;
		c.anchor =GridBagConstraints.NORTHEAST;
 		c.gridwidth = GridBagConstraints.REMAINDER ;	   	 
		gridbag.setConstraints(panel, c);
		add(panel);
		control = new Controls(panel,showp,status);
		c.weighty = 0.0;
		c.gridwidth =1;
 		c.fill =  GridBagConstraints.VERTICAL ;	 
        c.weightx = 0.0;
		gridbag.setConstraints(staten, c);
		add(staten);
		gridbag.setConstraints(status, c);
		add(status);
// 		c.gridwidth = GridBagConstraints.REMAINDER ;	   	 
		gridbag.setConstraints(control, c);
		add(control);
    }

    public void destroy() {
        remove(panel);
        remove(control);
    }

    public static void main(String args[]) {
        Frame f = new Frame("proj1");
        proj1 drawTest = new proj1();
        drawTest.init();
        f.add("Center", drawTest);
        f.setSize(700, 300);
        f.show();
    }
    public String getAppletInfo() {
        return "3D reconstruction from line drawings.By Adrian Barbu.";
    }
}

class DrawPanel extends Panel implements MouseListener, MouseMotionListener{
    int midx;
	int speed;
	boolean movemode=false;
	boolean delmode=false;
	Point pi,pf;
    Vector V = new Vector(); //the vertives
    Vector E = new Vector(); //the edges
	
    public DrawPanel() {
        setBackground(Color.white);
        addMouseMotionListener(this);
        addMouseListener(this);
		pi=new Point (-1,-1);
		pf=pi;
    }
	public Point findclose(Point p0){
	//returns a point from the Vertex list if it's
	//close enough to p0, else returns p0
		boolean found=false;
		int np=V.size ();
		int i,j;j=-1;
		for (i=0;i<np;i++){
			Point p= (Point)V.elementAt (i);
			if ((p0.x-p.x)*(p0.x-p.x)+(p0.y-p.y)*(p0.y-p.y)<100){ 
				found=true;
				j=i;
				i=np;
			}
		}
		if (j<0) {return p0;}
		else {return (Point)V.elementAt (j);}
	}
	public boolean colinear (Point a,Point b,Point c){
		double r,rx,ry;
		r=a.y*(c.x-b.x)-a.x*(c.y-b.y)-b.y*c.x+c.y*b.x;
		rx=(b.x-a.x)*(b.x-c.x);	
		ry=(b.y-a.y)*(b.y-c.y);
		if ((r>-400)&&(r<400)&&(rx<10)&&(ry<10)) {return true;}
		else return false; 
	}
	public void deletepoint(Point p){
	//remove all occurences of p
		int i,j,k;
		Point q;
		j=V.indexOf (p);
		if (V.removeElement(p)){
			for (i=0;i<E.size ();){
				q=(Point)E.elementAt (i);
				if ((j==q.x)||(j==q.y)){
					E.removeElementAt(i);
				}
				else i++;
			}
			for(i=0;i<E.size ();i++){
				q=(Point)E.elementAt (i);
				if (q.x>j)	q.x--;
				if (q.y>j)	q.y--;
			}
		}
		else{
			for (i=0;i<E.size ();i++){
				q=(Point)E.elementAt (i);
				if (colinear((Point)V.elementAt (q.x),p,(Point)V.elementAt (q.y))){
					E.removeElementAt(i);
					break;
				}
			}			
		}
	}	
    public void setDrawMode(int mode) {
    }
    public void mousePressed(MouseEvent e) {
        e.consume();
		pi=findclose(e.getPoint ());
		if (delmode) {
			deletepoint(pi);
		}
		else{
			if (movemode){
				midx=V.indexOf(pi);}
			else{
				if (V.indexOf (pi)==-1){
					 V.addElement (pi);
				}
			}
			pf=pi;
		}
		repaint();
    }
    public void mouseDragged(MouseEvent e) {
        e.consume();
		if (!delmode){
			if (movemode) {
				pf=e.getPoint ();
				if (midx>-1) V.setElementAt (pf,midx);
			}
			else{
				pf=findclose(e.getPoint ());
			}
		}
        repaint();	
    }
    public void mouseReleased(MouseEvent e) {
        e.consume();
		if (!delmode){
			if (movemode) {
				pf=e.getPoint ();
				if (midx>-1) V.setElementAt (pf,midx);
			}
			else {
				pf=findclose(e.getPoint ());
				if(pi!=pf){
					if (V.indexOf (pf)==-1) V.addElement (pf);
					E.addElement(new Point(V.indexOf (pi),V.indexOf (pf)));
				}
			}
			pi=pf;
		}
		repaint();		
    }
    public void mouseMoved(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mouseClicked(MouseEvent e) {
    }
    public void paint(Graphics g) {
        int np = V.size();
        int ne = E.size();
        /* draw the current lines and points */
        g.setColor(Color.black);
        g.setPaintMode();
		Point p0,p,q;
        for (int i=0; i < np; i++) {
            p = (Point)V.elementAt(i);
			g.fillRect (p.x-3,p.y-3,6,6);
			p0=p;
        } 
		for (int i=0;i<ne;i++) {
			q=(Point)E.elementAt(i);
			p0=(Point)V.elementAt (q.x);
			p=(Point)V.elementAt(q.y);
		    g.drawLine(p0.x, p0.y, p.x, p.y);
		}
		if ((!movemode)&&(!delmode)){
		    g.drawLine(pi.x, pi.y, pf.x, pf.y);
		}
    }
}
class Point3d extends Point {
	double z;
	Point3d(Point3d p){
		super(p.x,p.y);
		this.z =p.z;
	}
	Point3d(Point p,int z){
		super(p);
		this.z =z;
	}
	Point3d(int x,int y,double z){
		super(x,y);
		this.z =z;
	}
	public void setLocation(int x, int y, double z){
		this.setLocation (x,y);
		this.z=z;
	}
	public void setLocation(Point3d p){
		int x=p.x,y=p.y; double z=p.z;
		this.x=x;
		this.y=y;
		this.z=z;
	}
}class Matrix {	//Matrix for the rotation of the figure	//memorizes all rotations that have been done.
    double xx, xy, xz;
    double yx, yy, yz;
    double zx, zy, zz;
	Matrix(){		xx=yy=zz=1;		xy=xz=yx=yz=zx=zy=0;
	}
	public void reset(){
		xx=yy=zz=1;		xy=xz=yx=yz=zx=zy=0;
	}	public void xrot(double theta) {
		//rotate teta rad arround x axis
		double ct = Math.cos(theta);
		double st = Math.sin(theta);
		double Nyx = yx * ct + zx * st;
		double Nyy = yy * ct + zy * st;
		double Nyz = yz * ct + zz * st;
		double Nzx = zx * ct - yx * st;
		double Nzy = zy * ct - yy * st;
		double Nzz = zz * ct - yz * st;
		yx = Nyx;
		yy = Nyy;
		yz = Nyz;
		zx = Nzx;
		zy = Nzy;
		zz = Nzz;
    }
    public void yrot(double theta) {		//rotate teta rad arround y axis
		double ct = Math.cos(theta);
		double st = Math.sin(theta);
		double Nxx = xx * ct + zx * st;
		double Nxy = xy * ct + zy * st;
		double Nxz = xz * ct + zz * st;
		float Nzx = (float) (zx * ct - xx * st);
		float Nzy = (float) (zy * ct - xy * st);
		float Nzz = (float) (zz * ct - xz * st);
		xx = Nxx;
		xy = Nxy;
		xz = Nxz;
		zx = Nzx;
		zy = Nzy;
		zz = Nzz;
    }	public void rotatept(Point3d p){
		double x=xx*p.x+xy*p.y+xz*p.z;
		double y=yx*p.x+yy*p.y+yz*p.z;
		double z=zx*p.x+zy*p.y+zz*p.z;		p.setLocation ((int)x,(int)y,z);			}
	public void update (Vector V,Vector Vp){
		int i,n=V.size ();
		Point3d p,q;
		for (i=0;i<n;i++){
			p=(Point3d) V.elementAt (i);
			q=(Point3d) Vp.elementAt (i);
			q.setLocation (p);
			rotatept(q);
		}
	}
} 
class ShowPanel extends Panel implements MouseListener, MouseMotionListener, Runnable {
	DrawPanel inputp;		  //the right panel pointer
    Vector Vo = new Vector(); //the coords of the original figure
    Vector Vc = new Vector(); //the coords of the current (modified) figure
    Vector Vp = new Vector(); //the coords of the display figure (after rotation)
    Vector E = new Vector();  //the edge matrix
    Vector P = new Vector();  //the list of parallel edges
	Vector Th= new Vector();  //a list with all angles
	double T[];  //the current angles of the figure
	Point3d C=new Point3d (0,0,0); //coords of the center of the figure
	Point pi,pf;			//points to keep track of the mouse drag
	double minen;
	Matrix m=new Matrix (); //matrix that does the rotation
	Thread t;
	int method=0;
	Label energy;
	Label status;
	
    public ShowPanel(DrawPanel target, Label energy, Label status) {
		this.inputp =target;
		this.energy =energy;
		this.status =status;
        setBackground(Color.white);
        addMouseMotionListener(this);
        addMouseListener(this);
		add(energy);
		t = new Thread(this);		
    }
	public double dist(Point3d a, Point3d b){
		//computes the distace between 2 pts in 3d
		return Math.sqrt ((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
	}
	public double costheta(Point3d a,Point3d b, Point3d c){
		//computes the cosine of the angle abc
		double ab,ac,bc,co;
		ab=dist(a,b);
		bc=dist(b,c);
		ac=dist(a,c);
		co=(ab/bc+bc/ab-ac*ac/ab/bc)/2.;
		return co;
	}
	public double theta(Point3d a,Point3d b, Point3d c){
		//computes the  angle abc
		double co;
		co=costheta(a,b,c);
		co=Math.max (co,-1);
		co=Math.min (co,1);
		return Math.acos (co);
	}
	public double af(){
		//attraction force of the edges -does not allow edges to grow too big
		int i;double fa=0,d;
		Point q;
		for (i=0;i<E.size ();i++){
			q=(Point)E.elementAt (i);
			d=dist((Point3d)Vc.elementAt (q.x),(Point3d)Vc.elementAt (q.y));
			fa=fa+d;
		}
		return fa;
	}
	public double ep(){
		//potential energy 
		int i,k; double e=0,d;
		Point3d p1,p2;
		for (i=0;i<Vc.size ();i++){
			p1=(Point3d)Vc.elementAt(i);
			for (k=i+1;k<Vc.size ();k++){
				p2=(Point3d)Vc.elementAt(k);
				d=dist(p1,p2);
				e=e+1/d;
			}
		}
		return e;
	}
	public double sda(){
		double sum=0.,med=0.;
		int i,k;
		Point3d p,p1,p2,p3;
		p1=new Point3d(0,0,0);
		p2=new Point3d(0,0,0);
		p3=new Point3d(0,0,0);
		for (i=0;i<Th.size ();i++){
			p=(Point3d) Th.elementAt (i);
			p1.setLocation ((Point3d)Vc.elementAt(p.x));
			p2.setLocation ((Point3d)Vc.elementAt(p.y));
			p3.setLocation ((Point3d)Vc.elementAt((int)p.z));			
			T[i]=theta(p1,p2,p3);
			med=med+T[i];
		}
		med=med/Th.size ();
		for (i=0;i<Th.size ();i++){
			sum=sum+(T[i]-med)*(T[i]-med);
		}
		return sum/Th.size ();		
	}
	public void findangles(){
		int i,j,ne=E.size ();
		Point p1,p2;
		Th.removeAllElements ();
		for (i=0;i<ne;i++){
			p1=(Point)E.elementAt (i);
			for(j=i+1;j<ne;j++){
				p2=(Point)E.elementAt (j);
				if (p1.x==p2.x) Th.addElement (new Point3d (p1.y,p1.x,p2.y));
				if (p1.x==p2.y) Th.addElement (new Point3d (p1.y,p1.x,p2.x));
				if (p1.y==p2.x) Th.addElement (new Point3d (p1.x,p1.y,p2.y));
				if (p1.y==p2.y) Th.addElement (new Point3d (p1.x,p1.y,p2.x));
			}
		}
		T=new double [Th.size ()];
	}
	public double edgeangle(Point p,Point q){
		Point3d p1,p2,q1,q2;
		Point3d pr,qr,or;
		p1=(Point3d) Vc.elementAt (p.x);
		p2=(Point3d) Vc.elementAt (p.y);
		q1=(Point3d) Vc.elementAt (q.x);
		q2=(Point3d) Vc.elementAt (q.y);
		pr=new Point3d (p2.x-p1.x,p2.y-p1.y,p2.z-p1.z);
		qr=new Point3d (q2.x-q1.x,q2.y-q1.y,q2.z-q1.z);
		or=new Point3d (0,0,0);
		return Math.sin(theta(pr,or,qr));
	}
	public void findparallel(){
		//finds all pairs of parallel edges and puts in list
		//the indices of those edges
		int i,j;
		Point p,q;
		P.removeAllElements ();
		for(i=0;i<E.size ();i++){
			p=(Point)E.elementAt (i);
			for (j=i+1;j<E.size ();j++){
				q=(Point)E.elementAt (j);
				if (edgeangle(p,q)<0.1) 
					P.addElement (new Point (i,j));
			}
		}
	}
	public double sdp(){
		int i;
		double sum=0;
		Point p,q,r;
		for(i=0;i<P.size ();i++){
			p=(Point)P.elementAt (i);
			q=(Point)E.elementAt (p.x);
			r=(Point)E.elementAt (p.y);
			sum=sum+edgeangle(q,r);
		}
		return sum/(P.size ()+1);
	}
	public void recenter(Vector V){
		double	m=0;
		Point3d p;
		for (int k=0;k<V.size ();k++){
			p=(Point3d)V.elementAt (k);
			m=m+p.z;
		}
		m=m/V.size ();
		for (int k=0;k<V.size ();k++){
			p=(Point3d)V.elementAt (k);
			p.z=p.z-m;
		}					
	}
	public void setDrawMode(int mode) {
    }
    public void mousePressed(MouseEvent e) {
		pi=e.getPoint ();
   }
    public void mouseDragged(MouseEvent e) {
        e.consume();
		pf=e.getPoint ();
		m.xrot ((pi.y-pf.y) * 7./getSize().height);
		m.yrot ((pi.x-pf.x )* 7./getSize().width);
		pi=pf;
        repaint();	
    }
    public void mouseReleased(MouseEvent e) {
    }
    public void mouseMoved(MouseEvent e) {
    }
    public void mouseEntered(MouseEvent e) {
    }
    public void mouseExited(MouseEvent e) {
    }
    public void mouseClicked(MouseEvent e) {
    }
	public void changept(Point3d pt,Point3d p0,double dx,int l){
		//change the value of pt such that it is that of p0
		//plus dx, for the variable given by l.
		if (l>1){
			pt.z=p0.z+dx;			
		}
		else {
			if (l==1){
				pt.y=p0.y+(int)dx;				
			}
			else pt.x=p0.x+(int)dx;	
		}
	}		
	public double En(double c1,double c2,double c3){
		//energy function
		return (sda()+sdp()*c1+ep()*c2+af()*c3);
	}
	public boolean Gibbs(double temp,double c1,double c2,double c3){
		int i,j,k,nsamples=1,ns=2*nsamples+1;
		double m=10000,r,sum,dx=1,p[],e[];
		Point3d pt,p0=new Point3d (0,0,0);
		String s="energy   ";
		e=new double [ns]; 
		p=new double [ns]; 
		m=10000;
		i=(int)(Math.random ()*Vc.size());
		p0.setLocation ((Point3d)Vc.elementAt (i));
		pt=(Point3d)Vc.elementAt (i);
		for (k=0;k<ns;k++){
			changept(pt,p0,(k-nsamples)*dx,2);
			e[k]=En(c1,c2,c3);
			m=Math.min(e[k],m);
		}
		changept(pt,p0,0,2);
		sum=0;
		for (k=0;k<ns;k++){
			p[k]=Math.exp(-e[k]/m/temp);
			sum=sum+p[k];
		}
		r=Math.random ()*sum;
		sum=0;
		for (k=0;k<ns;k++){
			sum=sum+p[k];
			if (r<=sum){
//			if (e[i*ns+k]==m){
				pt=(Point3d)Vc.elementAt (i);
				p0.setLocation (pt);
				changept(pt,p0,(k-nsamples)*dx,2);
				break;
			}
		}
		m=En(c1,c2,c3);
		k=(int)(1000*m);r=((int)(temp*1000))/1000.;
		energy.setText ("E="+s.valueOf(k/1000.)+",T="+s.valueOf(r));
		if(m<0.001) return true;
		else return false;
	}
	public boolean Marill(double c1,double c2,double c3){
		int dz=1,n=Vc.size ();
		int i,minat,mindz;
		double r;
		Point3d p;
		String s="energy   ";
		mindz=0;minat=-1;
		for (i=0;i<Vc.size ();i++){
			p=(Point3d)Vc.elementAt (i);
			p.z=p.z-dz;
			r=En(c1,c2,c3);
			if (r<minen){
				minen=r;
				mindz=-dz;minat=i;
			}
			p.z=p.z+2*dz;
			r=En(c1,c2,c3);
			if (r<minen){
				minen=r;
				mindz=dz;minat=i;
			}
			p.z=p.z-dz;
		}
		if((minat==-1)||(minen<0.001)){
			return true;
		}
		else{
			p=(Point3d)Vc.elementAt (minat);
			p.z=p.z+mindz;
			i=(int)(10000*minen);
			energy.setText ("E="+s.valueOf(i/10000.));
			return false;
		}
	}
	public void run(){
		int i;
		E.removeAllElements ();
		Vo.removeAllElements ();
		Vc.removeAllElements ();
		Vp.removeAllElements ();
		if (inputp.V.size ()>0){
			status.setText("Optimizing. Please wait until done.");
			for(i=0;i<inputp.E.size ();i++){
				E.addElement (new Point ((Point)inputp.E.elementAt (i)));
			}
			C.setLocation (0,0,0);
			for(i=0;i<inputp.V.size ();i++){
				Point p=(Point)inputp.V.elementAt (i);
				C.x+=p.x;
				C.y+=p.y;
			}
			C.x=C.x/inputp.V.size ();
			C.y=C.y/inputp.V.size ();		
			for(i=0;i<inputp.V.size ();i++){
				Point p=(Point)inputp.V.elementAt (i);
				Vo.addElement (new Point3d (p.x-C.x,p.y-C.y,0));
				Vc.addElement (new Point3d (p.x-C.x,p.y-C.y,0));
				Vp.addElement (new Point3d (p.x-C.x,p.y-C.y,0));			
			}
			findangles ();
			findparallel();
			m.reset ();
			m.xrot (Math.PI /2);m.yrot (0.5);m.xrot (-0.2);
			repaint ();
			minen=10000.;
			if (method==1){
				for(i=1;(i<4000)&&(!Marill(1./30.,1,1./500.));i++){
					recenter(Vc);
					repaint();
				}
				status.setText("Now looking only at SDA.");
				for(i=1;(i<4000)&&(!Marill(0,0,0));i++){
					recenter(Vc);
					repaint();
				}
			}
			else{
				for(i=1;(i<3000)&&(!Gibbs(30/(double)i,1./30.,1,1./500.));i++){
					recenter(Vc);
					repaint();
				}			
				status.setText("Now applying Marill's descent.");
				for(i=1;(i<4000)&&(!Marill(1./30.,1,1./500.));i++){
					recenter(Vc);
					repaint();
				}
				status.setText("Now looking only at SDA.");
				for(i=1;(i<4000)&&(!Marill(0,0,0));i++){
					recenter(Vc);
					repaint();
				}
			}
			status.setText("Done.");
		}
	}
    public void paint(Graphics g) {
        /* draw the current lines and points */
        int ne = E.size();
		int sizex=getSize().width /2,sizey=getSize().height /2;
		Point3d p0,p; 
		Point idx;
        g.setColor(Color.black);
        g.setPaintMode();
		m.update (Vc,Vp);
		for (int i=0;i<ne;i++) {
			idx= (Point) E.elementAt (i);
			p0=(Point3d)Vp.elementAt(idx.x);
			p=(Point3d)Vp.elementAt(idx.y);
		    g.drawLine(p0.x+sizex, p0.y+sizey, p.x+sizex, p.y+sizey);
		}
	}
}

class Controls extends Panel implements ActionListener {
    DrawPanel target;
	ShowPanel showp;
	Button bClear,bDelete,bMarill,bGibbs,bMove,bfig1,bfig2,bfig3;
	Label status;
    public Controls(DrawPanel target, ShowPanel showp, Label status) {
        this.target = target;
		this.showp=showp;
		this .status =status;
        setLayout(new GridLayout(2,4));
		setBackground(Color.lightGray);
		target.setForeground(Color.black );		bClear = new Button("Clear");		add(bClear);
		bClear.addActionListener (this);
		bClear.setForeground(Color.black);
		bDelete = new Button("Delete");		add(bDelete);
		bDelete.addActionListener (this);
		bDelete.setForeground(Color.black);
		bMove = new Button("Move");		add(bMove);
		bMove.addActionListener (this);
		bMove.setForeground(Color.black);
		bMarill = new Button("Marill");		add(bMarill);
		bMarill.addActionListener (this);
		bMarill.setForeground(Color.black);
		bfig1 = new Button("Fig.1");		add(bfig1);
		bfig1.addActionListener (this);
		bfig1.setForeground(Color.black);
		bfig2 = new Button("Fig.2");		add(bfig2);
		bfig2.addActionListener (this);
		bfig2.setForeground(Color.black);
		bfig3 = new Button("Fig.3");		add(bfig3);
		bfig3.addActionListener (this);
		bfig3.setForeground(Color.black);
		bGibbs = new Button("Gibbs");		add(bGibbs);
		bGibbs.addActionListener (this);
		bGibbs.setForeground(Color.black);
    }

    public void paint(Graphics g) {
        Rectangle r = getBounds();
		g.setColor(Color.lightGray);
        g.draw3DRect(0, 0, r.width-1, r.height-1, true);
    }
public void actionPerformed(ActionEvent e) {
	Object src = e.getSource();
	showp.energy.setText ("E=N/A   ");
	if (src == bClear) {
		if (showp.t.isAlive ()){showp.t.stop() ;}		target.V.removeAllElements();
		target.E.removeAllElements ();
		target.pi.setLocation(-1,-1);		target.pf=target.pi;				target.movemode=false;		
		target.delmode=false;
		bMove.setLabel("Move");
		bDelete.setLabel("Delete");
		status.setText ("Draw object in the right panel.  ");
		target.repaint();		showp.Vc.removeAllElements ();
		showp.Vp.removeAllElements ();
		showp.E.removeAllElements ();
		showp.repaint ();
	}  
	if (src == bDelete) {		target.movemode=false;		
		bMove.setLabel("Move");
		if (target.delmode){
			target.delmode=false;
			bDelete.setLabel("Delete");
			status.setText ("Draw object in the right panel.  ");
		}
		else{
			target.delmode=true;
			bDelete.setLabel("Add");
			status.setText ("Delete elements from the right panel.");
		}
	}  
	if (src == bMove) {		target.delmode =false;
		bDelete.setLabel("Delete");
		if (target.movemode){
			target.movemode=false;
			bMove.setLabel("Move");
			status.setText ("Draw object in the right panel.  ");
		}
		else{
			target.movemode=true;
			bMove.setLabel("Add");
			status.setText ("Move vertices and edges.  ");
		}
	}  
	if (src == bGibbs) {
		if (showp.t.isAlive ()){showp.t.stop() ;}		showp.method=2;
		showp.t = new Thread(showp);
        showp.t.start ();
	}  
	if (src == bMarill) {
		if (showp.t.isAlive ()){showp.t.stop() ;}		showp.method =1;
		showp.t = new Thread(showp);
        showp.t.start ();
	}  
	if (src == bfig1) {
		target.V.removeAllElements ();
		target.E.removeAllElements ();
		target.V.addElement (new Point (80,60));
		target.V.addElement (new Point (140,60));
		target.V.addElement (new Point (160,110));
		target.V.addElement (new Point (140,160));
		target.V.addElement (new Point (80,160));
		target.V.addElement (new Point (60,110));
		
		target.V.addElement (new Point (100,40));
		target.V.addElement (new Point (160,40));
		target.V.addElement (new Point (180,90));
		target.V.addElement (new Point (160,140));
		target.V.addElement (new Point (100,140));
		target.V.addElement (new Point (80,90));

		target.E.addElement (new Point (0,1));
		target.E.addElement (new Point (1,2));
		target.E.addElement (new Point (2,3));
		target.E.addElement (new Point (3,4));
		target.E.addElement (new Point (4,5));
		target.E.addElement (new Point (5,0));
		target.E.addElement (new Point (6,7));
		target.E.addElement (new Point (7,8));
		target.E.addElement (new Point (8,9));
		target.E.addElement (new Point (9,10));
		target.E.addElement (new Point (10,11));
		target.E.addElement (new Point (11,6));
		
		target.E.addElement (new Point (0,6));
		target.E.addElement (new Point (1,7));
		target.E.addElement (new Point (2,8));
		target.E.addElement (new Point (3,9));
		target.E.addElement (new Point (4,10));
		target.E.addElement (new Point (5,11));
		target.repaint ();
	}
	if (src == bfig2) {
		target.V.removeAllElements ();
		target.E.removeAllElements ();
		target.V.addElement (new Point (60,180));
		target.V.addElement (new Point (60,140));
		target.V.addElement (new Point (100,140));
		target.V.addElement (new Point (100,100));
		target.V.addElement (new Point (140,100));
		target.V.addElement (new Point (140,60));
		target.V.addElement (new Point (180,60));
		target.V.addElement (new Point (80,200));
		target.V.addElement (new Point (80,160));
		target.V.addElement (new Point (120,160));
		target.V.addElement (new Point (120,120));
		target.V.addElement (new Point (160,120));
		target.V.addElement (new Point (160,80));
		target.V.addElement (new Point (200,80));
		target.V.addElement (new Point (200,200));
		
		target.E.addElement (new Point (0,1));
		target.E.addElement (new Point (1,2));
		target.E.addElement (new Point (2,3));
		target.E.addElement (new Point (3,4));
		target.E.addElement (new Point (4,5));
		target.E.addElement (new Point (5,6));
		target.E.addElement (new Point (0,7));
		target.E.addElement (new Point (1,8));
		target.E.addElement (new Point (2,9));
		target.E.addElement (new Point (3,10));
		target.E.addElement (new Point (4,11));
		target.E.addElement (new Point (5,12));
		target.E.addElement (new Point (6,13));
		target.E.addElement (new Point (8,7));
		target.E.addElement (new Point (9,8));
		target.E.addElement (new Point (10,9));
		target.E.addElement (new Point (11,10));
		target.E.addElement (new Point (12,11));
		target.E.addElement (new Point (13,12));
		target.E.addElement (new Point (7,14));
		target.E.addElement (new Point (13,14));
		target.repaint ();
	}
	if (src == bfig3) {
		target.V.removeAllElements ();
		target.E.removeAllElements ();
		target.V.addElement (new Point (60,70));
		target.V.addElement (new Point (60,170));
		target.V.addElement (new Point (170,170));
		target.V.addElement (new Point (170,70));
		target.V.addElement (new Point (80,50));
		target.V.addElement (new Point (80,150));
		target.V.addElement (new Point (190,150));
		target.V.addElement (new Point (190,50));
		
		target.V.addElement (new Point (30,110));
		target.V.addElement (new Point (110,180));
		target.V.addElement (new Point (200,110));
		target.V.addElement (new Point (110,40));
		target.V.addElement (new Point (120,130));
		target.V.addElement (new Point (140,110));
		target.E.addElement (new Point (0,1));
		target.E.addElement (new Point (1,2));
		target.E.addElement (new Point (2,3));
		target.E.addElement (new Point (3,0));
		target.E.addElement (new Point (4,5));
		target.E.addElement (new Point (5,6));
		target.E.addElement (new Point (6,7));
		target.E.addElement (new Point (7,4));
		target.E.addElement (new Point (0,4));
		target.E.addElement (new Point (1,5));
		target.E.addElement (new Point (2,6));
		target.E.addElement (new Point (3,7));
		target.E.addElement (new Point (8,1));
		target.E.addElement (new Point (8,4));
		target.E.addElement (new Point (8,5));
		target.E.addElement (new Point (8,0));
		target.E.addElement (new Point (9,1));
		target.E.addElement (new Point (9,2));
		target.E.addElement (new Point (9,5));
		target.E.addElement (new Point (9,6));
		target.E.addElement (new Point (10,2));
		target.E.addElement (new Point (10,3));
		target.E.addElement (new Point (10,6));
		target.E.addElement (new Point (10,7));
		target.E.addElement (new Point (11,3));
		target.E.addElement (new Point (11,0));
		target.E.addElement (new Point (11,4));
		target.E.addElement (new Point (11,7));
		target.E.addElement (new Point (12,0));
		target.E.addElement (new Point (12,1));
		target.E.addElement (new Point (12,2));
		target.E.addElement (new Point (12,3));
		target.E.addElement (new Point (13,4));
		target.E.addElement (new Point (13,5));
		target.E.addElement (new Point (13,6));
		target.E.addElement (new Point (13,7));
		target.repaint ();
	}
  }
}




