//package components;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/*
 * SliderDemo.java stolen from sun's demo examples
 */
public class SliderDemo extends JPanel implements ActionListener, WindowListener, ChangeListener 
{
  //Set up animation parameters.
  static final int FPS_MIN = 0;
  static final int FPS_MAX = 100;    // equivalent to 20 seconds of wait time
  static final int FPS_INIT = 50;    //initial value
  int frameNumber = 0;
  int NUM_FRAMES = 100;
  int delay;
  Timer timer;
  boolean frozen = false;

  //This label uses ImageIcon to show the doggy pictures.
  JLabel picture;

  //Called when the Timer fires.
  public void actionPerformed(ActionEvent e) {
    //Advance the animation frame.
    if (frameNumber == 99) {
      stopAnimation(); // that's it! // stop the animation
    } else {
      frameNumber++;
    }
    String command = e.getActionCommand();
    if (command != null)
    {
      System.out.println("command is " + e.getActionCommand());
      if (command.equals("quit"))
      {
      }
      if (command.equals("quitall"))
      {
      }
      if (command.equals("play"))
      {
        frameNumber = 0;
        startAnimation();
      }
    }

    updatePicture(frameNumber); //display the next picture
  }

  public SliderDemo() {
    setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

    delay = 10000 / FPS_INIT;

    JPanel buttonPanel = new JPanel( new FlowLayout() );

    /*
     * buttons
     */         
    JButton b1 = new JButton("Play!");
    b1.setVerticalTextPosition(AbstractButton.CENTER);
    b1.setHorizontalTextPosition(AbstractButton.CENTER); //aka LEFT, for left-to-right locales
    b1.setActionCommand("play");
    b1.addActionListener(this);
    JButton b2 = new JButton("Quit");
    b2.setVerticalTextPosition(AbstractButton.CENTER);
    b2.setHorizontalTextPosition(AbstractButton.CENTER); //aka LEFT, for left-to-right locales
    b2.setActionCommand("quit");
    b2.addActionListener(this);
    JButton b3 = new JButton("Quit All");
    b3.setVerticalTextPosition(AbstractButton.CENTER);
    b3.setHorizontalTextPosition(AbstractButton.CENTER); //aka LEFT, for left-to-right locales
    b3.setActionCommand("quitall");
    b3.addActionListener(this);

    buttonPanel.add( b1 );
    buttonPanel.add( b2 );
    buttonPanel.add( b3 );

    //Create the label.
    JLabel sliderLabel = new JLabel("Difficulty", JLabel.CENTER);
    sliderLabel.setAlignmentX(Component.CENTER_ALIGNMENT);

    //Create the slider.
    JSlider framesPerSecond = new JSlider(JSlider.HORIZONTAL,
        FPS_MIN, FPS_MAX, FPS_INIT);


    framesPerSecond.addChangeListener(this);

    //Turn on labels at major tick marks.

    framesPerSecond.setMajorTickSpacing(25);
    framesPerSecond.setMinorTickSpacing(5);
    framesPerSecond.setPaintTicks(true);
    framesPerSecond.setPaintLabels(true);
    framesPerSecond.setBorder(BorderFactory.createEmptyBorder(0,0,10,0));
    Font font = new Font("Serif", Font.ITALIC, 15);
    framesPerSecond.setFont(font);

    //Create the label that displays the animation.
    picture = new JLabel();
    picture.setHorizontalAlignment(JLabel.CENTER);
    picture.setAlignmentX(Component.CENTER_ALIGNMENT);
    picture.setBorder(BorderFactory.createCompoundBorder(
          BorderFactory.createLoweredBevelBorder(),
          BorderFactory.createEmptyBorder(10,10,10,10)));
    updatePicture(0); //display first frame

    //Put everything together.
    add( buttonPanel );
    //add(b1); add(b2);
    add(sliderLabel);
    add(framesPerSecond);
    add(picture);
    setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

    //Set up a timer that calls this object's action handler.
    timer = new Timer(delay, this);
    timer.setInitialDelay(delay * 7); //We pause animation twice per cycle
    //by restarting the timer
    timer.setCoalesce(true);
  }

  /** Add a listener for window events. */
  void addWindowListener(Window w) {
    w.addWindowListener(this);
  }

  //React to window events.
  public void windowIconified(WindowEvent e) {
    stopAnimation();
  }
  public void windowDeiconified(WindowEvent e) {
    startAnimation();
  }
  public void windowOpened(WindowEvent e) {}
  public void windowClosing(WindowEvent e) {}
  public void windowClosed(WindowEvent e) {}
  public void windowActivated(WindowEvent e) {}
  public void windowDeactivated(WindowEvent e) {}

  /** Listen to the slider. */
  public void stateChanged(ChangeEvent e) {
    JSlider source = (JSlider)e.getSource();
    if (!source.getValueIsAdjusting()) {
      int fps = (int)source.getValue();
      if (fps == 0) {
        if (!frozen) stopAnimation();
      } else {
        delay = 10000 / fps;
        timer.setDelay(delay);
        timer.setInitialDelay(delay * 10);
        if (frozen) startAnimation();
      }
    }
  }

  public void startAnimation() {
    //Start (or restart) animating!
    timer.start();
    frozen = false;
  }

  public void stopAnimation() {
    //Stop the animating thread.
    timer.stop();
    frozen = true;
  }

  /** Update the label to display the image for the current frame. */
  protected void updatePicture(int frameNum) {
    //Set the image.
    int L = (NUM_FRAMES-frameNumber-1);
    String label = "" + L;
    if (L == 0) {
      // make connection & get results
      label = label + " sets found.";
    }
    else if (L == 1) label = label + " tick left!";
    else label = label + " ticks left...";
    picture.setText(label);
  }

  /**
   * Create the GUI and show it.  For thread safety,
   * this method should be invoked from the
   * event-dispatching thread.
   */
  private static void createAndShowGUI() {
    //Create and set up the window.
    JFrame frame = new JFrame("Set Challenge");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    SliderDemo animator = new SliderDemo();

    //Add content to the window.
    frame.add(animator, BorderLayout.CENTER);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
    animator.startAnimation(); 
  }

  public static void main(String[] args) {
    /* Turn off metal's use of bold fonts */
    UIManager.put("swing.boldMetal", Boolean.FALSE);


    //Schedule a job for the event-dispatching thread:
    //creating and showing this application's GUI.
    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
        createAndShowGUI();
        }
        });
  }
}

