/** Handles slider input */
 @Override
 public void stateChanged(ChangeEvent arg0) {
   if (arg0.getSource() == direction) {
     if (direction.getValue() == 0) previewBox.setDirection(-1);
     else previewBox.setDirection(direction.getValue());
   }
   if (arg0.getSource() == walkCycle) {
     if (walkCycle.getValue() == 0) previewBox.setStep(-1);
     else previewBox.setStep(walkCycle.getValue());
   }
 }
 public void refreshList() {
   layerList.setListData(layers);
   layerListPane.setViewportView(layerList);
   previewBox.update();
   repaint();
 }
  /** Handles most input */
  @Override
  public void actionPerformed(ActionEvent event) {
    Object source = event.getSource(); // source of the action

    // start creating a new sprite
    if (source == newButton) {
      layers.removeAllElements();
      refreshList();
    } else if (source == savePNGButton) {
      BufferedImage b = previewBox.save();
      try {
        // retrieve image
        BufferedImage bi = previewBox.save();
        File outputfile = new File("data/actors/npcs/" + nameField.getText() + ".png");
        outputfile.createNewFile();
        ImageIO.write(bi, "png", outputfile);
      } catch (Exception e) {
        e.printStackTrace();
        JOptionPane.showMessageDialog(
            this,
            "The image was not able to save.\n"
                + "Possible reasons for this could be \n"
                + "1) The filesystem is read-only\n"
                + "2) There is already a file with that name in the npcs folder",
            "An error has occurred",
            JOptionPane.ERROR_MESSAGE);
      }
    }
    // add a new layer
    if (source == AddButton) {
      new SpriteLayerDialog(this);
    }
    // edit the currently selected layer
    else if (source == EdtButton) {
      int index = layerList.getSelectedIndex();
      if (index == -1) new SpriteLayerDialog(this);
      else new SpriteLayerDialog(this, index);
    }
    // removes the currently selected layer
    else if (source == RemButton) {
      layers.remove(layerList.getSelectedIndex());
      refreshList();
    }
    // shift the currently selected layer up one
    else if (source == SftUpButton) {
      int i = layerList.getSelectedIndex();
      if (i > 0) {
        layers.add(i - 1, layers.remove(i));
        refreshList();
        layerList.setSelectedIndex(i - 1);
      }
    }
    // shift the currently selected layer down one
    else if (source == SftDnButton) {
      int i = layerList.getSelectedIndex();
      if (i < layers.size() - 1) {
        layers.add(i + 1, layers.remove(i));
        refreshList();
        layerList.setSelectedIndex(i + 1);
      }
    }
  }