import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.midlet.MIDlet;
public class MainCanvas extends Canvas {
private MIDlet midlet = null;
private Display display = null;
private int selected ;
private int Width = -1 ;
private int Height = -1 ;
public static final int[] a = {0,1,2,3};
public static final String[] b = {"选项1","选项2","选项3","选项4"};
public MainCanvas(MIDlet m, Display d) {
this.midlet = m;
display = d;
selected = a[0];
Font f = Font.getDefaultFont();
for(int i=0;i<b.length;i++){
int c = f.stringWidth(b[i]);
if(c>Width){
Width = c ;
}
}
Width = Width + 2*8;
Height = f.getHeight() + 2*4 ;
}
protected void paint(Graphics g) {
//// 清屏幕
// int color = g.getColor();
// g.setColor(0xFFFFFF);
// g.fillRect(0, 0,Width, Height);
// g.setColor(color);
//// 计算
// int rectWidth = Width ;
// int rectHeight = Height * b.length ;
// int x = (getWidth() - rectWidth)/2;
// int y = (getHeight() - rectHeight )/2;
//
// g.drawRect(x, y, getWidth(), getHeight());
// for(int j=0;j<b.length;j++){
// if(selected ==j){
// g.setColor(0x6699cc);
// g.fillRect(x+1, y+j*Height+1, rectWidth-1, Height-1);
// g.setColor(color);
// }
// g.drawString(b[j], x+8, y+j*Height+4, Graphics.LEFT|Graphics.TOP);
// }
int color = g.getColor();
g.setColor(0xFFFFFF);
g.fillRect(0, 0, getWidth(), getHeight());
g.setColor(color);
// 计算整个菜单的高度,宽度和(x,y)
int rectWidth = Width;
int rectHeight = Height * b.length;
int x = (getWidth() - rectWidth) / 2;
int y = (getHeight() - rectHeight) / 2;
// 画矩形
g.drawRect(x, y, rectWidth, rectHeight);
for (int i = 1; i < b.length; i++) {
g.drawLine(x, y + Height * i, x + rectWidth, y + Height
* i);
}
// 画菜单选项,并根据selected的值判断焦点
for (int j = 0; j < b.length; j++) {
if (selected == j) {
g.setColor(0x6699cc);
g.fillRect(x + 1, y + j * Height + 1, rectWidth - 1,
Height - 1);
g.setColor(color);
}
g.drawString(b[j], x + 8, y + j * Height + 4,
Graphics.LEFT | Graphics.TOP);
}
}
public void keyPressed(int keyCode){
int action = this.getGameAction(keyCode);
switch(action){
// case Canvas.FIRE:
// printLabel(selected);
// //display.setCurrent(this);
//// repaint();
// break;
case Canvas.DOWN:
selected = (selected+1)%4;
// repaint();
break;
case Canvas.UP:{
if(--selected<0){
selected= selected+4;
}
// repaint();
break;
}
default:
break;
}
repaint();
serviceRepaints();
}
private void printLabel(int selected) {
System.out.println(b[selected]);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
简单的游戏选择界面 使用Canvas // 计算整个菜单的高度,宽度和(x,y) int rectWidth = Width; int rectHeight = Height * b.length; int x = (getWidth() - rectWidth) / 2; int y = (getHeight() - rectHeight) / 2; // 画矩形 g.drawRect(x, y, rectWidth, rectHeight); for (int i = 1; i < b.length; i++) { g.drawLine(x, y + Height * i, x + rectWidth, y + Height * i); } // 画菜单选项,并根据selected的值判断焦点 for (int j = 0; j < b.length; j++) { if (selected == j) { g.setColor(0x6699cc); g.fillRect(x + 1, y + j * Height + 1, rectWidth - 1, Height - 1); g.setColor(color); } g.drawString(b[j], x + 8, y + j * Height + 4, Graphics.LEFT | Graphics.TOP); }
资源详情
资源评论
资源推荐
收起资源包目录
Test.rar (11个子文件)
Test
Test
.project 536B
bin
MineMidlet.class 1010B
MainCanvas.class 3KB
.settings
org.eclipse.jdt.core.prefs 208B
src
MineMidlet.java 1KB
MainCanvas.java 3KB
verified
libs
classes
MineMidlet.class 1010B
MainCanvas.class 3KB
phoneBook
Test.jad 209B
.eclipseme 219B
res
.classpath 242B
共 11 条
- 1
mango31117
- 粉丝: 2
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1