Source code for AffineTransformDemo.java

What follows is the highlighted source code with comments. You can also download this file directly: AffineTransformDemo.java.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package justdrawing;

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

/**
 *
 * @author pat
 */
public class AffineTransformDemo extends JPanel {

    /** Default constructor. */
    public AffineTransformDemo() {
        this.setBackground(Color.WHITE); // Make the background color white
    }

    /** This is called when we need to draw something. */
    @Override
    public void paintComponent(Graphics gfx) {
        // This stuff is standard:
        super.paintComponent(gfx); // Paint the component (such as the background).
        Graphics2D g = (Graphics2D) gfx; // Better to draw with a Graphics2D object.
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON); // Anti-aliasing looks pretty!

        // We are drawing to a rectangle representing the visible portion of the screen.
        // The upper left corner of the rectangle has coordinates (0,0).
        // The lower right corner is (getWidth(), getHeight()). 
        
        // Construct a rectangle with one vertex (0,0) with width getWidth()/2 and 
        // height getHeight()/2.
        Rectangle2D rect = new Rectangle2D.Double(0, 0, 1, 1);
        
        AffineTransform t=AffineTransform.getScaleInstance(2, 2);
          
        
        Shape s=rect;
        g.setColor(Color.RED);
        for (int i=0; i<10; i++) {
            g.draw(s);
            s=t.createTransformedShape(s);
        }
    }

    /** Create a window and put our panel on display inside it. */
    public static void main(String[] args) {
        JFrame frame = new JFrame("Drawing Demo");
        // Dimensions of the window in pixels:
        frame.setSize(640, 480);
        // Quit the program when the window is closed:
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // The window will contain only our panel:
        AffineTransformDemo dd=new AffineTransformDemo();
        frame.add(dd);
        // Make the window visible:
        frame.setVisible(true);
    }
}
HOOPER >>>>> JAVA TUTORIAL
Last modified on August 17, 2018.
[check html] [check css]