import java.awt.*;
import java.applet.*;

class drawCanvas extends Canvas {
Point point = null;
        boolean xready,yready,zready;
public int tpointx[]=new int[256];
int tpointy[]=new int[256];
int fpointx[]=new int[256];
int fpointy[]=new int[256];
static int txp[]=new int[36],typ[]=new int[36];
static int fxp[]=new int[36],fyp[]=new int[36];
//public boolean tready=false,fready=false;
static boolean ffinished,tfinished;
int tc=0,fc=0;
public int f,txoffset;
Polygon tp,fp;

    public void paint(Graphics g) {
 
        int w = size().width;
        int h = size().height;

        g.drawRect(0, 0, w - 1, h - 1);

		if (tp!=null){
			g.fillPolygon(tp);			
//			System.out.println("topp: "+tp+"x: "+tp.xpoints);
			if(scanpoints("top",tp,g)) tfinished=true;
			}else{
			g.drawLine(w/2,0,w/2,h);
			g.drawLine(0,h/2,w,h/2);
			}
		if (fp!=null){
			g.fillPolygon(fp);			
			if(scanpoints("front",fp,g)) ffinished=true;
			}else{
			g.drawLine(w/2,0,w/2,h);
			g.drawLine(0,h/2,w,h/2);
			}
}


    public boolean mouseDown(Event event, int x, int y) {

		if (event.target==create_key.top){
            		if(tc<255){
				point = new Point(x, y);
				tc++;
				tpointx[tc-1]=x;
	    			tpointy[tc-1]=y;
				tpointx[tc]=tpointx[0];
	    			tpointy[tc]=tpointy[0];
			  	tp=new Polygon(tpointx,tpointy,tc+1);
			}else 
create_key.statusline.setText("use less than 256 sides!");
//System.out.println("not more than 256 sides are allowed");
		}
		if (event.target==create_key.front){
            		if(fc<255){
				point = new Point(x, y);
				fc++;
				fpointx[fc-1]=x;
	    			fpointy[fc-1]=y;
				fpointx[fc]=fpointx[0];
	    			fpointy[fc]=fpointy[0];
			  	fp=new Polygon(fpointx,fpointy,fc+1);
			
			}
else create_key.statusline.setText("use less than 256 sides!");
//else System.out.println("not more than 256 sides are allowed");
			}
//System.out.println("tc: "+tc+" fc: "+fc);
//"Click occurred at coordinate ("        + point.x + ", " + point.y + ").");

repaint();

        return false;
    }

public boolean scanpoints(String view, Polygon p,Graphics g){
int x,y,f=0,i=0;


//System.out.println("boundingb: "+p.getBoundingBox());
int w=p.getBoundingBox().width;
int h=p.getBoundingBox().height;
int boxleft=p.getBoundingBox().x;
int boxtop=p.getBoundingBox().y;
int boxright=boxleft+w;
int boxbottom=boxtop+h;
if (view=="top")txoffset=boxleft;
//			System.out.println("p: "+p+"x: "+p.xpoints+" y: "+p.ypoints);
if (w>35 && h>35){ 
System.out.println("w: "+w+" h: "+h);
for (y=boxtop;y<=boxbottom;y=y+(h/17)){
	for (x=boxleft;x<=boxright;x=x+(w/17)){
		if(p.inside(x,y)){
		    if (f < 36)
		    {
			if (view=="top"){txp[f]=x;typ[f]=y;
			}else {
				fxp[f]=x;fyp[f]=y;}
	//		System.out.println("left "+f+" x= "+xp[f]+" y= "+yp[f]);
			f++;
			break;
                     }
				}
				}
			}
if(f<16) g.drawString("polygon is too small", (w - g.getFontMetrics().stringWidth("Canvas"))/2, 30);
	
//    if (f<16)create_key.statusline.setText("polygon is too small");
/////////////////////////////////////////////////////////////////////////////////////////
i=f;
for (y=boxbottom;y>boxtop;y=y-(h/17)){
	for (x=boxright;x>boxleft;x=x-(w/17)){
		if(p.inside(x,y)){
		   if (i < 36)
		   {
			if (view=="top"){txp[i]=x;typ[i]=y;
			}else{
				 fxp[i]=x;fyp[i]=y;}			
			i++;
			break;
                    }
				}
				}
			}
if(f<16 || i<16){
	g.drawString("polygon is too small", (w - g.getFontMetrics().stringWidth("Canvas"))/2, 30);
	return false;
	}else return true;
}else{
System.out.println("!!!!!!!!!!!!!!!!!!!!!!1w: "+w+" h: "+h);
	 g.drawString("polygon is too small", (w - g.getFontMetrics().stringWidth("Canvas"))/2, 50);
	return false;}


}

public void pucol(){
int i;

tc=0;fc=0;
ffinished=false;
tfinished=false;
for(i=0;i<36;i++)
{
	txp[i]=0;
	typ[i]=0;
	fxp[i]=0;
	fyp[i]=0;
}

tp = null;
fp = null;
Graphics g=getGraphics();
repaint();
}

public boolean startwrite(){
System.out.println("tfinished: "+tfinished+" ffinished: "+ffinished);
if (ffinished && tfinished){
			create_key.statusline.setText("composing keyfile");
//			 new cr_writekey(txp,typ,fxp,fyp,txoffset);
//			create_key.statusline.setText("keyfile is ready");
			return true;
	}else create_key.statusline.setText("not enough polygons");
return false;
}
    public Dimension minimumSize() {
        return new Dimension(400,250);
    }
    public Dimension preferredSize() {
        return minimumSize();
    }
}
