?? menucanvas.java
字號:
package hujkay.menu;
import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import java.lang.*;
import java.util.Timer;
import javax.microedition.*;
import javax.microedition.midlet.*;
import java.util.TimerTask;
import java.io.*;
import hujkay.* ;
public class MenuCanvas extends Canvas implements CommandListener{
//菜單圖片的開始位置
private boolean fistloadpicture = true;
private int picturex , picturey ;
//上下左右空白的地方
private int blankleft , blanktop ;
//當(dāng)前的選項(xiàng)
private int currentchoice ;
private boolean redpicture ;
//當(dāng)前canvas的大小
private int wide,height ;
//圖片變量
private Image image[] = new Image[9] ;
//當(dāng)前是否活躍
private boolean active = true;
//背景的顏色設(shè)成(240,240,0)
private boolean activeerr = false;
//雙緩沖
private Image offcanvas ;
private Timer timer ;
private menuTask menuTask ;
private Command selectcommand = new Command("Select",Command.OK,1) ;
private Command exitcommand = new Command("Exit",Command.EXIT,2) ;
private Block block ;
public void restartnow()
{
active = true ;
activeerr = false ;
currentchoice = 0 ;
redpicture = false ;
currentchoice = 0 ;
redpicture = false ;
}
//構(gòu)造函數(shù)
public MenuCanvas()
{
restartnow( );
timer = new Timer();
menuTask = new menuTask(this);
timer.schedule(menuTask, 400, 400);
this.addCommand(selectcommand) ;
addCommand(exitcommand) ;
this.setCommandListener(this);
}
public MenuCanvas(Block b)
{
this.block = b;
restartnow( );
timer = new Timer();
menuTask = new menuTask(this);
timer.schedule(menuTask, 400, 400);
this.addCommand(selectcommand) ;
addCommand(exitcommand) ;
this.setCommandListener(this);
}
// 讀取圖片數(shù)據(jù)
private boolean readpictures()
{
if(fistloadpicture)
{
fistloadpicture = false ;
try
{
image[0] = Image.createImage("/block/menu/0.png");
image[1] = Image.createImage("/block/menu/1.png");
image[2] = Image.createImage("/block/menu/2.png");
image[3] = Image.createImage("/block/menu/3.png");
image[4] = Image.createImage("/block/menu/4.png");
image[5] = Image.createImage("/block/menu/5.png");
image[6] = Image.createImage("/block/menu/6.png");
image[7] = Image.createImage("/block/menu/7.png");
}
catch(Exception e)
{
return false ;
}
}
return true ;
}
private void Initdata()
{
wide = this.getWidth() ;
height = this.getHeight() ;
if(wide > image[0].getWidth())
{
blankleft = ( wide - image[0].getWidth()) / 2 ;
blanktop = ( height - image[0].getHeight()) / 2 ;
}
}
private void clearcanvas(Graphics g)
{
g.setColor(0,240,240) ;
g.fillRect(0,0,wide,height);
}
private void paintcanvas(Graphics g)
{
//檢查數(shù)據(jù)合法與否
if(currentchoice > 3)currentchoice = 0 ;
if(currentchoice < 0 ) currentchoice = 3 ;
//畫圖,使用自制雙緩沖
Graphics gg = offcanvas.getGraphics();
clearcanvas(gg);
if(redpicture)
{
gg.drawImage(image[currentchoice*2 + 1],blankleft,blanktop,Graphics.LEFT|Graphics.TOP);
redpicture = false ;
}
else
{
gg.drawImage(image[currentchoice*2 ],blankleft,blanktop,Graphics.LEFT|Graphics.TOP);
redpicture = true ;
}
//輸入到當(dāng)前屏幕
g.drawImage(offcanvas,0,0,Graphics.LEFT|Graphics.TOP);
/* try
{
clearcanvas(g) ;
g.drawImage(image[7],0,0,Graphics.LEFT|Graphics.TOP);
}
catch(Exception e)
{
}*/
}
protected void paint(Graphics g) {
// TODO Auto-generated method stub
if(fistloadpicture)
{
readpictures();
Initdata();
offcanvas = Image.createImage(wide,height);
}
if( active)
{
if(!activeerr)
{
paintcanvas(g);
}
else
{
g.drawLine(0,0,100,100);
}
}
else
g.drawRect(0,0,100,100);
}
protected synchronized void keyPressed(int keyCode)
{
switch(this.getGameAction(keyCode))
{
case Canvas.UP:
currentchoice -- ;
break ;
case Canvas.DOWN:
currentchoice ++ ;
break ;
}
redpicture = false ;
repaint();
}
//接口
public void Hide()
{
this.active = false ;
}
public boolean IsOK()
{
return activeerr ;
}
public synchronized void commandAction(Command c, Displayable d)
{
if(c.getLabel().equals("Select"))
{
if( this.currentchoice == 0)//啟動游戲
block.mangeaction(1,0) ;
else if( this.currentchoice == 1)//啟動記錄
block.mangeaction(2,0);
else if(this.currentchoice == 2)//啟動幫助
block.mangeaction(3,0);
else if( this.currentchoice == 3)//退出游戲
block.mangeaction(4,0);
}
else if(c.getLabel().equals("Exit"))
{
block.mangeaction(4,0);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -