/*
 * Dice.java
 *
 * Created on 31. Dezember 2000, 14:09
 */

import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

/**
 * @author  Dirk Sturzebecher
 * @version 0.1
 */
public class Dice extends MIDlet  implements CommandListener, ItemStateListener
{
    // -----------------------------------------------------------------------

    // GUI
    private Display display;
    private TextField field;
    private Gauge gauge;
    private Form form;

    // commands
    private Command role;

    // numbers
    private Random random;
    private int number;
    private int max=6;          // default faces
    private int MAX=24;        // max faces

    // -----------------------------------------------------------------------

    public void commandAction(Command c, Displayable s){
        if(c==role){
            number=random.nextInt();
            if(number<0)
            number=-number;
            if(number>max)
            number=number%max;
            if(number==0)
            number=max;
            field.setString(""+number);
        }
    }

    // -----------------------------------------------------------------------

    public void itemStateChanged(Item item)
    {
      if(item==gauge)    
        {
          max=gauge.getValue()+2;
          gauge.setLabel( "set max ("+max+")"); 
        }
    }
    
    // -----------------------------------------------------------------------

    public Dice(){
        // screen
        display=Display.getDisplay(this);
        field=new TextField(null,""+0,10,TextField.NUMERIC);
        gauge=new Gauge("set max ("+max+")",true,MAX-2,max-2);
        form=new Form("dice");
        form.append(field);
        form.append(gauge);
        form.setItemStateListener(this);
        // comands
        role=new Command("role",Command.SCREEN,1);
        random=new Random();
    }

    public void startApp() {
        form.addCommand(role);
        form.setCommandListener(this);
        display.setCurrent(form);
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    // -----------------------------------------------------------------------
}