import java.applet.Applet;
import java.awt.*;
import java.awt.image.PixelGrabber;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import Coordinate;
import MetaPanel;
import AnimCanvas;

public class SommerCampWrap extends Applet implements ActionListener
{
  private Choice nox, noy;
  private TextField noimg;
  private Checkbox SaveIt;
  private MetaPanel sPanel, tPanel, aPanel;
  private Image sImage, tImage;
  private int[] sPixels, tPixels;
  private boolean InAnApplet = false;
  private Label status;

  public void init()
  {
    String sName, tName;

    // Parameter einlesen
    sName = getParameter("sourceimage");
    tName = getParameter("targetimage");
    InAnApplet = true;
    if (sName==null)
      {
	showStatus("Kein Quell-Bild angegeben ...");
	return;
      }
    init(null, sName, tName);
  }

  public void init(Label status, String sName, String tName)
  {
    Panel panel;
    Button button;
    MediaTracker tracker;
    PixelGrabber grabber;
    int w, h, tw, th, i;

    // Bilder laden
    tImage = null;
    tPixels = null;
    this.status = status;
    showStatus("Bild(er) werden geladen ...");
    tracker = new MediaTracker(this);
    if (InAnApplet)
      sImage = getImage(getCodeBase(), sName);
    else
      sImage = Toolkit.getDefaultToolkit().getImage(sName);
    tracker.addImage(sImage, 0);
    // Zielbild nur laden wenn angegeben
    if (tName!=null)
      {
	if (InAnApplet)
	  tImage = getImage(getCodeBase(), tName);
	else
	  tImage = Toolkit.getDefaultToolkit().getImage(tName);
	tracker.addImage(tImage, 1);
      }
    try { tracker.waitForAll(); } catch (InterruptedException e)
      { showStatus("SommerCampWrap abgebrochen ..."); return; }
    // Bildgroesse holen und u.U. vergleichen.
    w = tw = sImage.getWidth(this);
    h = th = sImage.getHeight(this);
    if ((w<0) || (h<0))
      {
	showStatus("Quell-Bild konnte nicht geladen werden");
	return;
      }
    if (tName!=null)
      {
	tw = tImage.getWidth(this);
	th = tImage.getHeight(this);
	if ((tw<0) || (th<0))
	  {
	    showStatus("Ziel-Bild konnte nicht geladen werden");
	    return;
	  }
      }
    if ( (h!=th) || (w!=tw) )
      {
	showStatus("Quell- und Ziel-Bild sind nicht gleich groß");
	return;
      }

    // Pixelwerte holen
    sPixels = new int[w*h];
    grabber = new PixelGrabber( sImage, 0, 0, w, h, sPixels, 0, w);
    try { grabber.grabPixels(); } catch ( InterruptedException e )
      { showStatus("SommerCampWrap abgebrochen ..."); return; }

    if (tName!=null)
      {
	tPixels = new int[w*h];
	grabber = new PixelGrabber( tImage, 0, 0, w, h, tPixels, 0, w);
	try { grabber.grabPixels(); } catch ( InterruptedException e )
	  { showStatus("SommerCampWrap abgebrochen ..."); return; }
      }

    showStatus("Bild(er) laden beendet");

    // Benutzeroberfläche definieren
    setLayout(new BorderLayout());
    nox = new Choice();
    noy = new Choice();
    for (i=3; i<11; i++)
      {
	nox.add(Integer.toString(i));
	noy.add(Integer.toString(i));
      }
    nox.select("4");
    noy.select("6");
    panel = new Panel();
    panel.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 5));
    panel.add(new Label("Linienzahl:"));
    panel.add(new Label("Senkrecht"));
    panel.add(nox);
    panel.add(new Label("   Waagrecht"));
    panel.add(noy);
    panel.add(new Label("    "));
    panel.add(button = new Button("Neues Gitter"));
    button.addActionListener(this);
    add("North", panel);
    panel = new Panel();
    panel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 0));
    panel.add(sPanel = new MetaPanel("Quelle", w, h, sImage,
				     Color.red, InAnApplet, this));
    panel.add(tPanel = new MetaPanel("Ziel", w, h, tImage,
				     Color.blue, InAnApplet, this));
    panel.add(aPanel = new MetaPanel("Animation", w, h, this));
    add("Center", panel);
    panel = new Panel();
    panel.setLayout(new FlowLayout());
    panel.add(new Label("Anzahl Bilder"));
    panel.add(noimg = new TextField("5", 2));
    if (!InAnApplet)
      panel.add(SaveIt = new Checkbox("Bilder Sichern", false));
    panel.add(button = new Button("Starte Animation"));
    button.addActionListener(this);
    panel.add(button = new Button("Stoppe Animation"));
    button.addActionListener(this);
    add("South", panel);
  }

  public void showStatus(String s)
  {
    if (InAnApplet)
      super.showStatus(s);
    else
      status.setText("  " + s);
  }

  public void stop()
  {
    aPanel.StopThread();
  }


  // Button-Funktionalität
  public void actionPerformed(ActionEvent e)
  {
    if (e.getSource() instanceof Button)
      {
	String label = ((Button)(e.getSource())).getLabel();

	if (label.compareTo("Neues Gitter") == 0)
	  {
	    // Neues Gitter initialisieren gemäß den Chioce-Angaben
	    int x, y;

	    try { x = Integer.valueOf(nox.getSelectedItem()).intValue(); }
	    catch (java.lang.NumberFormatException ne) { x = 3; }
	    try { y = Integer.valueOf(noy.getSelectedItem()).intValue(); }
	    catch (java.lang.NumberFormatException ne) { y = 3; }
	    sPanel.NewCoordinate(x, y);
	    tPanel.NewCoordinate(x, y);
	  }
	if (label.compareTo("Starte Animation") == 0)
	  {
	    // Animation starten, falls Gitter existiert
	    Coordinate[][] sCoord, tCoord;
	    boolean doSave;
	    int number;

	    stop();
	    sCoord = sPanel.GetCoordinate();
	    tCoord = tPanel.GetCoordinate();
	    if (sCoord!=null)
	      {
		if (SaveIt==null)
		  doSave = false;
		else
		  doSave = SaveIt.getState();
		try { number = Integer.valueOf(noimg.getText()).intValue(); }
		catch (java.lang.NumberFormatException ne) { number = 2; }
		aPanel.StartThread(sPixels, tPixels, sCoord,
				   tCoord, number, doSave);
	      }
	  }
	if (label.compareTo("Stoppe Animation") == 0)
	  {
	    stop();
	  }
      }
  }

  public String getAppletInfo()
  {
    return "SommerCampWrap by Alexander Zimmermann";
  }

  public String[][] getParameterInfo()
  {
    String[][] info = {
      {"sourceimage", "String", "filename of sourceimage for wrapping"},
      {"targetimage", "String", "optional filename of targetimage"} };
    return info;
  }
}
