package opartgui;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.image.BufferedImage;
import com.sun.image.codec.jpeg.*;


public class AnimPanel extends JPanel implements ActionListener{
   protected BufferedImage displayImage;
   protected Graphics2D dispGc;

    int frameNumber = 0;
    int NUM_FRAMES = 21;
    int delay = 50;
    int location = 2;
    String locations[] = {"animation1/","animation2/","animation3/","animation4/","customanimation/"};
    Timer timer;
    boolean frozen = false;
    ImageIcon[] images = new ImageIcon[NUM_FRAMES];
    JLabel picture;

//    BufferedImage[] animate = new BufferedImage [21];

   int w = 600;
   int h = 600;

   public AnimPanel(){
       setBackground(Color.white);
       //Create the label that displays the animation.
        picture = new JLabel();
        picture.setHorizontalAlignment(JLabel.CENTER);
        picture.setAlignmentX(Component.CENTER_ALIGNMENT);
        //updatePicture(0); //display first frame
        add(picture);
        
        //Set up a timer that calls this object's action handler.
        timer = new Timer(delay, this);
//        timer.setInitialDelay(delay); //We pause animation twice per cycle
                                          //by restarting the timer
        timer.setCoalesce(true);
   }

   public void doDisplay(BufferedImage bi){
       displayImage = bi;
       repaint();
   }

   protected void createDisplayImage(){
        Dimension dim = getSize();
        w = dim.width;
        h = dim.height;
        displayImage = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
        setOffScrGc();
   }

   protected void setOffScrGc(){
        if (displayImage != null){
             dispGc = displayImage.createGraphics();
             dispGc.setColor(getBackground());
             dispGc.fillRect(0,0,w,h);
             dispGc.setColor(getForeground());
             dispGc.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        }
   }

   public void paintComponent(Graphics gc){
       super.paintComponent(gc);
       Graphics2D g = (Graphics2D)gc;
       if (displayImage != null){
           g.drawImage(displayImage, 0 ,0, this);
       }
       else createDisplayImage();
   }



   public void animateStart() {
	timer.start();
        frozen = false;
        images = new ImageIcon[NUM_FRAMES];
   }

   public void animateStop() {
        //Stop the animating thread.
        timer.stop();
        frozen = true;
        images = new ImageIcon[NUM_FRAMES];
   }

   //Called when the Timer fires.
   public void actionPerformed(ActionEvent e) {
        //Advance the animation frame.
        if (frameNumber == (NUM_FRAMES - 1)) {
            frameNumber = 0;
        } else {
            frameNumber++;
        }

        updatePicture(frameNumber); //display the next picture

        if ( frameNumber==(NUM_FRAMES - 1)
          || frameNumber==(NUM_FRAMES/2 - 1) ) {
            timer.restart();
        }
    }

    /** Update the label to display the image for the current frame. */
    protected void updatePicture(int frameNum) {
        //Get the image if we haven't already.
        if (images[frameNumber] == null) {
            images[frameNumber] = createImageIcon(locations[location] +frameNumber
                                                  + ".jpg");
        }

        //Set the image.
        if (images[frameNumber] != null) {
            picture.setIcon(images[frameNumber]);
        } else { //image not found
            //picture.setText("image #" + frameNumber + " not found");
        }
    }

    /** Returns an ImageIcon, or null if the path was invalid. */
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = AnimPanel.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

}
