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() );
g2.setColor( Color.BLACK );
g2.drawRect( 0, 0, 100, 200);
g2.setColor( Color.RED );
g2.drawRect( -4, -4, 10, 10);
saveTransform = g2.getTransform();
final AffineTransform transform = new AffineTransform();
transform.translate( 300, 50 );
g2.setTransform( transform );
g2.setColor( Color.BLUE );
g2.drawRect( 0, 0, 100, 200);
g2.setColor( Color.RED );
g2.drawRect( -4, -4, 10, 10);
transform.rotate( Math.toRadians( 90 ) );
g2.setTransform( transform );
g2.setColor( Color.GREEN );
g2.drawRect( 0, 0, 100, 200);
g2.setColor( Color.RED );
g2.drawRect( -4, -4, 10, 10);
transform.translate( 0, -30 );
g2.setTransform( transform );
g2.setColor( Color.YELLOW );
g2.drawRect( 0, 0, 100, 200);
g2.setColor( Color.RED );
g2.drawRect( -4, -4, 10, 10);
g2.setTransform( saveTransform );
}
}