Source code for

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

 * 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. */
    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.
                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;
        for (int i=0; i<10; i++) {

    /** 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:
        // The window will contain only our panel:
        AffineTransformDemo dd=new AffineTransformDemo();
        // Make the window visible:
Last modified on August 25, 2021.
[check html] [check css]