// one fold of a folding paper, each fold is a polygon
// with edges drawn in the following order.
//		_______
//	     0|4	|3
//		|	|
//		|	|
//		|	|
//	     1------2
//
//




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

class Myfold extends Object {  //by default protected and file scope.
  	final double PI = 3.143 ;
	static 	int 	H = 75,W=100;
			int 	Xs[] = new int[5];
			int 	Ys[] = new int[5];
			Point TL;				//top left
			Point TR;				//top_right 			
			Point BL;				//bottom left
			Point BR;				//bottom_right 			
			
	public  Myfold(Point p) {  //constructor
		TL = new Point(p.x, p.y);
		TR = new Point(p.x+W, p.y);
		BL = new Point(p.x, p.y+H);
		BR = new Point(p.x+W, p.y+H); 
	}

	static int getWidth() {		//class method
		return W;
	}
	static void setWidth(int w) {	//class method
		W = w;
	}
	static int getHeight() {	//class method
		return H;
	}
	static void getWidth(int h) {	//class method
		H = h;
	}

	Point getTopRight() {
		return TR;
	}

	int  getX()[] {
	    Xs[0] = TL.x;
	    Xs[1] = BL.x;
	    Xs[2] = BR.x;
	    Xs[3] = TR.x;
	    Xs[4] = TL.x;
	    return Xs;
	}

	int  getY()[] {
	    Ys[0] = TL.y;
	    Ys[1] = BL.y;
	    Ys[2] = BR.y;
	    Ys[3] = TR.y;
	    Ys[4] = TL.y;
	    return Ys;
	}

	public void rotate(int theta) {
			// compute x and y array for theta degree
	   
	   
	   int newW = (int) (W* Math.cos((double)theta*PI/180)) ;
	   int HShift = (int) (W* Math.sin((double)theta*PI/180 )) ;
	 if (theta >= 0 && theta <90 ) {  // if the turn is Positive  
	   BL.x = TL.x; 
	   TR.x = BR.x = TL.x + newW;
	   BL.y = TL.y +H;
	   TR.y = TL.y - HShift;
	   BR.y = BL.y - HShift;
	  } else if (theta >=90 && theta <=180) {
	   BL.x = TL.x; 
	   TR.x = BR.x = TL.x + (-newW);  // newW willbe -ve for angle > 90
	   BL.y = TL.y +H;
	   TR.y = TL.y + HShift;
	   BR.y = BL.y + HShift;
     	 }
	 else 
	   System.out.println("rotation > 180");
	}

} //class myfold  -------------------------------------


class MyCanvas extends Canvas {
	final static int	 pts= 5; 
	int[][]  Fx;
	int[][]  Fy;

	public void redraw(int[][] X, int[][] Y) {

		Fx = new int[X.length][pts];
		Fy = new int[X.length][pts];
	  
	   for (int j=0; j< X.length; j++) 
		for (int i=0; i< pts; i++) {
			Fx[j][i] = X[j][i];
			Fy[j][i] = Y[j][i];
//			System.out.print(Fx[j][i]);
//			System.out.println(Fy[j][i]);
		}	
	    
	   repaint();
	}

	


	public void paint(Graphics g)  {
	   int k;
	   g.setColor(Color.lightGray);
   	   for (k=0; k <Fx.length; k++) 
		g.fillPolygon(Fx[k],Fy[k],pts);
	   g.setColor(Color.black);
   	   for (k=0; k <Fx.length; k++) 
		g.drawPolygon(Fx[k],Fy[k],pts);

       }

} //class MyCanvas  -------------------------------------

class ControlPanel extends Panel {
	TextField height,width,rotation,focus;
	fold 	outerparent;

	public ControlPanel(fold target) {
	   setLayout(new FlowLayout());
	
	   outerparent = target;
	   add(new Label("Height(in pixels):"));
	   height = new TextField("75",4);
	   add(height);

	   add(new Label("Width(in pixels):"));
	   width = new TextField("100",4);
	   add(width);

	   add(new Label("Angle(in degrees):"));
	   rotation = new TextField("0",4);
	   add(rotation);	
	

	   add(new Label("Focus(at %of Tape):"));
	   focus = new TextField("50",4);
	   add(focus);	
	}

	int getValue(String s) {
	  if (s.equals("height")) 
		return Integer.parseInt(height.getText());
	  if (s.equals("width")) 
		return Integer.parseInt(width.getText());
	  if (s.equals("rotation")) 
		return Integer.parseInt(rotation.getText());
	  if (s.equals("focus")) 
		return Integer.parseInt(focus.getText());
	  else
	      return 0;
	}

	public boolean action(Event evt, Object arg) {
		if(evt.target instanceof TextField) {
		  outerparent.update();		// neat method of passing it up
              return true; 
		}
		else 
		  return false;
	}

} //class ControlPanel  -------------------------------------

class ScrollPanel extends Panel {
	Scrollbar sb;
	public ScrollPanel (fold target) {
	   sb = new Scrollbar(Scrollbar.HORIZONTAL,50,5,1,100);
	   sb.resize(new Dimension(20,500));
	   add(sb);
	}

} //class ControlPanel  -------------------------------------

public class  fold  extends Applet {
	int  		 NumBoards = 15;
	int 		 rotation=50 ; 
	Panel		 cPanel;
	ControlPanel controlP; 
	ScrollPanel  scrollP;
	MyCanvas 	 cv;
	int Fx[][] = new int[NumBoards][MyCanvas.pts];
	int Fy[][] = new int[NumBoards][MyCanvas.pts];
	int i;
	Myfold f;
	Point p;
	int focus = 3;


	public void init() {
		setLayout(new BorderLayout()) ; // verti and horz gap

		cv = new MyCanvas();
		add("Center", cv);
		cv.setBackground(Color.white);		

		scrollP = new ScrollPanel(this);
		add("North", scrollP);

		controlP = new ControlPanel(this);
		add("South", controlP);
		this.update();
	}


	public void update() {
		rotation=controlP.getValue("rotation");
		float x = 100/(NumBoards)  ;
		double focusVal=controlP.getValue("focus") /x;
		focus = (int) Math.floor(focusVal);
		Myfold.H=controlP.getValue("height");
		Myfold.W=controlP.getValue("width");
		p = new Point(50,150);
		for(i=0; i<NumBoards; i++) {
		   f = new Myfold(p);
		   if (i==focus || i==focus-1) f.rotate(0);
		   else f.rotate(rotation); 
	 	   Fx[i] = f.getX();
		   Fy[i] = f.getY();
		   p = f.getTopRight();
		   rotation = 180-rotation;
		}
		cv.redraw(Fx,Fy);		

	}

	public boolean handleEvent(Event evt) {
		// only a component can be a target; a polygon is not a comp
		if(evt.target instanceof Polygon) {
			System.out.println("Pressed on Canvas");
		 	return true;
		} else	
			return super.handleEvent(evt);

	}

}  //class





