WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章J2ME技术进阶教程用RMS存储游戏积分

用RMS存储游戏积分
作者:j2medev  来源:j2medev  发布时间:2006-7-25 8:53:48
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;

import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordFilter;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 * A class used for storing and showing game scores.
 */
public class RMSGameScores extends MIDlet implements RecordFilter,
    RecordComparator {
  /*
   * The RecordStore used for storing the game scores.
   */
  private RecordStore recordStore = null;

  /*
   * The player name to use when filtering.
   */
  public static String playerNameFilter = null;

  /**
   * The constuctor opens the underlying record store, creating it if
   * necessary.
   */
  public RMSGameScores() {
    // Create a new record store for this example
    try {
      recordStore = RecordStore.openRecordStore("scores"true);
    catch (RecordStoreException rse) {
      System.out.println("Record Store Exception in the ctor." + rse);
      rse.printStackTrace();
    }
  }

  /**
   * startApp()
   */
  public void startApp() throws MIDletStateChangeException {
    RMSGameScores rmsgs = new RMSGameScores();
    rmsgs.addScore(100"Alice");
    rmsgs.addScore(120"Bill");
    rmsgs.addScore(80"Candice");
    rmsgs.addScore(40"Dean");
    rmsgs.addScore(200"Ethel");
    rmsgs.addScore(110"Farnsworth");
    rmsgs.addScore(220"Alice");
    RMSGameScores.playerNameFilter = "Alice";
    System.out
        .println("Print all scores followed by Scores for Farnsworth");
    rmsgs.printScores();
  }

  /*
   * Part of the RecordFilter interface.
   */
  public boolean matches(byte[] candidatethrows IllegalArgumentException {
    // If no filter set, nothing can match it.
    if (this.playerNameFilter == null) {
      return false;
    }

    ByteArrayInputStream bais = new ByteArrayInputStream(candidate);
    DataInputStream inputStream = new DataInputStream(bais);
    String name = null;

    try {
      int score = inputStream.readInt();
      name = inputStream.readUTF();
    catch (EOFException eofe) {   System.out.println(eofe);
      eofe.printStackTrace();
    catch (IOException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }
    return (this.playerNameFilter.equals(name));
  }

  /*
   * Part of the RecordComparator interface.
   */
  public int compare(byte[] rec1, byte[] rec2) {

    // Construct DataInputStreams for extracting the scores from
    // the records.
    ByteArrayInputStream bais1 = new ByteArrayInputStream(rec1);
    DataInputStream inputStream1 = new DataInputStream(bais1);
    ByteArrayInputStream bais2 = new ByteArrayInputStream(rec2);
    DataInputStream inputStream2 = new DataInputStream(bais2);
    int score1 = 0;
    int score2 = 0;
    try {
      // Extract the scores.
      score1 = inputStream1.readInt();
      score2 = inputStream2.readInt();
    catch (EOFException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    catch (IOException eofe) {
      System.out.println(eofe);
      eofe.printStackTrace();
    }

    // Sort by score
    if (score1 > score2) {
      return RecordComparator.FOLLOWS;
    else if (score1 < score2) {
      return RecordComparator.PRECEDES;
    else {
      return RecordComparator.EQUIVALENT;
    }
  }

  /**
   * Add a new score to the storage.
   
   @param score
   *            the score to store.
   @param playerName
   *            the name of the play achieving this score.
   */
  public void addScore(int score, String playerName) {
    // Each score is stored in a separate record, formatted with
    // the score, followed by the player name.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(baos);
    try {
      // Push the score into a byte array.
      outputStream.writeInt(score);
      // Then push the player name.
      outputStream.writeUTF(playerName);
    catch (IOException ioe) {
      System.out.println(ioe);
      ioe.printStackTrace();
    }

    // Extract the byte array
    byte[] b = baos.toByteArray();
    try {
      // Add it to the record store
      recordStore.addRecord(b, 0, b.length);
    catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }
  }

  /**
   * A helper method for the printScores methods.
   */
  private void printScoresHelper(RecordEnumeration re) {

    try {
      while (re.hasNextElement()) {
        int id = re.nextRecordId();
        ByteArrayInputStream bais = new ByteArrayInputStream(
            recordStore.getRecord(id));
        DataInputStream inputStream = new DataInputStream(bais);
        try {
          int score = inputStream.readInt();
          String playerName = inputStream.readUTF();
          System.out.println(playerName + " = " + score);
        catch (EOFException eofe) {
          System.out.println(eofe);
          eofe.printStackTrace();
        }
      }
    catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    catch (IOException ioe) {
      System.out.println(ioe);
      ioe.printStackTrace();
    }
  }

  /**
   * This method prints all of the scores sorted by game score.
   */
  public void printScores() {
    try {
      // Enumerate the records using the comparator implemented
      // above to sort by game score.

      // No RecordFilter here. All records in the RecordStore
      RecordEnumeration re = recordStore.enumerateRecords(null, this,
          true);

      // Print all scores
      System.out.println("Print all scores...");
      printScoresHelper(re);

      // Enumerate records respecting a RecordFilter
      re = recordStore.enumerateRecords(this, this, true);

      //Print scores for Farnsworth
      System.out.println("Print sc

[1] [2]  下一页

[] [返回上一页] [打 印]
文章评论

用户名: 查看更多评论

分 值:100分 85分 70分 55分 40分 25分 10分 0分

内 容:

         (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码