📄 figurechoicecanvas.java
字号:
/**
*
* 人物头像选择界面
*
*
*/
package card;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Image;
import card.stringtable.ImageStringTable;
import card.stringtable.TextString;
import java.io.IOException;
public class FigureChoiceCanvas extends Canvas implements Runnable{
Display display = null;
Image imgFigureChoice = null;
Image imgLeftArrow = null;
Image imgRightArrow = null;
Thread thread = null;
CardGameCanvas cvsCardGame = null;
final int COLOR_SCREEN = 0x3A6EA5; //背景色
final int COLOR_BLACK = 0x000000; //黑色
final int COLOR_WHITE = 0xFFFFFF; //白色
final int SCREEN_WIDTH = 176; //屏幕宽度
final int SCREEN_HEIGHT = 208; //屏幕高度
final int FIGURE_WIDTH = 32; //头像宽度
final int FIGURE_HEIGHT = 32; //头像高度
int iFigureChoice; //选择的头像
int iChoice; //画的头像
int iFigureMoveX; //移动的像素
boolean bChoice; //是否已选择
boolean bMoveLeft; //是否往左移
boolean bFinishChoice; //是否结束选择
public FigureChoiceCanvas(Display display) {
this.setFullScreenMode(true);
this.display = display;
iFigureChoice = 0;
iChoice = 0;
iFigureMoveX = 0;
bChoice = false;
bMoveLeft = true;
bFinishChoice = false;
}
public void init () {
try {
imgLeftArrow = Image.createImage(ImageStringTable.getImgLeftArrowString());
imgRightArrow = Image.createImage(ImageStringTable.getImgRightArrowString());
imgFigureChoice = Image.createImage(ImageStringTable.getImgFigureMapSmallString());
thread = new Thread(this);
thread.start();
}catch (IOException e) {
ErrorHandling ehIOException = new ErrorHandling(display, ErrorHandling.IMGNOTFOUND);
}
}
protected synchronized void keyPressed(int keyCode) {
int iKey = getGameAction(keyCode);
if (iKey == Canvas.LEFT) { //左移,显示上一个头像
iChoice = iFigureChoice;
bMoveLeft = true;
bChoice = true;
if (iFigureChoice > 0) {
iFigureChoice--;
}else {
iFigureChoice = 3;
}
}else if (iKey == Canvas.RIGHT) { //右移,显示下一个头像
iChoice = iFigureChoice;
bMoveLeft = false;
bChoice = true;
if (iFigureChoice < 3) {
iFigureChoice++;
}else {
iFigureChoice = 0;
}
}else if (iKey == Canvas.FIRE) { //确定,以当前头像进入游戏
bFinishChoice = true;
if (cvsCardGame == null) {
cvsCardGame = new CardGameCanvas(display, iFigureChoice);
}
display.setCurrent(cvsCardGame);
cvsCardGame.init();
}
}
public void paint(Graphics g) {
g.setColor(COLOR_SCREEN);
g.fillRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setClip(SCREEN_WIDTH / 2 - FIGURE_WIDTH / 2 - iFigureMoveX, SCREEN_HEIGHT / 2 - FIGURE_HEIGHT / 2, FIGURE_WIDTH, FIGURE_HEIGHT);
g.drawImage(imgFigureChoice, SCREEN_WIDTH / 2 - FIGURE_WIDTH / 2 - iFigureMoveX, SCREEN_HEIGHT / 2 - FIGURE_HEIGHT / 2 - iChoice * FIGURE_HEIGHT, Graphics.TOP | Graphics.LEFT);
g.setClip(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
g.setColor(COLOR_WHITE);
g.drawString(TextString.getTxtFigureChoiceString(), SCREEN_WIDTH / 2, 50, Graphics.BASELINE | Graphics.HCENTER);
g.setColor(COLOR_BLACK);
g.drawImage(imgLeftArrow, SCREEN_WIDTH / 2 - 30, SCREEN_HEIGHT / 2 + 9, Graphics.BOTTOM | Graphics.HCENTER);
g.drawImage(imgRightArrow, SCREEN_WIDTH / 2 + 30, SCREEN_HEIGHT / 2 + 9, Graphics.BOTTOM | Graphics.HCENTER);
}
public void run() {
while (!bFinishChoice) {
if (bChoice) {
for (int i = 0; i<3; i++) {
if (bMoveLeft) {
iFigureMoveX += SCREEN_WIDTH /6;
}else {
iFigureMoveX -= SCREEN_WIDTH /6;
}
try {
Thread.sleep(100);
}catch (Exception e) {
}
repaint();
}
iChoice = iFigureChoice;
bChoice = false;
iFigureMoveX = 0;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -