/* @(#)Dice.java   28 September 2006 */

/* Useful imports */

import edu.neu.ccs.*;
import edu.neu.ccs.gui.*;
import edu.neu.ccs.codec.*;
import edu.neu.ccs.console.*;
import edu.neu.ccs.filter.*;
import edu.neu.ccs.jpf.*;
import edu.neu.ccs.parser.*;
import edu.neu.ccs.pedagogy.*;
import edu.neu.ccs.quick.*;
import edu.neu.ccs.util.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.font.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import java.math.*;
import java.beans.*;
import java.lang.reflect.*;
import java.net.URL;
import java.util.regex.*;
import java.text.ParseException;


public class Dice
    extends DisplayPanel
{
    /** The dice URL. */
    private String diceURL  =
        "http://www.ccs.neu.edu/jpt/images/dice/";
    
    /** The image list file name for reading the dice. */
    private String diceList =
        "imagelist.txt";
    
    /** The dice as an ImagePaintableLite[]. */
    private ImagePaintableLite[] dice =
        WebImageTools.readImagesAsPaintableLite
            (diceURL, diceList);
    
    /** The size of the dice array. */
    private int N = dice.length;
    
    
    /** The tile for die #1. */
    private Tile die1 = null;
    
    /** The tile for die #2. */
    private Tile die2 = null;
    
    
    /** The action to toss dice. */
    private SimpleAction tossDice =
        new SimpleAction("Toss Dice") {
            public void perform() { tossDice(); }
    };
    
    
    /** The panel gap. */
    private int gap = 20;
    
    
    /** The constructor. */
    public Dice() {
        if (N == 0) {
            addObject("Dice images failed to load from the web");
            return;
        }
        
        makeTiles();
        makeGUI();
    }
    
    /** The method to make the initial blank tiles. */
    private void makeTiles() {
        die1 = new Tile(dice[0]);
        die2 = new Tile(dice[0]);
    }
    
    
    /** The method to make the GUI. */
    private void makeGUI() {
        Object[] tileStuff = { die1, die2 };
        HTable tilePanel = new HTable(tileStuff, gap, gap, CENTER);
        
        Object[] mainStuff = { tilePanel, tossDice };
        VTable mainPanel = new VTable(mainStuff, gap, gap, CENTER);
        
        tilePanel.setBackground(Colors.tan);
        mainPanel.setBackground(Colors.tan);
        mainPanel.emptyBorder(gap);
        
        add(mainPanel);
    }
    
    
    /** The method to do a random toss of the dice. */
    private void tossDice() {
        int a = MathUtilities.randomInt(1, 6);
        int b = MathUtilities.randomInt(1, 6);
        
        die1.setPaintable(dice[a]);
        die2.setPaintable(dice[b]);
    }
    
    
    /** The main program. */
    public static void main(String[] args) {
        new Dice().frame("Random Dice");
    }
}

