📄 imagetest.java
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.IOException;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import javax.microedition.rms.*;
public class ImageTest extends MIDlet implements CommandListener
{
public static ImageTest instance;
public Image BgImage[] = new Image[3];
private Command cmdExit;
private Command pingbao;
public ImageCanvas canvas;
private ChooseBgCanvas choosebg;
private Display display;
private RecordStore optionsStore;
private Alert alert;
private final String optionsName = "Image";
public int BgIndex;
public ImageTest()
{
instance = this;
cmdExit = new Command("Exit", Command.SCREEN, 2);
pingbao = new Command("pingbao", Command.SCREEN, 2);
canvas = new ImageCanvas();
choosebg = new ChooseBgCanvas();
try {
BgImage[0]= Image.createImage("/bg1.png");
BgImage[1]= Image.createImage("/bg2.png");
BgImage[2]= Image.createImage("/bg3.png");
} catch (Exception e) {}
}
public void startApp()
{
canvas.addCommand(cmdExit);
canvas.setCommandListener(this);
display = Display.getDisplay(this);
display.setCurrent(canvas);
openOptions();
restoreOptions();
choosebg.SetBg(BgIndex);
}
public void exit()
{ destroyApp(true);
notifyDestroyed();
}
public void pauseApp()
{
saveOptions();
}
public void destroyApp(boolean unconditional)
{
saveOptions();
closeOptions();
}
public void commandAction(Command command, Displayable
screen)
{
if (command == cmdExit)
{
destroyApp(true);
notifyDestroyed();
}
}
public void openOptions() {
//open the record store
try {
optionsStore = RecordStore.openRecordStore(optionsName, false);
} catch (Exception e) {
alert.setString("Could not access options storage");
display.setCurrent(alert);
optionsStore = null;
}
}
public void saveOptions() {
try {
RecordStore.deleteRecordStore(optionsName);
} catch (Exception e) {}
try {
//if recordstore have existed open a record store ,if not create
optionsStore = RecordStore.openRecordStore(optionsName, true);
ByteArrayOutputStream bufOut = new ByteArrayOutputStream();
DataOutputStream dataOut = new DataOutputStream(bufOut);
dataOut.writeInt(BgIndex);
dataOut.flush();
byte[] buffer = bufOut.toByteArray();
optionsStore.addRecord(buffer, 0, buffer.length);
} catch (Exception e) {
}
}
public void restoreOptions() {
byte[] buf = null;
try {
RecordEnumeration rsEmu =
optionsStore.enumerateRecords(null, null, false);
if (rsEmu.hasNextElement()) {
buf = rsEmu.nextRecord();
}
} catch(Exception e) {}
// close the record store
try {
optionsStore.closeRecordStore();
} catch (Exception e) {}
if (buf == null) {
BgIndex = 0;
}
try {
ByteArrayInputStream bufIn = new ByteArrayInputStream(buf);
DataInputStream in = new DataInputStream(bufIn);
BgIndex = in.readInt();
bufIn = null;
in = null;
} catch (Exception e) {}
}
public void closeOptions() {
if (optionsStore != null) {
try {
optionsStore.closeRecordStore();
optionsStore = null;
} catch (RecordStoreException ex) {
alert.setString("Could not close options storage");
display.setCurrent(alert);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -