import java.awt.*;
import javax.media.*;


/**
 * An RGBRenderer that "pops-up" text
 *
 * This particular implementation can only be used with RGB
 * .mov files
 *
 * @version 1.0
 * @author  Prof. David Bernstein, James Madison University
 */
public class PopUpRenderer extends RGBRenderer 
{
    protected int       index, frame;
    protected int[]     duration, frames;
    protected String[]  text;

    /**
     * Constructor
     *
     * @param frames     Array of frame numbers for popups
     * @param text       Array of text for popups
     * @param duration   Array of durations (in frames) for popups
     */
    public PopUpRenderer(int[] frames, 
                         String[] text, 
                         int[] duration)
    {
	this.frames   = frames;
	this.text     = text;
	this.duration = duration;

	frame = -1;
	index =  0;
    }
    


    /**
     * Paint the current image
     *
     * @param g   The Graphics context
     */
    public void paintImage(Graphics g)
    {
	// This should be buffered to avoid flicker

	super.paintImage(g);

	if (index < frames.length) {
		
	    if ((frame >= frames[index]) && 
		(frame <= frames[index]+duration[index])) {
		
		
		g.setColor(Color.white);
		g.fillRect(50,50,150,25);
		g.setColor(Color.black);
		g.drawString(text[index], 70, 70);
		
		if (frame == 
	            frames[index]+duration[index]) index++;
	    }
	}
    }



    

    /**
     * Process the input data and render it
     *
     * @param buffer  The input data
     * @return        BUFFER_PROCESSED_OK if successful
     */
    public synchronized int process(Buffer buffer) 
    {
	frame++;

	return super.process(buffer);
    }


}
