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);
}
}
}
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

