// Uso de Graphics2D

import java.awt.*;
import java.awt.event.*;

public class JanelaDesenho2 extends Frame {
    private final int startPos = 50;
    private int incremento = 15;
    private int tamanho = 40;

    class WindowHandler extends WindowAdapter {
	public void windowClosing(WindowEvent we) {
	    dispose();
	    System.exit(0);
	}
    }

    public JanelaDesenho2() {
	setTitle("Janela e desenho 2");
	setSize(320,200);
	addWindowListener(new WindowHandler());
    }

    public void paint(Graphics g) {
	int x=startPos;
	int y=startPos;
	
	Dimension d = getSize();
	int finalPos = Math.min(d.width, d.height)-tamanho;

	Graphics2D g2 = (Graphics2D) g;
	
	g2.setStroke(new 
	    BasicStroke(10.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

	g2.drawString("Desenhando...", 10, 40);

	while (x < finalPos) {
	    g2.setColor(new Color((float)Math.random(),
				 (float)Math.random(), 
				 (float)Math.random()));
	    g2.drawRect(x, y, tamanho, tamanho);
	    x += incremento;
	    y += incremento;
	}
	g2.setFont(g.getFont().deriveFont(Font.BOLD));
	g2.setColor(Color.red);
	g2.drawString("...pronto!", 100, 40);
    }

    public static void main(String[] args) {
	JanelaDesenho2 j = new JanelaDesenho2();
	j.setVisible(true);
    }
}