import java.awt.*;

public class PremicaDemo extends java.applet.Applet {
	DrawPad dpxy,dpxz;
	DrawPad3D dp3D;
	int i,j,k,xy=0,xz=0,cc=1;
	float x1,y1,z1,x2,y2,z2;
    String text;
	static final int POZ = 100;
	static final int NEG = 100;
	static final int M = 200;
	Panel inp = new Panel(), p1 = new Panel(), 
		  p2 = new Panel(), p3 = new Panel();
	
	Tocka Dp = new Tocka(POZ,POZ,POZ); 	// oglisce prvega oktanta
	Tocka Dn = new Tocka(-NEG,-NEG,-NEG); // oglisce sedmega oktanta
	
	Tocka P1 = new Tocka(),P2 = new Tocka(),P3 = new Tocka();
	Tocka P[] = new Tocka[3];
	InputPanel in = new InputPanel();
	Vektor V = new Vektor();

	public void init() {
		inp.add( in.setInput() );
		p3.add( dp3D = new DrawPad3D((int)(M * 1.2f),Dp,Dn) );
 		p2.add( dpxz = new DrawPad(M,0,2,Dp,Dn) );
 		p1.add( dpxy = new DrawPad(M,0,1,Dp,Dn) );

		setLayout(new GridLayout(2,2));
		add(p2);
		add(p3);
		add(p1);
		add(inp);
		
		resize(preferredSize());
		validate();
	}
    public boolean mouseDown(Event e, int x, int y) {
        int s=0;
		if(e.target == dpxy) {
		    s = 1 + xy % 2; // sodo -> 1, liho -> 2
		    xy++;
		}
		if(e.target == dpxz) {
		    s = 3 + xz % 2; // sodo -> 3, liho -> 4
		    xz++;
		}
        switch(s) {
        case 1:
            x1 = -dpxy.oldx + M/2;
            y1 =  dpxy.oldy - M/2;
    	    break;
        case 2:
            x2 = -dpxy.oldx + M/2;
            y2 =  dpxy.oldy - M/2;
    	    break;
        case 3:
            z1 = -dpxz.oldy + M/2;
    	    break;
        case 4:
            z2 = -dpxz.oldy + M/2;
    	    break;
		}
        if(s != 0) {
    		dp3D.set(x1,y1,z1,x2,y2,z2);
    		dpxy.set(x1,y1,z1,x2,y2,z2);
    		dpxz.set(x1,y1,z1,x2,y2,z2);
    	}
		return true;
    }
	public boolean action(Event e,Object o) {
		if(e.target == in.cb1) { in.cards.show(in.p2,"vp1"); cc = 1;}
		if(e.target == in.cb2) { in.cards.show(in.p2,"vp2"); cc = 2;}
		if(e.target == in.cb3) { in.cards.show(in.p2,"vp3"); cc = 3;}
		if(e.target == in.cb4) { in.cards.show(in.p2,"vp4"); cc = 4;}

		if(e.target == in.b1) {
            switch(cc) {
            case 1:
            	text = in.T1.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
       	    	k = text.indexOf(',',j+1);
           		P1.x = Float.valueOf(text.substring(0,j)).floatValue();
           		P1.y = Float.valueOf(text.substring(j+1,i)).floatValue();
           		P1.z = Float.valueOf(text.substring(k+1,i)).floatValue();
            	
            	text = in.T2.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
       	    	k = text.indexOf(',',j+1);
           		P2.x = Float.valueOf(text.substring(0,j)).floatValue();
           		P2.y = Float.valueOf(text.substring(j+1,i)).floatValue();
           		P2.z = Float.valueOf(text.substring(k+1,i)).floatValue();
    			
    			dp3D.set(P1,P2);
    			dpxy.set(P1,P2);
    			dpxz.set(P1,P2);
        	    break;
            case 2:
            	text = in.T.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
       	    	k = text.indexOf(',',j+1);
           		P3.x = Float.valueOf(text.substring(0,j)).floatValue();
           		P3.y = Float.valueOf(text.substring(j+1,i)).floatValue();
           		P3.z = Float.valueOf(text.substring(k+1,i)).floatValue();

            	text = in.V.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
       	    	k = text.indexOf(',',j+1);
           		V.x = Float.valueOf(text.substring(0,j)).floatValue();
           		V.y = Float.valueOf(text.substring(j+1,i)).floatValue();
           		V.z = Float.valueOf(text.substring(k+1,i)).floatValue();

    			dp3D.set(P3,V);
    			dpxy.set(P3,V);
    			dpxz.set(P3,V);
        	    break;
            case 3:
             	text = in.T1t.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
           		x1 = Float.valueOf(text.substring(0,j)).floatValue();
           		y1 = Float.valueOf(text.substring(j+1,i)).floatValue();
            	
            	text = in.T2t.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
           		x2 = Float.valueOf(text.substring(0,j)).floatValue();
           		y2 = Float.valueOf(text.substring(j+1,i)).floatValue();

            	text = in.T1n.getText();
           		z1 = Float.valueOf(text).floatValue();

            	text = in.T2n.getText();
           		z2 = Float.valueOf(text).floatValue();

       			dp3D.set(x1,y1,z1,x2,y2,z2);
    			dpxy.set(x1,y1,z1,x2,y2,z2);
    			dpxz.set(x1,y1,z1,x2,y2,z2);
        	    break;
            case 4:
            	text = in.A.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
       	    	k = text.indexOf(',',j+1);
           		P[0] = new Tocka(Float.valueOf(text.substring(0,j)).floatValue(),
           						 Float.valueOf(text.substring(j+1,i)).floatValue(),
           						 Float.valueOf(text.substring(k+1,i)).floatValue());

            	text = in.B.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
       	    	k = text.indexOf(',',j+1);
           		P[1] = new Tocka(Float.valueOf(text.substring(0,j)).floatValue(),
           						 Float.valueOf(text.substring(j+1,i)).floatValue(),
           						 Float.valueOf(text.substring(k+1,i)).floatValue());

            	text = in.C.getText();
            	i = text.length();
       	    	j = text.indexOf(',',0);
       	    	k = text.indexOf(',',j+1);
           		P[2] = new Tocka(Float.valueOf(text.substring(0,j)).floatValue(),
           						 Float.valueOf(text.substring(j+1,i)).floatValue(),
           						 Float.valueOf(text.substring(k+1,i)).floatValue());     
            
    			dp3D.set(P);
    			dpxy.set(P);
    			dpxz.set(P);
        	    break;
    		}
		}		
		if(e.target == in.b2) {
			dpxy.clear();
			dpxz.clear();
			dp3D.clear();
            x1=0; y1=0; z1=0; x2=0; y2=0; z2=0;
		}		
		if(e.target == in.b3) {
            in.T1.setText("-20,-70,40");
            in.T2.setText("70,20,-80");
            in.T.setText("80,70,-50");
            in.V.setText("-3,-2,3");
            in.T1t.setText("55,10");
            in.T1n.setText("50");
            in.T2t.setText("-60,-70");
            in.T2n.setText("50");
            in.A.setText("100,100,100");
            in.B.setText("100,-100,0");
            in.C.setText("-100,100,0");
		}		
        if(e.target == in.T1) {
        	in.T2.requestFocus();
        	in.T2.selectAll();
    	}
        if(e.target == in.T2) {
          	in.T1.requestFocus();
        	in.T1.selectAll();
		}
        if(e.target == in.T) {
        	in.V.requestFocus();
        	in.V.selectAll();
    	}
        if(e.target == in.V) {
        	in.T.requestFocus();
        	in.T.selectAll();
    	}
        if(e.target == in.T1t) {
        	in.T2t.requestFocus();
        	in.T2t.selectAll();
    	}
        if(e.target == in.T2t) {
        	in.T1n.requestFocus();
        	in.T1n.selectAll();
		}
        if(e.target == in.T1n) {
        	in.T2n.requestFocus();
        	in.T2n.selectAll();
    	}
        if(e.target == in.T2n) {
        	in.T1t.requestFocus();
        	in.T1t.selectAll();
		}
        if(e.target == in.A) {
        	in.B.requestFocus();
        	in.B.selectAll();
		}
        if(e.target == in.B) {
        	in.C.requestFocus();
        	in.C.selectAll();
		}
        if(e.target == in.C) {
        	in.A.requestFocus();
        	in.A.selectAll();
		}
		return true;
	}
}