Source code:
/*
* Created on 2004-6-20
*
* Todo to change the template for this generated file go
* Window-preferences-Java-code style-code templates
*/
Package cn.edu. xtu. tilepuzzle. UI;
Import cn.edu. xtu. tilepuzzle. gamedb;
Import javax. microedition. lcdui. Canvas;
Import javax. microedition. lcdui. Command;
Import javax. microedition. lcdui. commandlistener;
Import javax. microedition. lcdui. displayable;
Import javax. microedition. lcdui. graphics;
Import cn.edu. xtu. tilepuzzle. contorler. mainuicontroller;
/**
* @ Author p2800
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/
Public class mainmenuui extends canvas implements commandlistener {
Static final int lowcolor = 0x000000ff;
Static final int highcolor = 0x00ff0000;
Static final int highbgcolor = 0x00cccc;
Public int width;
Public int height;
Static int startheight;
Static final int spacing = gamedb. monospace_bold_large_font.getheight ();
Static string [] mainmenu;
Private mainuicontroller;
Private int lineheight;
Static int menuidx;
Private command OK;
Private command cancel;
Public mainmenuui (mainuicontroller ){
This. mainuicontroller = mainuicontroller;
// This. setfullscreenmode (true );
This. width = getwidth ();
This. Height = getheight ();
// System. Out. println ("screen width, height:" + width + "," + height );
Mainmenu = gamedb. Menu;
Startheight = gamedb. monospace_bold_large_font.getheight ();
// Startheight = (height-startheight );
Menuidx = 0;
OK = new command ("select", command. OK, 1 );
Cancel = new command ("quit", command. Cancel, 0 );
Lineheight = This. Height/mainmenu. length;
Addcommand (OK );
Addcommand (cancel );
Setcommandlistener (this );
// System. Out. println ("= 2 ");
This. mainuicontroller. setoptions (this );
}
Protected void paint (Graphics g ){
// Todo auto-generated method stub
If (mainuicontroller. getboardmodel (). getgamestate () = gamedb. Playing ){
Mainmenu [0] = "restoring the game ";
Mainmenu [1] = "New Game ";
} Else {
Mainmenu [0] = "New Game ";
Mainmenu [1] = "set image ";
}
G. setcolor (255,255,190 );
G. fillrect (0, 0, width, height );
// System. Out. println ("screen width, height:" + width + "," + height );
For (INT I = 0; I <mainmenu. length; I ++ ){
If (I = menuidx ){
G. setcolor (highbgcolor );
G. fillrect (0, I * lineheight, width, lineheight );
G. setfont (gamedb. monospace_bold_large_font );
G. setcolor (highcolor );
G. drawstring (mainmenu [I], (width-gamedb. monospace_bold_large_font.stringwidth (mainmenu [I])/2, I * lineheight + (lineHeight-GameDB.MONOSPACE_BOLD_LARGE_Font.getHeight ()/2, graphics. left | graphics. top );
} Else {
G. setfont (gamedb. monospace_plain_medium_font );
G. setcolor (lowcolor );
G. drawstring (mainmenu [I], (width-gamedb. monospace_plain_medium_font.stringwidth (mainmenu [I])/2, I * lineheight + (lineHeight-GameDB.MONOSPACE_PLAIN_MEDIUM_Font.getHeight ()/2, graphics. left | graphics. top );
}
/*
If (I = menuidx ){
G. setcolor (highbgcolor );
G. fillrect (0, startheight + (I * gamedb. monospace_bold_large_font.getheight () * 3/2), width, startheight * 3/2 );
G. setfont (gamedb. monospace_bold_large_font );
G. setcolor (highcolor );
G. drawstring (mainmenu [I], (width-gamedb. monospace_bold_large_font.stringwidth (mainmenu [I])/2, startheight + (I * startheight * 3/2) + startheight/4, graphics. left | graphics. top );
} Else {
G. setfont (gamedb. monospace_plain_medium_font );
G. setcolor (lowcolor );
G. drawstring (mainmenu [I], (width-gamedb. monospace_plain_medium_font.stringwidth (mainmenu [I])/2, startheight + (I * startheight * 3/2 + startheight/4), graphics. left | graphics. top );
}*/
}
}
Protected void keypressed (INT code ){
// System. Out. println (CODE );
If (code =-5 | code = 5)
Mainuicontroller. handlecommand_id (menuidx );
If (getgameaction (CODE) = canvas. Up ){
If (menuidx-1> = 0)
Menuidx --;
Else
Menuidx = mainmenu. Length-1;
} Else if (getgameaction (CODE) = canvas. Down ){
If (menuidx + 1 <mainmenu. length)
Menuidx ++;
Else
Menuidx = 0;
} Else;
// System. Out. println ("menuidx =" + (menuidx) + ":" + mainmenu [menuidx]);
Repaint ();
}
Public void commandaction (command C, displayable arg1 ){
// Todo auto-generated method stub
If (C = OK ){
Mainuicontroller. handlecommand_id (menuidx );
} Else if (C = Cancel ){
Mainuicontroller. handlecommand_id (gamedb. command_id_exit );
}
}
}
Interface display