转来主义,自己慢慢看Canvas

public void paint(Graphics g){ 

//清除屏幕 

int color = g.getColor(); 

g.setColor(0xFFFFFF); 

g.fillRect(0,0,getWidth(),getHeight()); 

g.setColor(color); 

//计算整个菜单的高度,宽度和(x,y) 

int rectWidth = preferWidth; 

int rectHeight = preferHeight * LABELS.length; 

int x = (getWidth()-rectWidth)/2; 

int y = (getHeight()-rectHeight)/2; 

//画矩形 

g.drawRect(x,y,rectWidth,rectHeight); 

for(int i = 1;i<LABELS.length;i++){ 

g.drawLine(x,y+preferHeight*i,x+rectWidth,y+preferHeight*i); 

} 

//画菜单选项,并根据selected的值判断焦点 

for(int j = 0;j<LABELS.length;j++){ 

if(selected == j){ 

g.setColor(0x6699cc); 

g.fillRect(x+1,y+j*preferHeight+1,rectWidth-1,preferHeight-1); 

g.setColor(color); 

} 

g.drawString(LABELS[j],x+8,y+j*preferHeight+4,Graphics.LEFT|Graphics.TOP); 

} 

} 
 第二个重要的问题是:焦点的切换,在高级UI类中,这是不需要我们处理的。但是使用Canvas制作菜单需要自己来处理焦点的移动,这里我们定义一个int类型变量selected,来记录焦点所在的菜单条目位置,也就是选择的索引。当用户按键的时候,我们在keyPressed()方法中判断用户的移动方向,对selected进行相关的加减运算,然后repaint()整个屏幕即可。
public void keyPressed(int keyCode){ 

//根据用户输入更新selected的值,并重新绘制屏幕 

int action = this.getGameAction(keyCode); 

switch(action){ 

case Canvas.FIRE: 

printLabel(selected); 

break; 

case Canvas.DOWN: 

selected = (selected+1)%4; 

break; 

case Canvas.UP:{ 

if(--selected < 0){ 

selected+=4; 

} 

break; 

} 

default: 

break; 

} 

repaint(); 

serviceRepaints(); 

} 
 这样我们就制作出了一个基本的菜单,你还可以发挥想象给被选中的菜单增加动画效果。MenuCanvas的代码如下所示:
package com.j2medev.chapter3; 

import javax.microedition.lcdui.*; 

public class MenuCanvas extends Canvas{ 

//selected变量标记了焦点位置 

private int selected = 0; 

private int preferWidth = -1; 

private int preferHeight = -1; 

public static final int[] OPTIONS = {0,1,2,3}; 

public static final String[] LABELS={"New Game","Setttings","High Scores","Exit"}; 

public MenuCanvas() { 

selected = OPTIONS[0]; 

//计算菜单选项的长度和高度值 

Font f = Font.getDefaultFont(); 

for(int i = 0;i<LABELS.length;i++){ 

int temp = f.stringWidth(LABELS[i]); 

if(temp > preferWidth){ 

preferWidth = temp; 

} 

} 

preferWidth = preferWidth + 2*8; 

preferHeight = f.getHeight()+2*4; 

} 

public void paint(Graphics g){ 

//清除屏幕 

int color = g.getColor(); 

g.setColor(0xFFFFFF); 

g.fillRect(0,0,getWidth(),getHeight()); 

g.setColor(color); 

//计算整个菜单的高度,宽度和(x,y) 

int rectWidth = preferWidth; 

int rectHeight = preferHeight * LABELS.length; 

int x = (getWidth()-rectWidth)/2; 

int y = (getHeight()-rectHeight)/2; 

//画矩形 

g.drawRect(x,y,rectWidth,rectHeight); 

for(int i = 1;i<LABELS.length;i++){ 

g.drawLine(x,y+preferHeight*i,x+rectWidth,y+preferHeight*i); 

} 

//画菜单选项,并根据selected的值判断焦点 

for(int j = 0;j<LABELS.length;j++){ 

if(selected == j){ 

g.setColor(0x6699cc); 

g.fillRect(x+1,y+j*preferHeight+1,rectWidth-1,preferHeight-1); 

g.setColor(color); 

} 

g.drawString(LABELS[j],x+8,y+j*preferHeight+4,Graphics.LEFT|Graphics.TOP); 

} 

} 

public void keyPressed(int keyCode){ 

//根据用户输入更新selected的值,并重新绘制屏幕 

int action = this.getGameAction(keyCode); 

switch(action){ 

case Canvas.FIRE: 

printLabel(selected); 

break; 

case Canvas.DOWN: 

selected = (selected+1)%4; 

break; 

case Canvas.UP:{ 

if(--selected < 0){ 

selected+=4; 

} 

break; 

} 

default: 

break; 

} 

repaint(); 

serviceRepaints(); 

} 

//showNotify()在paint()之前被调用 

public void showNotify(){ 

System.out.println("showNotify() is called"); 

} 

private void printLabel(int selected){ 

System.out.println(LABELS[selected]); 

} 

} 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值