⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gaofencanvas.java

📁 用J2ME写的一个益智类手机游戏。玩家回答问题
💻 JAVA
字号:
package mindsurf;

import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
import com.nokia.mid.ui.FullCanvas;

public class GaofenCanvas
    extends FullCanvas {
    Display display;
   private RecordStore rs;
   int[] score;
   private Image wenhao=null;
   private int userFenshu;
   private int a[][]={{1,0,0,1},
                   {0,1,1,0},
                   {0,1,1,0},
                   {1,0,0,1},
                   };

  public GaofenCanvas(Display display) {
    for (int i = 1; i < 6; i++) {
            add(0, 0);
          }

     this.display=display;
     try {
      wenhao=Image.createImage("/res/wenhao.png");
    }
    catch (Exception e) {}
    score = new int[6];
    rs = openRSAnyway("Heros");
    userFenshu=IqMIDlet.score;
    if (rs == null) {
      System.out.println("rs open failed");
    }
    else {
      try {
          if(userFenshu>score[5]){
            add(userFenshu, 5);
            lineUp();
          }
      }
      catch (Exception e) {
      }
    }
    readData();
  }

  public void paint(Graphics g) {
    g.setColor(0xFFCC66);
    g.fillRect(0,0,128,128);
    for (int i = 0; i < 4; i++) {
      for(int j=0;j<4;j++){
        if(a[i][j]==1){
          g.drawImage(wenhao, j * 32, i * 32,
                      Graphics.LEFT | Graphics.TOP);
        }
       }
     }
    g.setColor(0x990099);
    Font f=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_ITALIC,Font.SIZE_SMALL);
    g.setFont(f);
    for (int i = 1; i < 6; i++) {
      g.drawString("play"+String.valueOf(i), 10,
                   10 + i * 15, 20);
      g.drawString(String.valueOf(score[i]), 70,
                   10 + i * 15, 20);
    }
    g.drawString("press any key", 50, 110, 20);
  }

  public RecordStore openRSAnyway(String rsName) {
    RecordStore rs = null;
    if (rsName.length() > 32) {
      return null;
    }

    try {
      rs = RecordStore.openRecordStore(rsName, true);
      return rs;
    }
    catch (Exception e) {
      return null;
    }

  }

  public void add(int score, int ID) {
    ByteArrayOutputStream bo = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bo);

    try {
      dos.writeInt(score);
      byte data[] = bo.toByteArray();
      if (ID == 0) {
        rs.addRecord(data, 0, data.length);
      }
      else {
        rs.setRecord(ID, data, 0, data.length);
      }

      dos.close();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }

  public void readData() {
    byte[] data;
    DataInputStream dis;

    for (int i = 1; i < 6; i++) {
      try {
        data = rs.getRecord(i);
        dis = new DataInputStream(new ByteArrayInputStream(data));
        score[i] = dis.readInt();
        dis.close();
      }
      catch (Exception e) {
      }
    }
  }

  public void lineUp() { //must be runned after readData() method
    int tempL;
    readData();
    for (int i = 1; i < 6; i++) {

      for (int j = i + 1; j < 6; j++) {
        if (score[i] < score[j]) {
          tempL = score[i];
          score[i] = score[j];
          score[j] = tempL;
        }
      }

    }

    for (int i = 1; i < 6; i++) {
      add(score[i], i);
    }
  }

  public boolean shownOnHeros(int theLevel) {
    if (theLevel > score[5]) {
      return true;
    }
    else {
      return false;
    }
  }

  public void refresh() {
    readData();
    repaint();
  }

  protected void keyPressed(int KeyCode) {
        try {
          rs.closeRecordStore();
        }
        catch (Exception e) { }
        rs = null;
        wenhao = null;
        System.gc();
        display.setCurrent(new MainCanvas(display));
    }

    public void initiateRS() {
      for (int i = 1; i < 6; i++) {
        add(0, i);
      }
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -