J2ME 拼图游戏 快速开发 全过程 (六)——图片设置界面设计

代码:

package cn.edu.xtu.tilepuzzle.ui;


import java.io.IOException;
import java.util.Vector;


import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;


import cn.edu.xtu.tilepuzzle.GameDB;
import cn.edu.xtu.tilepuzzle.contorller.MainUIController;
import cn.edu.xtu.tilepuzzle.model.BoardModel;
import cn.edu.xtu.tilepuzzle.model.ClassListItem;


/**
 * @author sky
 * 
 */
public class PhotoListUI extends List implements CommandListener {
private Vector itemList = new Vector();
private ClassListItem currentSelectedObject = null;
private int currentSelectedIndex = -1;
private Vector appearList = new Vector();

Command ok;
Command cancel;

MainUIController mainUIController;
    Display display;
    BoardModel boardModel;
    Displayable displayable;


public PhotoListUI(String title, int type, Vector itemList,MainUIController mainUIController,BoardModel boardModel,Display display,Displayable displayable) {
super(title, type);
this.boardModel=boardModel;
this.display=display;
this.displayable=displayable;
this.mainUIController=mainUIController;

this.itemList = itemList;
ok=new Command("预览",Command.OK,0);
cancel=new Command("返回",Command.CANCEL,1);
this.addCommand(ok);
this.addCommand(cancel);
this.setCommandListener(this);
try {
LoadList();
} catch (IOException e) {
e.printStackTrace();
}
}


public void appendItem(ClassListItem item, Image icon, boolean ifSub) {
appearList.addElement(item);
//System.out.println("Add current display list:" + item);
if (!ifSub) {
this.append("  " + item.getLabel(), icon);
} else {
this.append("  " + item.getLabel(), icon);
}
}


public void Init() {
int count = this.size();
for (int i = 0; i < count; i++) {
this.delete(0);
}
this.appearList.removeAllElements();
}


public void LoadList() throws IOException {
Init();
for (int i = 0; i < itemList.size(); i++) {
ClassListItem elItem = (ClassListItem) itemList.elementAt(i);
if (elItem.getType() == ClassListItem.GROUP) {
Image icon = null;
try {
icon = Image.createImage(elItem.getDefaultImagePath());
} catch (IOException e1) {
e1.printStackTrace();
}
/**
* @Debug
* @是被选择的
*/
if (elItem.Ifselected()) {
this.appendItem(elItem, icon, false);
Vector group = (Vector) elItem.getContent();
for (int j = 0; j < group.size(); j++) {
ClassListItem item = (ClassListItem) group.elementAt(j);
Image ic = getIcon(item.iconString);
this.appendItem(item, ic, true);
}
} else {
this.appendItem(elItem, icon, false);
}
}
}
if (this.currentSelectedIndex != -1) {
this.setSelectedIndex(currentSelectedIndex, true);
}
}


public void commandAction(Command arg0, Displayable arg1) {
if(arg0==ok){
this.currentSelectedIndex = this.getSelectedIndex();
this.currentSelectedObject = (ClassListItem) this.appearList.elementAt(currentSelectedIndex);
if (this.currentSelectedObject.getType() != ClassListItem.GROUP) {
ShowPhotoUI shUI=new ShowPhotoUI(
this.currentSelectedObject.getPhotoString(),
this.mainUIController,
this.boardModel,
this.display,
this.display.getCurrent());
this.display.setCurrent(shUI);
}
}else if(arg0==cancel){
//System.out.println("返回");
this.display.setCurrent(mainUIController.mainMenuUI);
}else if (arg0 == List.SELECT_COMMAND) {
System.out.println(arg0);
this.currentSelectedIndex = this.getSelectedIndex();
this.currentSelectedObject = (ClassListItem) this.appearList.elementAt(currentSelectedIndex);
//int indexInItemList = currentSelectedObject.getIndexInItemList();
//System.out.println(" Selected: " + currentSelectedIndex + " " + this.currentSelectedObject + " indexInItemList:" + indexInItemList);

if (this.currentSelectedObject.getType() == ClassListItem.GROUP) {
System.out.println(this.currentSelectedObject.Ifselected());
if (this.currentSelectedObject.Ifselected() == false) {
this.currentSelectedObject.setIfselected(true);
} else {
this.currentSelectedObject.setIfselected(false);
}
this.Init();
try {
this.LoadList();
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (this.currentSelectedObject.Ifselected() == false) {
this.currentSelectedObject.setIfselected(true);
} else {
this.currentSelectedObject.setIfselected(false);
}
this.setSelectedIndex(this.currentSelectedIndex, true);
}
}
}

public Image getIcon(String iconString) {
if(iconString==null || iconString=="" ){
//System.out.println("没有图片。。。");
try {
return Image.createImage( GameDB.orgImageString);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
else{
//System.out.println("有图片。。。。。。");
try {
return Image.createImage(iconString);
} catch (IOException e) {
e.printStackTrace();
try {
return Image.createImage("/img/defaultImg.jpg");
} catch (IOException e1) {
e1.printStackTrace();
return null;
}
}
}
}


public Vector getItemList() {
return itemList;
}


public void setItemList(Vector itemList) {
this.itemList = itemList;
}
}

运行时显示如图:


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值