package com.mindprod.example;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

/**
 * Rotating drawings with Affine Transforms. Run this program and follow the logic. You may be surprised.
 * <p/>
 * composed with IntelliJ IDEA
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0, 2007-06-03
 */
public final class TestRotate extends JFrame
    {
    // -------------------------- PUBLIC INSTANCE  METHODS --------------------------
    /**
     * Paints this component using the given graphics context.
     *
     * @param g Graphics context where to paint, e.g. to screen, printer, RAM.
     */
    public void paint( Graphics g )
        {
        AffineTransform saveTransform;
        final Graphics2D g2 = ( Graphics2D ) g;

        g.setColor( Color.WHITE );
        g.clearRect( 0, 0, this.getWidth(), this.getHeight() );

        // draw outer rectangle
        g2.setColor( Color.BLACK );
        g2.drawRect( 0/* x */, 0/* y */, 100/* width */, 200/* height */ );

        // draw tiny red box around origin
        g2.setColor( Color.RED );
        g2.drawRect( -4/* x */, -4/* y */, 10/* width */, 10/* height */ );
        // save existing transform.
        saveTransform = g2.getTransform();

        final AffineTransform transform = new AffineTransform();
        // move origin right and down.
        transform.translate( 300, 50 );

        g2.setTransform( transform );
        g2.setColor( Color.BLUE );
        g2.drawRect( 0/* x */, 0/* y */, 100/* width */, 200/* height */ );

        // draw tiny red box around origin
        g2.setColor( Color.RED );
        g2.drawRect( -4/* x */, -4/* y */, 10/* width */, 10/* height */ );

        transform.rotate( Math.toRadians( 90 ) );
        g2.setTransform( transform );

        g2.setColor( Color.GREEN );

        g2.drawRect( 0/* x */, 0/* y */, 100/* width */, 200/* height */ );

        // draw tiny red box around origin
        g2.setColor( Color.RED );
        g2.drawRect( -4/* x */, -4/* y */, 10/* width */, 10/* height */ );

        transform.translate( 0, -30 );
        g2.setTransform( transform );

        g2.setColor( Color.YELLOW );

        g2.drawRect( 0/* x */, 0/* y */, 100/* width */, 200/* height */ );

        // draw tiny red box around origin
        g2.setColor( Color.RED );
        g2.drawRect( -4/* x */, -4/* y */, 10/* width */, 10/* height */ );

        // restore usual transform
        g2.setTransform( saveTransform );
        }

    // --------------------------- main() method ---------------------------

    /**
     * Debugging harness for a Frame
     *
     * @param args command line arguments are ignored.
     */
    public static void main( String args[] )
        {
        SwingUtilities.invokeLater( new Runnable()
        {
        /**
         * fire up a JFrame on the Swing thread
         */
        public void run()
            {
            final TestRotate frame = new TestRotate();

            frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
            frame.setSize( 600, 500 );
            frame.setVisible( true );
            }
        } );
        }
    }