import java.applet.*;
import java.awt.*;
import java.awt.image.*;
import java.lang.Double;

public class DiceLoader extends Applet {
  protected Button button;
  protected Image diePic[] = new Image[6];
  protected String dieName[] = { "dicepics/pow!.gif", 
				 "dicepics/pow.gif", 
				 "dicepics/pushback.gif",
				 "dicepics/skull-pow.gif",
				 "dicepics/skull.gif", 
				 "dicepics/pushback.gif" };
  protected int die[] = new int[3];
  protected MediaTracker tracker;

  GridBagLayout gridbag = new GridBagLayout();

  // Get some panels
  Panel buttonpanel, imagepanel;
  ImageCanvas dicecanvas;

  Image workImage;
  Graphics workGraphics;


  public DiceLoader() {
    super();
  }

  public void init() {

    workImage = createImage(73 * 3, 70);
    workGraphics = workImage.getGraphics();
    workGraphics.setPaintMode();

    this.showStatus("Please wait...loading images...");
    tracker = new MediaTracker(this);

    for (int i=0; i<5; i++) {
      diePic[i] = getImage(this.getDocumentBase(), dieName[i]);
      if (null == diePic[i])
        System.out.println("Unable to load image " + dieName[i]);
      tracker.addImage(diePic[i], i);
    }
    for (int i=0; i<5; i++) {
      try { tracker.waitForID(i); }
      catch (InterruptedException e) { };
    }
    diePic[5] = diePic[2];
    this.showStatus("Images loaded");

    newDice();

    Panel buttonpanel = new Panel();
    button = new Button("Reload Dice");
    buttonpanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
    buttonpanel.add(button);

    Panel imagepanel = new Panel();
    dicecanvas = new ImageCanvas(workImage, this, 73*3, 70);
    imagepanel.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
    imagepanel.add(dicecanvas);
			 
    this.setLayout(gridbag);
    constrain(this, buttonpanel, 0, 1, 1, 1, GridBagConstraints.BOTH,
    	      GridBagConstraints.CENTER, 1.0, 1.0, 0, 0, 0, 0);
    constrain(this, imagepanel, 0, 0, 1, 1, GridBagConstraints.BOTH,
	      GridBagConstraints.CENTER, 1.0, 1.0, 0, 0, 0, 0);
    validate();

  }

  private void newDice() {
    for (int i=0; i<3; i++) {
      double d = Math.random();
      die[i] = (int)( d * 5 );
      //      die[i] = die[i] % 6;
      workGraphics.drawImage(diePic[die[i]], 73 * i, 0, this);
    }

  }

  public boolean action (Event e, Object arg) {
    if (e.target == button) {
      newDice();
      dicecanvas.repaint();
      return true;
    }
    return false;
  }
      
  // This is the main constrain() method
  // It has arguments for all constraints
  public void constrain(Container container, Component component,
			int grid_x, int grid_y, int grid_width,
			int grid_height, int fill, int anchor,
			double weight_x, double weight_y, int top,
			int left, int bottom, int right) {
    GridBagConstraints c = new GridBagConstraints();
    c.gridx = grid_x;  c.gridy = grid_y;
    c.gridwidth = grid_width;  c.gridheight = grid_height;
    c.fill = fill;  c.anchor = anchor ;
    c.weightx = weight_x;  c.weighty = weight_y;
    if (top+bottom+left+right>0)
      c.insets = new Insets(top, left, bottom, right);

    ((GridBagLayout)container.getLayout()).setConstraints(component, c);
    container.add(component);
  }

}


