/*
 * [TestRotate.java]
 *
 * Summary: Rotating drawings with Affine Transforms. Run this program and follow the logic. You may be surprised.
 *
 * Copyright: (c) 2009-2017 Roedy Green, Canadian Mind Products, http://mindprod.com
 *
 * Licence: This software may be copied and used freely for any purpose but military.
 *          http://mindprod.com/contact/nonmil.html
 *
 * Requires: JDK 1.8+
 *
 * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/
 *
 * Version History:
 *  1.0 2007-06-03
 */
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.
 *
 * @author Roedy Green, Canadian Mind Products
 * @version 1.0 2007-06-03
 * @since 2007-06-03
 */
public final class TestRotate extends JFrame
    {
    /**
     * 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 );
                }
            } );
        }

    /**
     * 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 );
        }
    }