📄 shoping.java
字号:
import java.util.Vector;
import javax.microedition.lcdui.Graphics;
public class Shoping
{
Canvas1 canvas;
Graphics g;
int width,height;
private int tabIndex;
private int xMoney;
public ShopTab shopTable;
synchronized public void prevTab()
{
if(tabIndex>0)
{
tabIndex--;
shopTable=null;
}
}
synchronized public void nextTab()
{
if(tabIndex<1)
{
tabIndex++;
shopTable=null;
}
}
public Shoping(Canvas1 canvas)
{
this.canvas = canvas;
this.g = canvas.g;
this.width = canvas.width;
this.height = canvas.height;
}
public Shoping()
{
}
//定义卖出数量的临时数组,跟shopTable的VC一一对应
private int[] saleCount;
//买卖总价
private int totalPrice;
public int calcTotalPrice()
{
if(tabIndex==0) //买
{
int total=0;
for(int i=0;shopTable!=null && shopTable.getVC()!=null && i<shopTable.getVC().size();i++)
{
Mat mat=(Mat)shopTable.getVC().elementAt(i);
total +=mat.getAmount()*mat.getPrice();
}
totalPrice=total;
}
else if(tabIndex==1) //卖
{
int total=0;
for(int i=0; shopTable != null && shopTable.getVC()!=null && i < shopTable.getVC().size(); i++)
{
Mat mat=(Mat)shopTable.getVC().elementAt(i);
total +=saleCount[i]*mat.getPrice();
}
totalPrice=total;
}
return totalPrice;
}
//获得交易金额,卖是正数,买是负数
public int getTradePrice()
{
if(tabIndex==0) //买
{
return -calcTotalPrice();
}
else if(tabIndex==1) //卖
{
return calcTotalPrice();
}
return 0;
}
public void ShowShopUi(int inShopStatus)
{
int w = width-10;
int h = height-10;
int x=width/2-w/2;
int y=height/2-h/2;
g.setColor(0xDAE6D1);
g.fillRect(x,y,w,h); //对话框背景色
g.setColor(0x618E3F);
g.drawRect(x,y,w,h); //外框
int bLMargin=5;
int bTMargin=5;
int bw = w - 10;
int bh = h - (Style.font.getHeight()+10);
if(shopTable==null)
{
//System.out.println("tabIndex="+tabIndex);
shopTable = new ShopTab();
shopTable.setTabName(new String[]{"买入","卖出"});
shopTable.setTabIndex(tabIndex);
shopTable.setBgColor(0xC7E2B3);
shopTable.setHighLightColor(0xF1F8EC);
shopTable.setPosition(x+bLMargin,y+bTMargin);
shopTable.setW(bw);
shopTable.setH(bh);
if(tabIndex == 0) //买
{
/*
for(int i = 0; i < MatLib.items.length; i++)
{
Mat med = MatLib.getItemByIndex(i);
med.setAmount(0);
shopTable.addElement(med);
}
for(int i = 0; i < MatLib.weapons.length; i++)
{
Mat equip = MatLib.getWeaponByIndex(i);
equip.setAmount(0);
shopTable.addElement(equip);
}
for(int i = 0; i < MatLib.shells.length; i++)
{
Mat guard = MatLib.getShellByIndex(i);
guard.setAmount(0);
shopTable.addElement(guard);
}
for(int i = 0; i < MatLib.caps.length; i++)
{
Mat guard = MatLib.getCapByIndex(i);
guard.setAmount(0);
shopTable.addElement(guard);
}
for(int i = 0; i < MatLib.cloths.length; i++)
{
Mat guard = MatLib.getClothByIndex(i);
guard.setAmount(0);
shopTable.addElement(guard);
}
for(int i = 0; i < MatLib.shoes.length; i++)
{
Mat guard = MatLib.getShoeByIndex(i);
guard.setAmount(0);
shopTable.addElement(guard);
}
for(int i = 0; i < MatLib.jewelrys.length; i++)
{
Mat guard = MatLib.getJewelryByIndex(i);
guard.setAmount(0);
shopTable.addElement(guard);
}
*/
Mat mat=null;
mat = MatLib.getMatByName("草药");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("山药");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("七叶莲");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("佛手");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("鬼切草");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("短剑");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("铜剑");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("玄铁剑");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("木盾");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("皮盾");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("青铜盾");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("布帽");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("皮帽");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("金丝冠");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("布衣");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("皮衣");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("披风");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("布鞋");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("皮靴");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("侠客履");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("护身符");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("珍珠链");
mat.setAmount(0);
shopTable.addElement(mat);
mat = MatLib.getMatByName("苍魂珠");
mat.setAmount(0);
shopTable.addElement(mat);
}else if(tabIndex==1) //卖
{
Vector matVC = null;
matVC = GameMIDlet.mainRole.getBagMatVC(Mat.TYPE_USED);
for(int i=0; matVC != null && i < matVC.size(); i++)
{
Mat _mat = (Mat)matVC.elementAt(i);
//_mat.setStoredAmount(_mat.getAmount()); //记录卖之前的原始数量
//_mat.setSoldAmount(0); //设置卖出数量 初始值
shopTable.addElement(_mat); //add to draw
}
Vector matVC2 = GameMIDlet.mainRole.getBagMatVC(Mat.TYPE_EQUIP);
for(int i=0; matVC2 != null && i < matVC2.size(); i++)
{
Mat _mat = (Mat)matVC2.elementAt(i);
//_mat.setStoredAmount(_mat.getAmount()); //记录卖之前的原始数量
//_mat.setSoldAmount(0); //设置卖出数量 初始值
shopTable.addElement(_mat); //add to draw
}
int matCount1=0;
int matCount2=0;
if(matVC!=null)
{
matCount1=matVC.size();
}
if(matVC2!=null)
{
matCount2=matVC2.size();
}
if(matCount1+matCount2>0)
{
saleCount=new int[matCount1+matCount2];
shopTable.setSaleCount(saleCount);
}
}
}
if(shopTable != null)
{
shopTable.paint(g, inShopStatus);
}
calcTotalPrice();
g.drawString("现金:"+GameMIDlet.mainRole.getMoney(), x+5, y+h-Style.font.getHeight(), Style.LT);
g.drawString("合计:"+totalPrice, x+w-5, y+h-Style.font.getHeight(), Style.RT);
}
//增加买入数量
public void addAmount()
{
Vector vc = shopTable.getVC();
int rowIndex = shopTable.getRowIndex();
if (vc != null && rowIndex >= 0 && rowIndex < vc.size())
{
Mat mt = (Mat)vc.elementAt(rowIndex);
if (shopTable.getTabIndex() == 0) //只有当 是买入的情况
mt.setAmount(mt.getAmount()+1);
}
}
//增加卖出数量
public void addSoldAmount()
{
Vector vc = shopTable.getVC();
int rowIndex = shopTable.getRowIndex();
if (vc != null && rowIndex >= 0 && rowIndex < vc.size())
{
Mat mt = (Mat)vc.elementAt(rowIndex);
if (shopTable.getTabIndex() == 1) //卖出的情况
{
//最大可卖出数量不超过实际拥有量
/*
if (mt.getSoldAmount()+1 > mt.getStoredAmount())
mt.setSoldAmount(mt.getStoredAmount());
else
mt.setSoldAmount(mt.getSoldAmount()+1);
*/
if(saleCount[rowIndex]<mt.getAmount())
{
saleCount[rowIndex]++;
}
}
}
}
//减少买入数量
public void reduAmount()
{
Vector vc = shopTable.getVC();
int rowIndex = shopTable.getRowIndex();
if (shopTable.getTabIndex() == 0)
if (vc!=null && rowIndex>=0 && rowIndex<vc.size())
{
Mat mt = (Mat)vc.elementAt(rowIndex);
if(mt.getAmount()>0)
mt.setAmount(mt.getAmount()-1);
}
}
//减少卖出数量
public void reduSoldAmount()
{
Vector vc = shopTable.getVC();
int rowIndex = shopTable.getRowIndex();
if (shopTable.getTabIndex() == 1)
if (vc != null && rowIndex >= 0 && rowIndex < vc.size())
{
Mat mt = (Mat)vc.elementAt(rowIndex);
/*
if (mt.getSoldAmount() > 0)
mt.setSoldAmount(mt.getSoldAmount() - 1);
*/
if(saleCount[rowIndex]>0)
{
saleCount[rowIndex]--;
}
}
}
//改变买卖总价
/*
public int changeAmt()
{
Vector vc = shopTable.getVC();
int totalAmt = 0;
for (int i=0;vc!=null && i<vc.size(); i++)
{
Mat tmpMat = (Mat)vc.elementAt(i);
if (this.shopTable.getTabIndex()==0) //判断买入卖出状态,0 is buy
totalAmt -= tmpMat.getAmount()* tmpMat.getPrice(); //买入状态
else if (this.shopTable.getTabIndex()==1) //卖出状态
//totalAmt += tmpMat.getSoldAmount()* tmpMat.getPrice();
totalAmt += saleCount[i]* tmpMat.getPrice();
}
return totalAmt;
}
*/
//确定交易
public void changeTrade()
{
Vector vc = shopTable.getVC();
for (int i=0;vc!=null && i<vc.size(); i++)
{
Mat tmpMat = (Mat)vc.elementAt(i);
if (this.shopTable.getTabIndex()==0)
{
//买入状态 加入装备;
tmpMat.setPrice(tmpMat.getPrice()*7/10); //价值打7折
GameMIDlet.mainRole.addMat(tmpMat);
}
else if (this.shopTable.getTabIndex()==1)
{
//卖出状态
//tmpMat.setAmount(tmpMat.getAmount() - tmpMat.getSoldAmount());
tmpMat.setAmount(tmpMat.getAmount() - saleCount[i]);
}
}
//去掉包里的数量为0的物品
GameMIDlet.mainRole.updateBag();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -