import javax.microedition.lcdui.*;
import java.io.*;
//リソースの読み込み(キャンバス)
public class ResCanvas extends Canvas {
String info="?";
//コンストラクタ
ResCanvas() {
try {
info=new String(readRes("/test.txt"));
} catch (Exception e) {
info=e.toString();
}
}
//描画
public void paint(Graphics g) {
g.setColor(255,255,255);
g.fillRect(0,0,getWidth(),getHeight());
g.setColor(0,0,0);
g.drawString(info,0,0,g.LEFT|g.TOP);
}
//リソースの読み込み
private byte[] readRes(String name) throws Exception {
int size;
byte[] w=new byte[10240];
InputStream in =null;
ByteArrayOutputStream out=null;
try {
//リソースと接続
in=getClass().getResourceAsStream(name);
out=new ByteArrayOutputStream();
//リソースの読み込み
while (true) {
size=in.read(w);
if (size<=0) break;
out.write(w,0,size);
}
out.close();
in.close();
return out.toByteArray();
} catch (Exception e) {
try {
if (in !=null) in.close();
if (out!=null) out.close();
} catch (Exception e2) {
}
throw e;
}
}
}
|