import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

public class RotatingPythagoras extends JPanel implements ActionListener {
    int n = 0;
    int MAX = 10;
    public RotatingPythagoras() {
        setBackground(Color.white);
    } 

    public void actionPerformed(ActionEvent event) {
	String actionCommand = event.getActionCommand();
	if (actionCommand.equals("Rotate") && n < MAX) {
	    n++;
	    repaint();
	}
	else if (actionCommand.equals("Reset")) {
	    n = 0;
	    repaint();
	}
    }

    public void paintComponent(Graphics gfx) {
	// usual stuff
        super.paintComponent(gfx);
        Graphics2D g = (Graphics2D) gfx;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                           RenderingHints.VALUE_ANTIALIAS_ON);

	g.translate(100, 200);
	double scale = 35;
	g.scale(scale, -scale);
	g.setStroke(new BasicStroke((float) (1/scale)));

	double angle = Math.atan2(4, 3);
	double topX = 3*Math.cos(angle);
	double topY = 3*Math.sin(angle);

	GeneralPath triangle = new GeneralPath();
	triangle.moveTo(0, 0);
	triangle.lineTo(0, -5);
	triangle.lineTo((float) topX, (float) topY);
	triangle.closePath();
	
	AffineTransform transform = new AffineTransform();
	transform.rotate(n*Math.PI/(2*MAX));
	Shape triShape = transform.createTransformedShape(triangle);
	g.setPaint(new Color(1f, 0.5f, 0.5f));
	g.fill(triShape);
	g.setPaint(Color.black);
	g.draw(triShape);
	

	AffineTransform at = new AffineTransform();
	at.rotate(-Math.PI/2);
	drawSquare(g, 5, at);

	at = new AffineTransform();
	at.rotate(angle);
	drawSquare(g, 3, at);

	at = new AffineTransform();
	at.translate(topX, topY);
	at.rotate(angle-Math.PI/2);
	drawSquare(g, 4, at);

	GeneralPath path = new GeneralPath();
	path.moveTo(0, 0);
	path.lineTo(5, 0);
	path.lineTo((float) topX, (float) topY);
	path.closePath();
	g.draw(path);

	g.draw(new Line2D.Double(topX, topY, topX, -5));
	g.draw(new Line2D.Double(topX, topY, 5, -5));
	g.draw(new Line2D.Double(-topY, topX, 5, 0));
	g.draw(new Line2D.Double(0, 0, 5+4*Math.cos(angle), 4*Math.sin(angle)));
    }

    public static void drawSquare(Graphics2D g, float side, AffineTransform at){
	GeneralPath path = new GeneralPath();
	path.moveTo(0, 0);
	path.lineTo(side, 0);
	path.lineTo(side, side);
	path.lineTo(0, side);
	path.closePath();
	Shape s = at.createTransformedShape(path);
	g.draw(s);
    }

    public static void main(String[] args) {
        RotatingPythagoras pythagoras = new RotatingPythagoras();
        pythagoras.setPreferredSize(new Dimension(400, 400));

	JPanel buttonPanel = new JPanel();
	JButton rotate = new JButton("Rotate");
	JButton reset = new JButton("Reset");
	buttonPanel.add(rotate);
	buttonPanel.add(reset);
	rotate.addActionListener(pythagoras);
	reset.addActionListener(pythagoras);

        JFrame frame = new JFrame("RotatingPythagoras");
	frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(pythagoras, BorderLayout.CENTER);
        frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        frame.pack();
        frame.setVisible(true);
    }
        
}
        
                                         
