public void stateChanged(ChangeEvent e) {
   if (e.getSource().equals(sldIterations)) {
     txtIterations.setText(Integer.toString((int) sldIterations.getValue()));
     if (preview) {
       parent.setPreviewMap(parent.getHeightMap(), 128);
       parent.refreshMiniView(apply(preview));
     }
   }
 }
  private int[][] apply(boolean preview) {
    int[][] heightmap = new int[128][128];
    int step = 1;

    if (!preview) {
      parent.setProgressBar(sldIterations.getValue());
      heightmap = parent.cloneArray(parent.getHeightMap());
    } else {
      heightmap = parent.getPreviewMap();
      step =
          (int)
              Math.ceil(
                  (double) parent.getHeightMap().length / (double) parent.getPreviewMap().length);
    }

    for (int i = 0; i < sldIterations.getValue() / step; i++) {
      heightmap = applyThermal(heightmap);

      if (!preview) parent.increaseProgressBar();
    }

    if (!preview) parent.resetProgressBar();

    return heightmap;
  }
  /**
   * Sets the panel in which the GUI should be implemented Any standard swing components can be used
   *
   * @param panel The GUI panel
   */
  public void setPanel(JPanel panel) {
    this.panel = panel;

    panel.setLayout(new BorderLayout());

    JLabel label =
        new JLabel("Produces an effect similar to thermal erosion", (int) JLabel.CENTER_ALIGNMENT);
    panel.add(label, BorderLayout.PAGE_START);

    JPanel iterations = new JPanel();
    iterations.setLayout(new FlowLayout());

    label = new JLabel("Iterations ");
    iterations.add(label);
    sldIterations = new JSlider(1, 100, 1);
    sldIterations.addChangeListener(this);
    iterations.add(sldIterations);
    txtIterations = new JTextField(Integer.toString((int) sldIterations.getValue()));
    txtIterations.setEditable(false);
    txtIterations.setPreferredSize(new Dimension(35, 25));
    iterations.add(txtIterations);

    panel.add(iterations, BorderLayout.CENTER);

    btnApply = new JButton("Apply Thermal Erosion");
    btnApply.addActionListener(this);
    panel.add(btnApply, BorderLayout.PAGE_END);

    if (preview) parent.refreshMiniView(apply(preview));
  }
 /**
  * Called when an action event occurs, in this case the 'Invert Terrain' button is the only
  * component which generates an action event
  *
  * @param e The action event generated
  */
 public void actionPerformed(ActionEvent e) {
   if (e.getSource().equals(btnApply)) {
     parent.amendLog("Applying filter: " + toString());
     parent.setHeightMap(apply(false));
   }
 }