import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Graphics;
public class ShuangSeQiu6 extends MIDlet implements CommandListener {
private MyCanvas2 myCanvas;
private Display disp; // 引用MIDlet的Display 对象
private Command cmdExit; // 设定按钮用于退出MIDlet
private Command cmdOK; // 确定按钮
private MyCalc myCalc;
public ShuangSeQiu6() {
super();
//---
disp = Display.getDisplay(this); // 获得当前MIDlet的Display对象,以便进行界面显示
//---
myCanvas = new MyCanvas2();
//---
cmdOK = new Command("生成", Command.OK, 1);
cmdExit = new Command("退出", Command.EXIT, 1); // 新建两个控制按钮
//---
myCanvas.addCommand(cmdOK);
myCanvas.addCommand(cmdExit); // 添加控制按钮
myCanvas.setCommandListener(this); // 开始侦听命令
//---
myCalc = new MyCalc();
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub
}
protected void pauseApp() {
// TODO Auto-generated method stub
}
protected void startApp(){
disp.setCurrent(myCanvas);
}
public void commandAction(Command arg0, Displayable arg1)
{
if (arg0 == cmdExit) { // 按下退出键时停止运行
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
} else if (arg0 == cmdOK) { // 按下阅读键后阅读对应信息
myCanvas.myMessage = myCalc.getSSQ();
myCanvas.repaint();
}
}
}
class MyCanvas2 extends Canvas{
private MyFont myFont;
public String myMessage;
public MyCanvas2() {
super();
//---
myFont = new MyFont_35(this);
if (! myFont.checkFont())
{
myFont = new MyFont_25(this);
if (! myFont.checkFont())
{
myFont = new MyFont_16(this);
if (! myFont.checkFont())
myFont = new MyFont_D(this);
}
}
}
private void clearRect(Graphics grap){
grap.setColor(255,255,255);
grap.fillRect(0,0,this.getWidth(),this.getHeight());
}
protected void paint(Graphics grap){
clearRect(grap);
//---
myFont.drawString(grap,myMessage);
}
}
import java.util.Random;
public class MyCalc {
private final Random random_R = new Random();
private final Random random_B = new Random();
int[] items_R = new int[33 + 1];
int item_B;
private String FormatEx(int v){
if (v < 10){
return "0" + v;
}
else{
return "" + v;
}
}
public int getRandomInt(Random random,int v){
random.setSeed(System.currentTimeMillis());
return Math.abs(random.nextInt()) % v + 1; //产生1到v之间的随机数
//---
//return random.nextInt(v);
}
public String getSSQ() {
//---初始化
int itemCount = 0;
int i;
for (i = 1; i < items_R.length; i++) {
items_R[i] = 0;
itemCount++;
}
//---选择红球
int counter = 0;
int id;
int index;
while (true) {
id = getRandomInt(random_R,itemCount);
if (id == 0) {
continue;
}
//---
index = 0;
for (i = 1; i < items_R.length; i++) {
if (items_R[i] == 0)
index++;
if (index == id){
items_R[i] = 1;
break;
}
}
itemCount--;
//---
counter++;
if (counter == 6)
break;
}
//---选择蓝球
while (true) {
item_B = getRandomInt(random_B,16);
if (item_B != 0)
break;
}
//---输出结果
StringBuffer strResult = new StringBuffer();
for (i = 1; i < items_R.length; i++) {
if (items_R[i] == 1){
strResult.append(FormatEx(i));
strResult.append(",");
}
}
strResult.deleteCharAt(strResult.length() - 1);
//---
strResult.append("+");
strResult.append(FormatEx(item_B));
//---
return strResult.toString();
}
}