import java.awt.*; 
import java.awt.geom.*;
import javax.swing.*;

public class TransformDemo {
    public static void main(String[] args) {
	EventQueue.invokeLater(new Runnable() {
		public void run() {
		    TransformDemo demo = new TransformDemo();
		}
	    });
    }

    public TransformDemo() {
	frame = new JFrame("Transformation Demo");
	DrawRegion drawRegion = new DrawRegion();
	frame.getContentPane().add(drawRegion);
	frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.setVisible(true);
    }

    private JFrame frame;
    private static final int WINDOW_WIDTH = 600;
    private static final int WINDOW_HEIGHT = 500;

    class DrawRegion extends JPanel {
	public Dimension getPreferredSize() {
	    return new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT); 
	}
	
	public void paintComponent(Graphics g) {
	    super.paintComponent(g);
	    setBackground(Color.WHITE);
	    Graphics2D g2 = (Graphics2D) g;

	    int rect_width = 50;
	    int rect_height = 30;
	    
	    Ellipse2D oval = new Ellipse2D.Float(0,
						 0,
						 rect_width,
						 rect_height); 
	    Rectangle2D rect = new Rectangle2D.Float(0,
						     0,
						     rect_width,
						     rect_height);

	    Ellipse2D center = new Ellipse2D.Float((WINDOW_WIDTH - 5)/2,
						   (WINDOW_HEIGHT - 5)/2,
						   10,
						   10); 
	    
	    g2.setPaint(Color.ORANGE);
	    g2.fill(center);
	    
	    
	    g2.setPaint(Color.RED);
	    g2.draw(rect);
	    g2.fill(oval);
		    
	    // save the current transform
	    AffineTransform savedTransform = g2.getTransform(); 

	    // change the transformation to move the ellipse
	    g2.translate((WINDOW_WIDTH - rect_width)/2, (WINDOW_HEIGHT - rect_height)/2);
	    g2.rotate(Math.toRadians(45));
	    g2.scale(2,2);
	    g2.setPaint(Color.BLUE);
	    g2.draw(rect);
	    g2.fill(oval);

	    // reset
	    g2.setTransform(savedTransform);

	    // change the transformation to move the rectangle
	    g2.translate(350,150);
	    g2.rotate(Math.toRadians(-45));
	    g2.scale(1.5,1);
	    g2.setPaint(Color.GREEN);
	    g2.draw(rect);
	    g2.fill(oval);

	    // reset
	    g2.setTransform(savedTransform);
	}
    }
}
