/*
    This program is exactly like the last one except that it gets
    its rotation angle from a Scrollbar; this allows for a nice
    annimation if you move the Scrollbar when the program is running.
*/
import java.awt.*;
import java.applet.Applet;
import java.awt.event.*;

public class RotateSquareClass5 extends Applet implements AdjustmentListener {

    Scrollbar rotation;
    Square sq1, sq2, sq3;
    Point p1, p2 ,p3, p4, c;
    int side = 195;         //the size of the square
    double shrink;
    double angle = .06*180/Math.PI;  //this is in degrees
    
    public void init () {
        rotation = new Scrollbar(Scrollbar.HORIZONTAL, 60, 1, 0, 100);
        add(rotation);
        rotation.addAdjustmentListener(this);
    }

    public void paint (Graphics g) {
        c = new Point(200, 200);             //create the center point
        p1 = new Point(200-side, 200-side);  //create the four corner points
        p2 = new Point(200+side, 200-side);
        p3 = new Point(200+side, 200+side);
        p4 = new Point(200-side, 200+side);
        sq1 = new Square(p1, p2, p3, p4);    //use the four points to create a square  
        sq1.display(g);                      //draw the initial square
        for (int i = 0; i < 40; i++) {
            sq1 = sq1.rotateAbout(c, angle);    //rotate a square
            shrink = 1/(Math.cos(angle*Math.PI/180) + Math.sin(angle*Math.PI/180));
            sq1 = sq1.shrinkAbout(c, shrink);   //shrink the rotated square
            sq1.display(g);                     //draw the rotated and shrunk square
        }
    }
    
    public void adjustmentValueChanged(AdjustmentEvent event) {
        angle = rotation.getValue();
        angle = (angle/1000.0)*180/Math.PI;  //this is in degrees
        repaint();
    }
}