文章分类 | 推荐文章 | 最新文章 | 热点文章 | 最新软件 | 精品软件 | 下载排行 | 推荐下载 | firefox | WPS | 杀毒软件 | Picasa
清风网络
首 页 软件下载 网络学院 数码学院
QQ 电脑入门 游戏 操作系统 图形图像 办公软件 媒体动画 精文荟萃 常用软件 网页编程 技术开发 网络技术 认证考试 网站建设 文章专栏
当前位置:清风网络学院网络编程J2EE/J2ME用RMS存储游戏积分
精品推荐
特别推荐
·J2EE Web开发技术期待一次新的技术变革
·J2ME程序开发初学者快速入门的九大要点
·使用技巧:J2ME中程序优化的十个小方法
·RMS从入门到精通之一
·J2EE应用程序中SQL语句的自动构造方法
·解决J2EE系统应用性能问题常用优化项目
·J2EE实用技巧:提升JSP应用程序的绝招
·J2ME内存优
·J2ME简介
·J2EE技术
热点TOP10
·FC API(JSR 75)简单讲解
·用J2ME实现简单电子邮件发送功能
·3D数学知识简介
·从自定义字节数组创建图片
·JBoss 文档(三) JBoss和JMS
·通用联接框架(GCF)连接类型使用总结
·基于J2EE的Blog平台
·JBoss文档(二)??JBoss开发、打包、部署
·MIDP终端模拟之一:一个简单的模拟器MIDlet
·MIDP终端模拟之二:高级终端模拟
·经典飞机游戏代码S60
·走进JBoss (1)
·基于MIDP1.0实现通信录
·RMS高效编程指南
·Spring 系列:当 Hibernate 遇上 Spring
·介绍J2ME可选包WMA(JSR120)
·J2ME联网中采用序列化机制
·Spring 系列:Spring JMS 消息处理
·JBOSS4数据源配置大全
·J2ME可选包—PIM介绍

用RMS存储游戏积分

日期:2007年5月11日 作者: 查看:[大字体 中字体 小字体]


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[] candidate) throws 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 scores for : " + this.playerNameFilter);
      printScoresHelper(re);
    } catch (RecordStoreException rse) {
      System.out.println(rse);
      rse.printStackTrace();
    }}

  /**
   * pauseApp()
   */
  public void pauseApp() {
    System.out.println("pauseApp()");
  }

  /**
   * destroyApp()
   * 
   * This closes our open RecordStore when we are destroyed.
   * 
   * @param cond
   *            true if this is an unconditional destroy false if it is not
   *            (ignored here and treated as unconditional)
   */
  public void destroyApp(boolean cond) {
    System.out.println("destroyApp( )");
    try {
      if (recordStore != null)
        recordStore.closeRecordStore();
    } catch (Exception ignore) {
      // ignore this
    }
  }

}
[1] [2] 下一页 




上一篇:对RMS中的数据进行排序

下一篇:调用 CGI 脚本

用RMS存储游戏积分 相关文章:
·用RMS存储游戏积分
用RMS存储游戏积分 相关软件:

特别声明:本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。
[打印本页] [关闭窗口] 转载请注明来源:http://www.viphot.com
| 帮助(?) | 版权声明 | 友情连接 | 关于我们 | 信息发布
Copyright 2007 www.viphot.com All Rights Reserved. 鄂ICP备05000083号Powered by:vipcn