import java.awt.*;
import java.awt.geom.*;
import java.util.Random;
import javax.swing.*;

/**
 * A JComponent that renders a Rectangle with randomly
 * generated attributes each time it's paint method is called
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class RandomRectangleComponent extends JComponent
{
    private Random      generator;

    
    /**
     * Default Constructor
     */
    public RandomRectangleComponent()
    {
       super();
       generator = new Random(System.currentTimeMillis());       
    }
    
    /**
     * Paint this Component
     *
     * @param g   The rendering engine to use
     */
    public void paint(Graphics g)
    {
       Graphics2D      g2;       
       int             height, maxHeight, maxWidth, width, x, y;
       Rectangle       rectangle;
        

       g2 = (Graphics2D)g;

       maxHeight = getHeight();
       maxWidth  = getWidth();

       x      = generator.nextInt(maxWidth  - 1);
       y      = generator.nextInt(maxHeight - 1);
       width  = generator.nextInt(maxWidth  - x - 1);
       height = generator.nextInt(maxHeight - y - 1);

       rectangle = new Rectangle(x, y, width, height);
       
       g2.draw(rectangle);       
    }
}
