?? canvas_stageselect.java
字號:
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
// Download by http://www.codefans.net
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
public class Canvas_StageSelect extends GameCanvas
{
MIDlet_Main mainMIDlet;
Graphics g;
Thread t;
static boolean isThreadCancel;
Font font=Font.getFont(0,0,16);
short selRectX=(short) ((Class_Camera.width-font.stringWidth(Class_Map.name))/2-2);
short selRectY=20;
//選擇框變化記數
private byte rectChangeCount;
public Canvas_StageSelect(MIDlet_Main mid)
{
super(false);
setFullScreenMode(true);
mainMIDlet=mid;
g=getGraphics();
save();
load();
render();
t=new Thread()
{
public void run()
{
if(!isThreadCancel)
{
long start=System.currentTimeMillis();
render();
long end=System.currentTimeMillis();
short time=(short)(end-start);
try {
if(time<100)
Thread.sleep(100-time);
} catch (InterruptedException e) {
e.printStackTrace();
}
mainMIDlet.dis.callSerially(this);
}
}
};
t.start();
}
//處理選擇框
public void showSelRect()
{
if(rectChangeCount%2==0)
{
g.setColor(255,255,255);
g.drawRoundRect(selRectX,selRectY,font.stringWidth(Class_Map.name)+4,font.getHeight(),2,2);
}
}
public void render()
{
//選擇框變化記數
rectChangeCount++;
g.setFont(font);
g.setColor(50,125,255);
g.fillRect(0,0,Class_Camera.width,Class_Camera.height);
showSelRect();
for(byte i=1;i<Class_Map.IDMax+2;i++)
{
g.setColor(130,245,255);
g.drawString("第"+i+"關",(Class_Camera.width-font.stringWidth("第"+i+"關"))/2,i*20,0);
}
g.drawString("確定",5,Class_Camera.height-20,0);
g.drawString("返回菜單",Class_Camera.width-60,Class_Camera.height-20,0);
flushGraphics();
}
protected void keyPressed(int keyCode)
{
switch(keyCode)
{
case -1:
case 50:
{
if(selRectY==20)
selRectY=(short) (Class_Map.IDMax*20+20);
else
selRectY-=20;
break;
}
case -2:
case 56:
{
if(selRectY==(short) (Class_Map.IDMax*20+20))
selRectY=20;
else
selRectY+=20;
break;
}
case -5:
case -6:
case 53:
{
Class_Map.ID=(byte) (selRectY/20-1);
Canvas_Main.isThreadCancel=false;
mainMIDlet.dis.setCurrent(new Canvas_Main(mainMIDlet));
isThreadCancel=true;
System.gc();
break;
}
case -7:
Canvas_Menu.isThreadCancel=false;
mainMIDlet.dis.setCurrent(new Canvas_Menu(mainMIDlet));
isThreadCancel=true;
break;
}
}
public static void save()
{
try {
RecordStore rs=RecordStore.openRecordStore("關卡",true);
ByteArrayOutputStream bo=new ByteArrayOutputStream();
DataOutputStream out=new DataOutputStream(bo);
out.writeByte(Class_Map.IDMax);
byte[] save=bo.toByteArray();
if(rs.getNumRecords()>0)
rs.setRecord(1,save,0,save.length);
else
rs.addRecord(save,0,save.length);
out.close();
rs.closeRecordStore();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void load()
{
try {
RecordStore rs=RecordStore.openRecordStore("關卡",false);
byte[] load=rs.getRecord(1);
ByteArrayInputStream bi=new ByteArrayInputStream(load);
DataInputStream in=new DataInputStream(bi);
Class_Map.IDMax=in.readByte();
in.close();
rs.closeRecordStore();
} catch (RecordStoreFullException e) {
e.printStackTrace();
} catch (RecordStoreNotFoundException e) {
e.printStackTrace();
} catch (RecordStoreException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -