Reg.java
资源名称:J2ME&Game.rar [点击查看]
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:4k
源码类别:
J2ME
开发平台:
Java
- import javax.microedition.lcdui.*;
- import javax.microedition.rms.RecordStore;
- import javax.microedition.rms.RecordStoreException;
- public class Reg
- implements CommandListener
- {
- private FiveMIDlet midFive;
- private boolean registered;
- private static long nTriedMax = 0x989680L;
- private int nTriedLeft;
- private int serialNo;
- private Form frmReg;
- private StringItem siInfo;
- private TextField tfRegNo;
- private Command cmdOk;
- private Command cmdCancel;
- public Reg(FiveMIDlet m)
- {
- midFive = m;
- registered = false;
- nTriedLeft = 0x989680;
- serialNo = (int)(System.currentTimeMillis() % 0xf4240L);
- load();
- save();
- }
- public boolean canPlay()
- {
- return registered || nTriedLeft > 0;
- }
- public boolean registered()
- {
- return registered;
- }
- public int nTriedLeft()
- {
- return nTriedLeft;
- }
- public void play()
- {
- if(!registered && nTriedLeft > 0)
- save();
- }
- public void register(Display display)
- {
- frmReg = new Form("注册五子棋");
- frmReg.append("您的序列号:[" + serialNo + "]n" + "请把该号码告知作者并获得注册号");
- tfRegNo = new TextField("请输入注册号:", "", 8, 2);
- frmReg.append(tfRegNo);
- siInfo = new StringItem(null, null);
- frmReg.append(siInfo);
- cmdOk = new Command("确定", 4, 1);
- cmdCancel = new Command("取消", 3, 2);
- frmReg.addCommand(cmdOk);
- frmReg.addCommand(cmdCancel);
- frmReg.setCommandListener(this);
- display.setCurrent(frmReg);
- }
- private void freeRes()
- {
- frmReg = null;
- cmdOk = null;
- cmdCancel = null;
- siInfo = null;
- tfRegNo = null;
- }
- public void commandAction(Command c, Displayable s)
- {
- if(c == cmdOk)
- {
- String sRegNo = tfRegNo.getString();
- int regNo = 0;
- try
- {
- regNo = Integer.parseInt(sRegNo);
- }
- catch(Exception _ex) { }
- if(isRegNoOk(regNo))
- {
- registered = true;
- save();
- midFive.backHome();
- freeRes();
- } else
- {
- siInfo.setText("注册号不正确!请重新输入!");
- }
- } else
- if(c == cmdCancel)
- {
- midFive.backHome();
- freeRes();
- }
- }
- private boolean isRegNoOk(int regNo)
- {
- return regNo == serialNo;
- }
- private void save()
- {
- try
- {
- byte bs[] = new byte[5];
- bs[0] = (byte)(registered ? 1 : 0);
- bs[1] = (byte)nTriedLeft;
- bs[2] = (byte)(serialNo >> 16 & 0xff);
- bs[3] = (byte)(serialNo >> 8 & 0xff);
- bs[4] = (byte)(serialNo & 0xff);
- RecordStore rs = RecordStore.openRecordStore("Reg", true);
- if(rs.getNumRecords() >= 1)
- rs.setRecord(1, bs, 0, 5);
- else
- rs.addRecord(bs, 0, 5);
- rs.closeRecordStore();
- }
- catch(RecordStoreException _ex) { }
- }
- private void load()
- {
- try
- {
- RecordStore rs = RecordStore.openRecordStore("Reg", false);
- if(rs.getNumRecords() >= 1)
- {
- byte bs[] = rs.getRecord(1);
- if(bs.length >= 5)
- {
- registered = bs[0] == 1;
- nTriedLeft = bs[1];
- if(nTriedLeft > 10)
- nTriedLeft = 10;
- int b1 = bs[2];
- if(b1 < 0)
- b1 += 256;
- int b2 = bs[3];
- if(b2 < 0)
- b2 += 256;
- int b3 = bs[4];
- if(b3 < 0)
- b3 += 256;
- serialNo = (b1 << 16) + (b2 << 8) + b3;
- }
- }
- rs.closeRecordStore();
- }
- catch(RecordStoreException _ex) { }
- }
- }