import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import com.tlt.util.Consts;
import com.tlt.util.Util;
import com.tlt.view.AppCanvas;
import com.tlt.view.CommonObject;
public class BallList extends CommonObject {
AppCanvas appCanvas;
// private Image[] balls;
// int ballWidth;
int ballCount;//小球个数
public boolean[] selected;//选中的小球
public boolean isLastLine,isFirstLine;//红框是否在最后一行或第一行
public int selectedCount;//选中的小球个数
public int min,max;//数字从min开始到max结束
public int index=0;//总索引index=indexY*col+indexX
public int indexX,indexY;
public int col=10,row;
public boolean isFocus=false;//是否显示小红框
public BallList(AppCanvas appCanvas,int min,int max){
this.appCanvas=appCanvas;
this.min=min;
this.max=max;
this.ballCount=max-min+1;
// balls=new Image[3];
// balls[0]=Util.getImage("/ball.png");
// balls[1]=Util.getImage("/ball_red.png");
// balls[2]=Util.getImage("/ball_blue.png");
// ballWidth=balls[0].getWidth();
}
public void close() {
// TODO Auto-generated method stub
// balls=null;
}
public void init() {
// TODO Auto-generated method stub
//根据屏幕大小判断一行显示小球的个数
if(Consts.SCREEN_HEIGHT>=300){
col=10;
}
else if(Consts.SCREEN_HEIGHT>=196){
col=8;
}
else if(Consts.SCREEN_HEIGHT>=128){
col=4;
}
//计算行数
if(ballCount%col==0){
row=ballCount/col;
}else{
row=ballCount/col+1;
}
resetPosition();
selected=new boolean[ballCount];
}
/**
* 设置红框的位置
*/
public void resetPosition(){
index=5;
indexX=5;
indexY=0;
}
public void keyPressed(int keyCode) {
// TODO Auto-generated method stub
switch (keyCode) {
case Consts.KEY_LS:
break;
case Consts.KEY_OK:
selected[index]=!selected[index];
if(selected[index]){
selectedCount++;
}else{
selectedCount--;
}
break;
case Consts.KEY_RS:
break;
case Consts.KEY_UP:
index-=col;
indexY--;
if(indexY<=0){
isFirstLine=true;
}
isLastLine=false;
break;
case Consts.KEY_DOWN:
index+=col;
indexY++;
if(index>ballCount-1){
index-=col;
indexY--;
isLastLine=true;
}
isFirstLine=false;
break;
case Consts.KEY_LEFT:
if(index>0){
index--;
indexX--;
if((index+1)%col==0){
indexY--;
indexX=col-1;
// if(indexY<0){
// indexY=0;
// indexX=0;
// }
}
}
break;
case Consts.KEY_RIGHT:
if(index<ballCount-1){
index++;
indexX++;
if(index%col==0){
indexY++;
indexX=0;
}
}
break;
}
}
public void keyReleased(int keyCode) {
// TODO Auto-generated method stub
}
public void paint(Graphics g){
}
public void paint(Graphics g,int x, int y) {
// TODO Auto-generated method stub
drawBall(g,x,y);
}
public void run() {
// TODO Auto-generated method stub
}
private void drawBall(Graphics g,int x, int y){
int j=0;
int gapX=0;
if((Consts.SCREEN_WIDTH-col*ballWidth)%(col+1)==0){
gapX=(Consts.SCREEN_WIDTH-col*ballWidth)/(col+1);
}else{
gapX=(Consts.SCREEN_WIDTH-col*ballWidth)/(col+1)+1;
}
int gap=ballWidth+gapX;
for(int i=0;i<ballCount;i++){
g.drawImage(selected[i]?balls[1]:balls[0], x+gapX+gap*i-j*(col*gap), y+j*gap, Graphics.TOP|Graphics.LEFT);
g.setColor(selected[i]?0xffffff:0x0);
g.drawString((i+min)+"", x+gapX+gap*i-j*(col*gap)+(ballWidth>>1), y+j*gap+(ballWidth>>1)+8, Graphics.BASELINE|Graphics.HCENTER);
if((i+1)%col==0){
j++;
}
}
//小红框
if(isFocus){
g.setColor(0xff0000);
g.drawRect(x+gapX+indexX*gap, y+indexY*gap, ballWidth-1, ballWidth-1);
}
}
}
![点击查看原始大小图片](http://dl2.iteye.com/upload/attachment/0029/9671/d23c8472-a77d-3304-b3e4-a6b3ef9d46d7-thumb.png)
- 大小: 9.6 KB