קוד:
package twister;
import java.util.Random;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeExcepti on;
public class Twister extends MIDlet implements CommandListener
{
Command cmdGenerate = new Command("Regenerate", Command.BACK, 1);
Command cmdAbout = new Command("About", Command.ITEM, 1);
Display phoneDisplay = null;
Random rand = new Random();
String[] organs = {"רגל","יד"};
String[] sides = {"ימין","שמאל"};
String[] colors = {"אדום", "כחול", "צהוב","ירוק"};
public Twister()
{
// The seed makes the random funcion more effective
rand.setSeed(System.currentTimeMillis());
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {}
protected void pauseApp() {}
protected void startApp() throws MIDletStateChangeException
{
phoneDisplay = Display.getDisplay(this);
Form mainForm = new Form("Twister");
// Creates the organ selector
StringItem nextOrgan = new StringItem("טרם נקבע..", null);
nextOrgan.setLayout(Item.LAYOUT_RIGHT);
mainForm.append(nextOrgan);
StringItem nextOrganLbl = new StringItem(null, "האיבר הבא הוא:");
nextOrganLbl.setLayout(Item.LAYOUT_RIGHT | Item.LAYOUT_NEWLINE_AFTER);
mainForm.append(nextOrganLbl);
// Creates the color selector
StringItem nextColor = new StringItem("טרם נקבע..", null);
nextColor.setLayout(Item.LAYOUT_RIGHT);
mainForm.append(nextColor);
StringItem nextColorLbl = new StringItem(null, "להניח בצבע:");
nextColorLbl.setLayout(Item.LAYOUT_RIGHT | Item.LAYOUT_NEWLINE_AFTER);
mainForm.append(nextColorLbl);
mainForm.addCommand(cmdGenerate);
mainForm.addCommand(cmdAbout);
phoneDisplay.setCurrent(mainForm);
mainForm.setCommandListener(this);
}
// Get random number up until the argumented untilWhen number
private int rand(int untilWhen)
{
int udini = rand.nextInt() % (untilWhen + 1);
return Math.abs(udini);
}
// This function returns the oragan and its side by int values
private String getBodyOrgan(int org, int side)
{
return organs[org] + " " + sides[side];
}
public void commandAction(Command arg0, Displayable arg1)
{
if (arg0.equals(cmdGenerate))
{
// Sets the body organ on the currect label from the form
((StringItem)((Form)arg1).get(0)).setLabel(getBody Organ(rand(1), rand(1)));
// Sets the color on the currect label from the form
((StringItem)((Form)arg1).get(2)).setLabel(colors[rand(3)]);
}
else if (arg0.equals(cmdAbout))
{
Alert about =
new Alert(null,
"Twister\nVersion: 1.0\nDeveloped by\nGh0sT d0g",
null, AlertType.INFO);
about.setTimeout(5000);
Display.getDisplay(this).setCurrent(about, arg1);
}
}
}