
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.util.*;
import java.io.*;

public class Three extends Applet implements Runnable {
	
  Thread design;	
  int w, h;

  private Font font;
  private Color black, white;

  Image offscreen;
  Dimension offscreensize;
  Graphics offgraphics;

  int x0,x1,x2,x3,y0,y1,y2,y3;
  int counter2=0;

  int counter;
  Vector theVect = new Vector();
  Vector xVect, yVect, tVect;
  int clickNumber = 3;


	
  public String getAppletInfo() {
      	return "Thales seriEs: carpetPoetry, by joey beRzowski";
  } 
  
  public void start() {
       	repaint();
       	if (design == null) {
	       	design = new Thread(this);
	       	design.start();
       	}      
  } // start


  public void stop() {
       	if ((design != null) && design.isAlive())
	       	design.stop();
       	design = null; 
   } // stop


      public void destroy() {
		if ((design != null) && design.isAlive())
			design.stop();
		design = null;
      } // destroy

	 
  public void run() {

       while(true) {
		
 	repaint();  
        	try {Thread.sleep(30);}
      	catch (InterruptedException e) {}
			
       }	

  }
	
  public void init() {

	theVect.addElement(new aPoint(0,0,0));
	theVect.addElement(new aPoint(0,0,0));
	theVect.addElement(new aPoint(0,0,0));
       	white =  Color.white;
       	black =  Color.black;
       	this.setBackground(white);
       	this.setForeground(black);	
  } // init
		
  public void update(Graphics g) {
       	paint(g);
	
  } // update

	
  public void paint(Graphics g) {

       	Dimension d = size();
       	w=d.width;
       	h=d.height;
       	if ((offscreen == null) || (w != offscreensize.width) || (h != offscreensize.height)) {
       		offscreen = createImage(d.width, d.height);
	       	offscreensize = d;
	       	offgraphics = offscreen.getGraphics();
       	}
       	offgraphics.setColor(white);		
       	offgraphics.fillRect(0,0,w,h);

  	offgraphics.setColor(black);


	int xMin, xMid, xMax, yMin, yMid, yMax;
	  yMin = Math.min(y0,Math.min(y1,y2));
	  yMax = Math.max(y0,Math.max(y1,y2));

	  if (yMin == y0) {
	    xMin = x0;
	    if (yMax == y1) {
	      xMax = x1;
	      yMid = y2;
	      xMid = x2;
	    }
	    else {
	      xMax = x2;
	      yMid = y1;
	      xMid = x1;
	    } 
	  }

	  else {
	    if (yMin == y1) {
	    xMin = x1;
	    if (yMax == y2) {
	      xMax = x2;
	      yMid = y0;
	      xMid = x0;
	    }
	    else {
	      xMax = x0;
	      yMid = y2;
	      xMid = x2;
	    } 
	  }

	  else {
	    xMin = x2;
	    if (yMax == y1) {
	      xMax = x1;
	      yMid = y0;
	      xMid = x0;
	    }
	    else {
	      xMax = x0;
	      yMid = y1;
	      xMid = x1;
	    } 
	  }
	  }
	  int[] xes1 = {-1,  xMin+xMin/2,  xMin,  -1,           -1};
	  int[] yes1 = {-1,  -1,           yMin,  yMin-yMin/3,  -1};

	  int[] xes1b = {-1,  xMin+xMin/2-3,  xMin-7,  -1-3,           -1};
	  int[] yes1b = {-1-5,  -1-5,           yMin-3,  yMin-yMin/3-7,  -1-5};

	  int[] xes2 = {-1,                  x2,    w,     w,  xMax+xMax/3,  xMax,  -1,       -1};
	  int[] yes2 = {yMid-(yMid-yMin)/2,  yMid,  yMin,  h,  h,            yMax,  yMax+30,  yMid-(yMax-yMin)/2};

	  int[] xes2b = {-1,                  x2,    w,     w,  xMax+xMax/3+5,  xMax+3,  -1,       -1};
	  int[] yes2b = {yMid-(yMid-yMin)/2+5, yMid+3, yMin+5, h+7, h+5, yMax-3, yMax+30-5, yMid-(yMax-yMin)/2+5};


	  if (counter2 == 3) {
	     offgraphics.fillPolygon(xes1, yes1, 5);
	     offgraphics.fillPolygon(xes2, yes2, 8);

	     offgraphics.setColor(white);
	     offgraphics.fillPolygon(xes1b, yes1b, 5);
	     offgraphics.fillPolygon(xes2b, yes2b, 8);
	     offgraphics.setColor(black);



	  }
	  if (counter2 == 4) {
	    offgraphics.fillPolygon(xes1, yes1, 5);
	    offgraphics.fillPolygon(xes2, yes2, 8);

	  }
       	g.drawImage(offscreen, 0, 0, null);	
  } // paint
	 

	
  public boolean mouseUp(Event e, int x, int y) {

    if (counter2==0) {
      //     theVect.removeAllElements();
   
      x0=x;y0=y;
      //   theVect.addElement(new aPoint(e.when, x, y));
      counter2++;
    }
    else if (counter2==1) {
      x1=x;y1=y;
      theVect.addElement(new aPoint(e.when, x, y));
      counter2++;
    }
    else if (counter2==2) {
      x2=x;y2=y;
      theVect.addElement(new aPoint(e.when, x, y));
      counter2++;
     }
    else if (counter2==3) {
      counter2++;
    
    }
    else if (counter2==4) {
      counter2=0;
      repaint();
    }
  



    return true;
  }

}

class aPoint extends java.lang.Object {
    long time;
    int x, y;

    public aPoint(long time, int x, int y) {
        this.time = time;
	this.x = x;
        this.y = y;
    }
}
