D:\582615994.doc 1/5 myCasino.java (vormals test_zufallszahlen.java) Zufallszahlen – Glückszahl, Lotto 6 aus 45, Bildeinbindung (*.png) Das MIDlet besteht aus einem Screen mit einer MenüListe und einem Screen mit einem Formular, auf dem die Texte/Zahlen ausgegeben werden. Liste (= ein Screen auf dem Display; wie Form) für Auswahl = Hauptmenü Implicite Liste für Auswahl (= Hauptmenü) myList = new List("Casino Royale",List.IMPLICIT); Ticker ("ticker-tape"), a piece of text that runs continuously across the display. Random(); an instance of this class (import java.util.Random!) is used to generate a stream of pseudorandom numbers. Methodenaufruf ( ≡ Aufruf von Prozeduren bzw. Unterprogrammen) – void od.mit Rückgebewert (return) MIDlet (Application) myList (Menü) Zwölf Lottotipps Glücksklee Die CommandAction ist nun etwas komplexer: Von der (Menü-)Liste (Screen) wird auf das Formular (Screen) verzweigt. Das Formular erhält dann je nach Funktion verschiedene Commandofunktionen, die beim Rücksprung auf die (Menü-)Liste entfernt werden müssen. D:\582615994.doc 2/5 import javax.microedition.midlet.*; import javax.microedition.lcdui.*; import java.lang.String; import java.lang.Integer; import java.util.Random; import java.util.Date; import java.io.IOException; public class myCasino extends MIDlet implements CommandListener { private Display display; private Form myForm; private Ticker myTicker; private List myList; Command selectFromListCommand; Command backToListCommand; Command exitCommand; // Exit the MIDlet Command nextGluecksZahlCommand; Command nextLottoSixPackCommand; Random myRandom; int[] myLottoSet; StringItem myStringItem1, myStringItem2; String myLottoSixPackString; private Image[] myIcons = new Image[4]; //liegen im Ordner "res" public myCasino() { super(); //Bilder für ("Menü-")Liste einlesen readMyIcons(); display = Display.getDisplay(this); //Liste für Auswahl = Hauptmenü myList = new List("Casino Royale",List.IMPLICIT); //EXCLUSIVE=RadioButtons, MULTIPLE=Checkboxes //myList.append("Glückszahl",null); //falls kein Bild gewünscht myList.append("Glückszahl",myIcons[0]); myList.append("Ein Lottotipp 6/45",myIcons[1]); myList.append("Zwölf Lottotipps 6/45",myIcons[2]); myList.append("GlücksKlee",myIcons[3]); selectFromListCommand = new Command("Go to", Command.ITEM, 1); myList.setSelectCommand(selectFromListCommand); //bewirkt gleichzeitig ein myList.addCommand(selectFromListCommand); exitCommand = new Command("Exit", Command.EXIT, 1); myList.addCommand(exitCommand); myList.setCommandListener(this); //Formular zur Darstellung eines Menüpunktes myForm = new Form(null); myForm.setCommandListener(this); backToListCommand = new Command("BackToList", Command.BACK, 1); myForm.addCommand(backToListCommand); nextGluecksZahlCommand = new Command("Noch eine", Command.BACK, 1); nextLottoSixPackCommand = new Command("Nächster", Command.BACK, 1); } D:\582615994.doc 3/5 protected void startApp() { myRandom = new Random(); myTicker = new Ticker("Gott würfelt nicht!"); myList.setTicker(myTicker); display.setCurrent(myList); } protected void pauseApp() {} protected void destroyApp(boolean arg0) {} public void commandAction(Command c, Displayable d) { int itemIndex; if(c == exitCommand) { destroyApp(false); notifyDestroyed(); } else if (c==nextGluecksZahlCommand) { if (myForm.size()>0){myForm.deleteAll();}//alte Einträge löschen myForm.append(DateAndTime()); myForm.append("\nDeine Glückszahl ist: } "+myGlueckszahl()+"\n"); else if (c==nextLottoSixPackCommand) { if (myForm.size()>0){myForm.delete(0);}//alte Einträge löschen myStringItem1 = new StringItem(null,myLottoSixPack()); //Set von sechs Zahlen myStringItem1.setLayout(Item.LAYOUT_LEFT); myForm.append(myStringItem1); } else if (c==selectFromListCommand){ itemIndex = myList.getSelectedIndex(); //System.out.println("test"); //funktioniert als debugmöglichkeit - ausgabe auf wtk-oberfläche! myForm.removeCommand(nextGluecksZahlCommand); //geht das auch gut, wenn command nicht existiert? (z.B. beim ersten mal) //API: If the command is not in the Displayable (tested by comparing the object references), the method has no effect. myForm.removeCommand(nextLottoSixPackCommand); if (myForm.size()>0){myForm.deleteAll();}//Altbestand löschen if (itemIndex==0){//Glückszahl // if (myForm.size()>0){myForm.deleteAll();} myForm.append(DateAndTime()); myForm.append("Deine Glückszahl ist: "+myGlueckszahl()+"\n"); myForm.addCommand(nextGluecksZahlCommand); } if (itemIndex==1){//6 aus 45 //if (myForm.size()>0){myForm.deleteAll();} myStringItem1 = new StringItem(null,myLottoSixPack()); myStringItem1.setLayout(Item.LAYOUT_LEFT); myForm.append(myStringItem1); myForm.addCommand(nextLottoSixPackCommand); } D:\582615994.doc 4/5 if (itemIndex==2){//12 mal 6 aus 45 myLottoSet = new int[6]; for (int j=0;j<12;j++){ myLottoSixPackString=""; myLottoSet= mySechsAus45(); for (int i=0;i<6;i++){ //im sixpack ausgeben: myForm.append(Integer.toString( myLottoSet[i]) +" "); myLottoSixPackString+=Integer.toString( myLottoSet[i]) +" "; } //myForm.append(myLottoSixPackString+"\n"); myLottoSixPackString+="\n"; myStringItem2 = new StringItem(null,myLottoSixPackString); myStringItem2.setLayout(Item.LAYOUT_CENTER); myForm.append(myStringItem2); }//j } if (itemIndex==3){//Bild laden loadMyImage();} display.setCurrent(myForm); } else if (c==backToListCommand){display.setCurrent(myList);} } //_________________________________________________________________________ //_________________________________________________________________________ public String myLottoSixPack(){ myLottoSet = new int[6]; myLottoSixPackString=""; myLottoSet= mySechsAus45(); for (int i=0;i<6;i++){ //einzeln ausgeben: myForm.append(Integer.toString( myLottoSet[i]) +" "); //im sixpack ausgeben: myForm.append(Integer.toString( myLottoSet[i]) +" "); myLottoSixPackString+=Integer.toString( myLottoSet[i]) +" "; } return myLottoSixPackString; } //_________________________________________________________________________ public int myGlueckszahl(){ final int MAX_RND=45; //int n = 1+Math.abs(myRandom.nextInt()) % MAX_RND; return 1+myRandom.nextInt(44); //.nextInt(int i) gibt es erst in CLDC1.1 //return n; // return 1+Math.abs(myRandom.nextInt()) % MAX_RND; }//myGlueckszahl //_________________________________________________________________________ public int[] mySechsAus45(){ final int MAX_RND=45; int[] myLottoSet = new int[6]; boolean istOK; for (int i=0;i<6;i++) { do { D:\582615994.doc 5/5 istOK=true; myLottoSet[i]=1+Math.abs(myRandom.nextInt()) % MAX_RND; for (int j=0; j<i; j++) {if (myLottoSet[i]==myLottoSet[j]) {istOK=false;} }//for j } while (!istOK); } //for i myLottoSet=myBubblesort(myLottoSet); return myLottoSet; }//sechs_aus_45 //_________________________________________________________________________ public int[] myBubblesort(int[] a ){ for(int i=0;i<6;i++) { for (int j=0;j<(5-i);j++) { if(a[j+1]<a[j]) {int h=a[j]; a[j]=a[j+1]; a[j+1]=h; }//if } //j } //i return a; } //_________________________________________________________________________ public void loadMyImage(){ try { myForm.append(new Spacer(1,30)); //für Zwischenräume auf dem Bildschirm ImageItem myImageItem = new ImageItem ("Glücksklee\n", Image.createImage ("/gluecksklee.png"), ImageItem.LAYOUT_CENTER, null); myForm.append (myImageItem); } catch (IOException e) { myForm.append (new StringItem ("Sorry", "\nImage not available:\n"+e)); } } //_________________________________________________________________________ public String DateAndTime(){ String s; s=new Date().toString(); //Format des Strings: dow mon dd hh:mm:ss zzz yyyy //Datum in der Form "17. Jan. 2007 15:30" anzeigen return s.substring(8,10)+". "+s.substring(4,7)+". "+s.substring(24,28)+" "+s.substring(11,19)+"\n"; } //_________________________________________________________________________ public void readMyIcons() { //Quelle: C:\WTK22\apps\HANSERBuch\FormBeispiel\src\de\java_me\beispiele\formbeispiel\FormMIDlet.java try { for (int i = 0; i < myIcons.length; ++i) { myIcons[i] = Image.createImage("/myCasino_" + i + ".png"); } } catch (IOException ioe) { // keine Reaktion, falls Bilder nicht vorhanden } } //_________________________________________________________________________ }//finis