/* RandomDice.java
 * October 24, 2006
 * John Costanzo
 */

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 RandomDice extends VTable
{
	/* load the dice from the web */
	private String diceURL = "http://www.ccs.neu.edu/jpt/images/dice/";
	private String diceList = "imagelist.txt";
	private ImagePaintableLite[] dice = 
		WebImageTools.readImagesAsPaintableLite
		(diceURL, diceList);
	private int N = dice.length;
	
	/* represent the dice */
	private HTable table;
	private Tile die1 = null;
	private Tile die2 = null;
	
	/* "Toss Dice" button */
	private SimpleAction roll = new SimpleAction( "Toss Dice" ) {
		public void perform() { tossDice(); }
	};
	
	public static void main(String args[])
	{
		new RandomDice().frame( "Random Dice" );
	}
	
	public RandomDice()
	{
		super( null, 20, 20, CENTER );
		
		if( N == 0 )
		{
			addObject( "Dice images failed to load from the web" );
			return;
		}
		
		makeTiles();
		makeGUI();
	}
	private void makeTiles()
	{
		die1 = new Tile(dice[0]);
		die2 = new Tile(dice[0]);
	}
	private void makeGUI()
	{
		setBackground( Colors.tan );
		emptyBorder(20);
		
		Object[] objs = { die1, die2 };
		table = new HTable( objs, 20, 20, CENTER );
		table.setBackground( Colors.tan );

		addObject( table );
		addObject( roll );
	}
	private void tossDice()
	{
		int a = MathUtilities.randomInt( 1, 6 );
		int b = MathUtilities.randomInt( 1, 6 );
		die1.setPaintable(dice[a]);
		die2.setPaintable(dice[b]);
	}
}
