package visual.dynamic.sampled;

import java.awt.*;
import java.awt.geom.*;

/**
 * A wipe that consists of a rectangle that grows
 * from the center
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class RectangleWipe extends AbstractTransition
{
    private   AffineTransform    at;
    protected float              scale;
    private   Shape              originalClip;
    

    /**
     * Explicit Value Constructor
     *
     * @param first     The first frame
     * @param duration  The duration (in frames)
     */
    public RectangleWipe(int first, int duration)
    {
       super(first, duration);
       scale = 1.0f/(float)duration;
    }


    /**
     * Apply the pre-rendering portion of
     * this Transition
     *
     * @param g      The rendering engine
     * @param frame  The current frame number
     */
    public void preRendering(Graphics g, int frame)
    {
       boolean            done;
       float              height, width;
       Graphics2D         g2;
       Rectangle          bounds;
       Rectangle2D        clip;


       if (shouldApplyAt(frame))
       {
//[preRendering.

          g2 = (Graphics2D)g;
          originalClip = g2.getClip();        
           
          bounds = g2.getClipBounds();
          height = (float)(bounds.getHeight());
          width  = (float)(bounds.getWidth());
           
           
          clip = calculateClip(width, height, frame-first+1);
          g2.setClip(clip);
//]preRendering.
       }
        
       if (hasFinishedAt(frame)) done=true;
    }


    /**
     * Apply the post-rendering portion of
     * this Transition
     *
     * @param g      The rendering engine
     * @param frame  The current frame number
     */
    public void postRendering(Graphics g, int frame)
    {
       Graphics2D        g2;
       
//[postRendering.

       g2 = (Graphics2D)g;

       if (originalClip != null) g2.setClip(originalClip);       
//]postRendering.
    }
    
//[calculateClip.

    /**
     * Calculate the size of the clip rectangle
     *
     * @param width   The width  of the image
     * @param height  The height of the image
     * @param int     The frame of the wipe
     */
    protected Rectangle2D calculateClip(float width,
					float height,
                                        int   frame)
    {
       float              h, w, x, y;
       Rectangle2D        clip;


       w = scale*frame*width;
       h = scale*frame*height;
       x = width/2.0f  - w/2.0f;
       y = height/2.0f - h/2.0f;

       clip = new Rectangle2D.Float(x, y, w, h);

       return clip;
    }
//]calculateClip.
}
