package visual.dynamic.sampled;

import java.awt.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;

import visual.statik.TransformableContent;

/**
 * A piece of (transformable) static visual content that is 
 * superimposed on a frame
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class   TransformableContentSuperimposition 
       extends AbstractSuperimposition
{
    private boolean                done;    
    private TransformableContent   content;

//[constructor.
    
    /**
     * Explicit Value Constructor
     *
     * @param content     The visual content to use
     * @param first       The first frame
     * @param duration    The duration (in frames)
     * @param position    The position (SwingConstants.NORTH, ...)
     */
    public TransformableContentSuperimposition(
                               TransformableContent content, 
                               int first, int duration, 
                               int position)
    {
       super(first, duration, position);
       
       this.content = content;
    }
//]constructor.    

    
    /**
     * Apply the post-rendering portion of
     * this Superimposition
     *
     * @param g      The rendering engine
     * @param frame  The current frame number
     */
    public void postRendering(Graphics g, int frame)
    {
       double               frameHeight, frameWidth;
       double               contentHeight, contentWidth;
       Graphics2D           g2;       
       Rectangle            frameBounds;
       Rectangle2D          contentBounds;
       Point2D              rp;
       
       
       if (shouldApplyAt(frame))
       {
//[postRendering.

          g2 = (Graphics2D)g;       
          
          // Transform the TransformableContent so that 
          // it is positioned properly
          frameBounds   = g2.getClipBounds();
          frameWidth    = (double)frameBounds.width;
          frameHeight   = (double)frameBounds.height;
          
          contentBounds = content.getBounds2D(false);
          contentWidth  = contentBounds.getWidth();
          contentHeight = contentBounds.getHeight();
          
          
          rp = calculateRegistrationPoint(frameWidth, 
                                          frameHeight,
                                          contentWidth, 
                                          contentHeight);
          
          content.setLocation(rp.getX(), rp.getY());
          content.render(g);
//]postRendering.
       }

       if (hasFinishedAt(frame)) done = true;
    }



    /**
     * Apply the pre-rendering portion of
     * this Superimposition
     *
     * @param g      The rendering engine
     * @param frame  The current frame number
     */
    public void preRendering(Graphics g, int frame)
    {
    }
       
}
