📄 j2medice.java
字号:
/*J2MEDice - Mobile Java Dice Rolling Program based onJDice (JDice by Andreq D. Hilton)Copyright (C) 2008 Teemu Vaattovaara (t8vate00@students.oamk.fi)This program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */package J2MEDice;import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.lcdui.TextBox;import java.util.*;import java.io.*;import javax.microedition.lcdui.*;public class J2MEDice extends MIDlet implements CommandListener { Alert infoAlert; Image infoImage; Display midletDisplay; Form mainForm; Form detailsForm; Vector resultVector = new Vector(); /*store all results to vector*/ Vector lastResultVector = new Vector(); /*store only latest results*/ static final Command rollCommand = new Command("Roll selection", Command.SCREEN, 2); static final Command showLastRollCommand = new Command("Show last roll", Command.SCREEN, 3); static final Command d6Command = new Command("1 x Dice 6", Command.SCREEN, 3); static final Command twod6Command = new Command("2 x Dice 6", Command.SCREEN, 3); static final Command d10Command = new Command("1 x Dice 10", Command.SCREEN, 3); static final Command d12Command = new Command("1 x Dice 12", Command.SCREEN, 3); static final Command d20Command = new Command("1 x Dice 20", Command.SCREEN, 3); static final Command d100Command = new Command("1 x Dice 100", Command.SCREEN, 3); static final Command clearCommand = new Command("Clear roll history", Command.SCREEN, 4); static final Command exitCommand = new Command("Exit", Command.EXIT, 1); static final Command backCommand = new Command("Back", Command.BACK, 1); static final Command infoCommand = new Command("About", Command.SCREEN, 5); static final TextField diceText = new TextField("Dice selection", "", 20, TextField.ANY); public void startApp() { //try to load splashImage try { infoImage = Image.createImage("/images/dice.png"); } catch (IOException e) { } if (midletDisplay == null) { mainForm = new Form("J2MEDice"); midletDisplay = Display.getDisplay(this); mainForm.append(diceText); mainForm.addCommand(rollCommand); mainForm.addCommand(showLastRollCommand); mainForm.addCommand(d6Command); mainForm.addCommand(twod6Command); mainForm.addCommand(d10Command); mainForm.addCommand(d12Command); mainForm.addCommand(d20Command); mainForm.addCommand(d100Command); mainForm.addCommand(clearCommand); mainForm.addCommand(infoCommand); mainForm.addCommand(exitCommand); midletDisplay.setCurrent(mainForm); mainForm.setCommandListener(this); } } private void updateResults() { mainForm.deleteAll(); mainForm.append(diceText); mainForm.addCommand(rollCommand); mainForm.addCommand(showLastRollCommand); mainForm.addCommand(d6Command); mainForm.addCommand(twod6Command); mainForm.addCommand(d10Command); mainForm.addCommand(d12Command); mainForm.addCommand(d20Command); mainForm.addCommand(d100Command); mainForm.addCommand(clearCommand); mainForm.addCommand(infoCommand); mainForm.addCommand(exitCommand); //roll results are stored into vector //add StringItem for each resultVector item //NOTE: there should be some limit here...e.g.20 last results? int i = 0; for (i = 0; i < resultVector.size(); i++) { String resultText = (String) resultVector.elementAt(resultVector.size() - i - 1); StringItem resultItem = new StringItem(null, resultText+"\n", TextField.ANY); mainForm.append(resultItem); } } public void commandAction(Command c, Displayable d) { if (c == exitCommand) { notifyDestroyed(); } else if (c == showLastRollCommand) { showLastRollDetails(mainForm); } else if (c == d6Command) { diceText.setString("d6"); rollDice(diceText.getString()); } else if (c == twod6Command) { diceText.setString("2d6"); rollDice(diceText.getString()); } else if (c == d10Command) { diceText.setString("d10"); rollDice(diceText.getString()); } else if (c == d12Command) { diceText.setString("d12"); rollDice(diceText.getString()); } else if (c == d20Command) { diceText.setString("d20"); rollDice(diceText.getString()); } else if (c == d100Command) { diceText.setString("d100"); rollDice(diceText.getString()); } else if (c == rollCommand) { rollDice(diceText.getString()); } else if (c == clearCommand) { resultVector.removeAllElements(); } else if (c == infoCommand) { showInfo(mainForm); } else if (c == backCommand && d == detailsForm) { //delete last roll details detailsForm.deleteAll(); midletDisplay.setCurrent(mainForm); } updateResults(); } public void showLastRollDetails(Form returnForm) { detailsForm = new Form("Last roll details"); midletDisplay = Display.getDisplay(this); midletDisplay.setCurrent(detailsForm); detailsForm.addCommand(backCommand); int i = 0; //detailsForm.append(lastRollResult); TextField lastRollResult = new TextField(null, "", 100, TextField.ANY); lastRollResult.setString(""); int resultTotalLen = 0; for (i = 0; i < lastResultVector.size(); i++) { //String resultText = (String) lastResultVector.elementAt(lastResultVector.size() - i - 1); //int resultLen = ((String) (lastResultVector.elementAt(lastResultVector.size() - i - 1))).length() + 1; String resultText = (String) lastResultVector.elementAt(i); int resultLen = ((String) (lastResultVector.elementAt(i))).length() + 1; resultTotalLen = lastRollResult.size() + resultLen; lastRollResult.setMaxSize(resultTotalLen); lastRollResult.setString(resultText + "\n" + lastRollResult.getString()); } detailsForm.append(lastRollResult); /* for (i = 0; i < lastResultVector.size(); i++) { TextField tempRollResult = new TextField(null, "", 100, TextField.ANY); String tempResultText = (String) lastResultVector.elementAt(lastResultVector.size() - i - 1); tempRollResult.setString(tempResultText); detailsForm.append(tempRollResult); }*/ detailsForm.setCommandListener(this); } public void showInfo(Form returnForm) { infoAlert = new Alert("About J2MEDice", "This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 or later.\n\n See project web page http://sourceforge.net/projects/j2medice for more information and sourcecode.", infoImage, AlertType.INFO); infoAlert.setTimeout(Alert.FOREVER); midletDisplay.setCurrent(infoAlert, returnForm); } public void showError(Form returnForm, String errorText) { infoAlert = new Alert("Oops!", errorText, null, AlertType.ERROR); infoAlert.setTimeout(Alert.FOREVER); midletDisplay.setCurrent(infoAlert, returnForm); } public void pauseApp() { } public void destroyApp(boolean unconditional) { } private void rollDice(String diceString) { Vector v = DiceParser.parseRoll(diceString); String prepend = ""; int start = 0; int i; lastResultVector.removeAllElements(); if (v == null) { resultVector.addElement("Invalid dice string"); } else { for (i = 0; i < v.size(); i++) { DieRoll dr = (DieRoll) v.elementAt(i); RollResult rr = dr.makeRoll(); String toAdd = prepend + dr + " => " + rr; System.out.println(toAdd); resultVector.addElement(toAdd); lastResultVector.addElement(toAdd); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -