?? drawcanvas.java
字號:
/*
* DrawCanvas.java
*
* Copyright 2001 SkyArts. All Rights Reserved.
*/
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.io.*;
/**
* 從資源中讀出與描繪用的Canvas類
*
* @author Hideki Yonekawa
* @version 1.0
*/
class DrawCanvas extends Canvas implements CommandListener {
/** 存放畫面寬度的變量 */
private int screenWidth;
/** 存放畫面高度的變量 */
private int screenHeight;
/** 存放ResourceRead對象--MIDlet的變量 */
private ResourceRead resourceRead;
/** Exit指令變量 */
private Command exitCmd = new Command("Exit", Command.SCREEN, 5);
/** 存放畫面顯示用字符串的變量 */
private String drawSt;
/** 構(gòu)造函數(shù) */
DrawCanvas(ResourceRead resourceRead) {
this.resourceRead = resourceRead;
//取得寬度與高度
screenWidth = getWidth();
screenHeight = getHeight();
//讀取數(shù)據(jù)文件
InputStreamReader reader = null;
try {
InputStream in = getClass().getResourceAsStream("/resouce.txt");
if(in != null) {
reader = new InputStreamReader(in);
char[] readChar = new char[128];
int readLength;
StringBuffer readStBuffer = new StringBuffer();
while((readLength = reader.read(readChar, 0, readChar.length)) > -1) {
readStBuffer.append(new String(readChar, 0, readLength));
}
drawSt = readStBuffer.toString();
} else {
drawSt = "Resouce not found.";
}
}catch (java.io.IOException ex) {
ex.printStackTrace();
}finally {
if(reader != null) {
try {
reader.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
/*
DataInputStream din = null;
try {
InputStream in = getClass().getResourceAsStream("/resouce.txt");
if(in != null) {
din = new DataInputStream(in);
drawSt = din.readUTF();
} else {
drawSt = "Resouce not found.";
}
}catch (java.io.IOException ex) {
ex.printStackTrace();
}finally {
if(din != null) {
try {
din.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
*/
/*
DataInputStream din = null;
try {
InputStream in = getClass().getResourceAsStream("/resouce.txt");
if(in != null) {
din = new DataInputStream(in);
byte[] readByte = new byte[128];
int readLength;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
while((readLength = din.read(readByte)) > -1) {
byteStream.write(readByte, 0, readLength);
}
drawSt = new String(byteStream.toByteArray());
readByte = byteStream.toByteArray();
for(int i=0; i < readByte.length; i++) {
System.out.println(Integer.toHexString( (int)readByte[i] ));
}
} else {
drawSt = "Resouce not found.";
}
}catch (java.io.IOException ex) {
ex.printStackTrace();
}finally {
if(din != null) {
try {
din.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
*/
/*
InputStream in = null;
try {
in = getClass().getResourceAsStream("/resouce.txt");
if(in != null) {
int readInt;
// StringBuffer readBuffer = new StringBuffer();
byte[] readBytes = new byte[128];
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
while((readInt = in.read(readBytes)) > -1) {
// readBuffer.append((char)readInt);
byteOut.write(readBytes, 0, readInt);
}
// drawSt = readBuffer.toString();
// drawSt = byteOut.toString();
drawSt = new String(byteOut.toString());
} else {
drawSt = "Resouce not found.";
}
}catch (java.io.IOException ex) {
ex.printStackTrace();
}finally {
if(in != null) {
try {
in.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
*/
/*
InputConnection conn = null;
try {
// String url = "jar://" + rsName + "/" + fileName;
String url = "jar://" + "resouce.txt";
System.out.println("----- jar: connection URL: " + url);
conn = ( InputConnection ) Connector.open( url, Connector.READ );
InputStream in = conn.openInputStream();
System.out.println("----- jar: connecting, expected byte count = " );
byte[] readBytes = new byte[128];
int readInt;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
while((readInt = in.read(readBytes)) > -1) {
// readBuffer.append((char)readInt);
byteOut.write(readBytes, 0, readInt);
}
in.close();
drawSt = new String(byteOut.toString());
/*
} else {
drawSt = "Resouce not found.";
}
*/
/*
}catch (java.io.IOException ex) {
ex.printStackTrace();
}finally {
if(conn != null) {
try {
conn.close();
}catch(Exception e) {
e.printStackTrace();
}
}
}
*/
// System.out.println(getClass().getResourceAsStream("/resouce.txt"));
addCommand(exitCmd);
//登錄Command listeners
setCommandListener(this);
}
/**
* 描繪用的方法。通常不會直接由自己類來調(diào)用
* @param g Graphics對象
*/
protected void paint(Graphics g) {
//背景涂白
g.setColor(0x00FFFFFF);
g.fillRect(0, 0, screenWidth, screenHeight);
g.setColor(0x00000000);
g.drawString(drawSt, 0, 0, Graphics.TOP|Graphics.LEFT);
}
/** 指令的事件發(fā)生時所調(diào)用的方法 */
public void commandAction(Command c, Displayable s) {
if(c == exitCmd) {//EXIT指令
//調(diào)用NetworkTestImg類的doExit方法,并結(jié)束MIDlet
resourceRead.doExit();
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -