📄 covercanvas.java
字号:
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
import javax.microedition.midlet.MIDletStateChangeException;
//import com.nokia.mid.ui.FullCanvas;
public class CoverCanvas extends GameCanvas implements Runnable
{
private Display disp;
private Displayable last;
Graphics g;
boolean isPlay;
int delay;
int width;
int height;
boolean isDrawing;
private Image coverImg,menubk;
private String[] menuStr;
private int menuX;
private int[] menuY;
private int menuIndex;
private boolean haveStartGame;
int windowX,windowY; //可见视窗左上角坐标
int runStatus;
static final int LEFT_PRESSED=0x0004;
static final int RIGHT_PRESSED=0x0020;
static final int UP_PRESSED=0x0002;
static final int DOWN_PRESSED=0x0040;
public CoverCanvas(String title,Display disp,Displayable last)
{
super(false);
this.disp = disp;
this.last = last;
this.setFullScreenMode(true);
init();
}
public void init()
{
this.g = getGraphics();
delay = 1000/60; //1秒500祯
width = this.getWidth();
height = this.getHeight();
if(width>=176)
{
if(height<208)
{
height=208;
}
}
coverImg=Tools.getImage("/pic/cover.png");
menubk=Tools.getImage("/pic/menubk.png");
menuStr=new String[]{"开始游戏","故事背景","开发团队","退 出"};
menuX=176-((menubk.getWidth()-Style.font.stringWidth("开始游戏"))/2+Style.font.stringWidth("开始游戏")+5);
int padding=4;
int menuHeight=Style.font.getHeight()*menuStr.length+padding*(menuStr.length-1)+12;
int menuTop=0;
if(height<=220)
{
menuTop=height-menuHeight;
}
else
{
menuTop=220-menuHeight;
}
menuY=new int[menuStr.length];
for(int i=0;i<menuStr.length;i++)
{
menuY[i]=menuTop+(Style.font.getHeight()+padding)*i;
}
menuIndex=0;
}
boolean ThreadRunning;
public void start()
{
isPlay = true;
if(!ThreadRunning)
{
(new Thread(this)).start();
ThreadRunning=true;
}
}
public void stop()
{
isPlay=false;
}
public void run()
{
while(isPlay)
{
redrawAll();
try {
Thread.sleep(delay);
//wait(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
ThreadRunning = false;
}
public void redrawAll()
{
if(!isDrawing)
{
isDrawing = true;
g.drawImage(coverImg,0,0,Style.LT);
g.drawImage(menubk,menuX-(menubk.getWidth()-Style.font.stringWidth("开始游戏"))/2,menuY[menuIndex]-(menubk.getHeight()-Style.font.getHeight())/2,Style.LT);
g.setFont(Style.font);
for(int i=0;i<menuStr.length;i++)
{
if(menuIndex==i)
g.setColor(0x000000);
else
g.setColor(0xFFFFFF);
g.drawString(menuStr[i],menuX,menuY[i],Style.LT);
}
flushGraphics();
isDrawing=false;
}
}
protected void keyRepeated(int keyCode)
{
}
protected void keyPressed(int keyCode)
{
int gameAction = KeyMapping.getGameKey(keyCode);
if(gameAction == Canvas.UP)
{
if(menuIndex>0)menuIndex--;
}else
if(gameAction == Canvas.DOWN)
{
if(menuIndex<menuStr.length-1)menuIndex++;
}else
if(gameAction == Canvas.FIRE)
{
if(menuIndex==0)
{
//开始游戏
if(!haveStartGame)
{
stop();
GameMIDlet.midlet.startGame();
haveStartGame=true;
}
}else
if(menuIndex==1)
{
//故事背景
}else
if(menuIndex==2)
{
//开发团队
DeveloperCanvas developerCanvas = new DeveloperCanvas("Developer",disp,this);
developerCanvas.start();
disp.setCurrent(developerCanvas);
}else
if(menuIndex==3)
{
//退出
try {
GameMIDlet.midlet.destroyApp(true);
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -