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

📄 testcanvas.java

📁 j2me 源码
💻 JAVA
字号:
import javax.microedition.lcdui.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;
import java.util.Enumeration;

public class TestCanvas
    extends Canvas
    implements Runnable {

  testMIDlet game;
  private Thread gameThread;
  private int key;
  private int key_delay;
  private boolean is_keypressed = false;
  private boolean quitGame = false;
  private boolean isOK = false;
  String root[] = null;
  private int rootnum = 0;
  private final int WIDTH = getWidth();
  private final int HEIGHT = getHeight();

  private FileConnection fc;
  public TestCanvas(testMIDlet game) {
    this.game = game;
  }


  public void game_start() {
    gameThread = new Thread(this);
    gameThread.start();
    getRoots();
    openfile(0);
  }

  public void game_stop() {
    gameThread = null;
    System.gc();
  }

  public void paint(Graphics g) {
    g.setColor(0);
    g.fillRect(0,0,WIDTH,HEIGHT);
    try{
      g.setColor(0xffffff);
      g.drawString(""+isOK,20,20,20);
    }
    catch(Exception e){

    }
  }

  public void run() {
    long starttime = 0;
    long timetaken = 0;
    try {
      while (!quitGame) {
//        System.out.println("select_menu == "+select_menu);
        starttime = System.currentTimeMillis();
        if(is_keypressed&&key_delay==0){
        keyProcess();
      }

      if(key_delay>0){
        key_delay--;
      }

        repaint();
        serviceRepaints();
        System.gc();
        timetaken = System.currentTimeMillis() - starttime;
          if (timetaken < 80)
            gameThread.sleep(80 - timetaken);
      }

    }
    catch (Exception ex) {

    }
    game.exit();
  }

  //  key
  protected  void keyPressed(int key) {
    is_keypressed = true;
     this.key = key;
     if(key==KEY_STAR){
       quitGame = true;
     }
  }

  protected  void keyReleased(int key) {
    is_keypressed = false;
    this.key = key;
  }

  public void keyProcess(){
    key_delay = 2;
  }

  //open file
  private void openfile(int rootnum){
    try {
      fc = (FileConnection) Connector.open("file:///"+root[rootnum]+"/dfdf.txt",3);
      if(!fc.exists()) {
        fc.create();
          throw new IOException("File does not exist");
       }
       isOK = false;
       String url = fc.getURL();
       String rname = fc.getName();
       System.out.println("rname"+rname);
      System.out.println(""+fc.isOpen());
      System.out.println("url = "+url);
      System.out.println(""+ fc.canWrite());
      System.out.println(""+ fc.canRead());
      DataInputStream is = fc.openDataInputStream();
      byte b[] = new byte[1024];
       int length = is.read(b, 0, 1024);
       System.out.println
          ("Content of "+"testfile" + ": "+ new String(b, 0, length));
      is.close();
      DataOutputStream dos = fc.openDataOutputStream();
      String s = "add to testfile++";
      dos.write(s.getBytes());
      dos.close();
      dos = null;
      fc.close();
      fc=null;
    }
    catch (IOException ex) {
      System.out.println("openfile false!");
      isOK = true;
    }
  }

 private void getRoots() {
   root = new String[100];
    Enumeration drives = FileSystemRegistry.listRoots();
    System.out.println("The valid roots found are: ");
    while(drives.hasMoreElements()) {
       root[rootnum] = (String) drives.nextElement();
       System.out.println("\t"+root[rootnum]);
       rootnum++;
    }
 }

 private void GetSDcardContent() {
    try {
       FileConnection fc = (FileConnection)
          Connector.open("file:///CFCard/");
       // Get a filtered list of all files and directories.
       // True means: include hidden files.
       // To list just visible files and directories, use
       // list() with no arguments.
       System.out.println
          ("List of files and directories under CFCard:");
       Enumeration filelist = fc.list("*", true);
       while(filelist.hasMoreElements()) {
          String fileName = (String) filelist.nextElement();
          fc = (FileConnection)
             Connector.open("file:///CFCard/" + fileName);
          if(fc.isDirectory()) {
             System.out.println("\tDirectory Name: " + fileName);
          } else {
             System.out.println
                ("\tFile Name: " + fileName +
                 "\tSize: "+fc.fileSize());
          }
       }
       fc.close();
    } catch (IOException ioe) {
       System.out.println(ioe.getMessage());
    }
 }

 public void showFile(String fileName) {
    try {
       FileConnection fc = (FileConnection)
          Connector.open("file:///CFCard/" + fileName);
       if(!fc.exists()) {
          throw new IOException("File does not exist");
       }
       InputStream is = fc.openInputStream();
       byte b[] = new byte[1024];
       int length = is.read(b, 0, 1024);
       System.out.println
          ("Content of "+fileName + ": "+ new String(b, 0, length));
    } catch (Exception e) {
    }
 }



}

⌨️ 快捷键说明

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