📄 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;
/** 构造函数 */
DrawCanvas(ResourceRead resourceRead) {
this.resourceRead = resourceRead;
//取得宽度与高度
screenWidth = getWidth();
screenHeight = getHeight();
//读取数据文件
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);
}
/**
* 描绘用的方法。通常不会直接由自己类来调用
* @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);
}
/** 指令的事件发生时所调用的方法 */
public void commandAction(Command c, Displayable s) {
if(c == exitCmd) {//EXIT指令
//调用NetworkTestImg类的doExit方法,并结束MIDlet
resourceRead.doExit();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -