Java ME中一个通用的游戏信息框架类
【打印文章】
本类实现游戏菜单,游戏关于和游戏帮助的信息,能够自适应不同的手机
/*游戏中的一些常用的框架*/
import javax.microedition.lcdui.*;
import java.util.Vector;
class showContent
{
int w,h;//屏幕的长和宽
int cw;//字体的宽
int page,maxPage;//帮助信息中的页码/最大页码
int rn;//每行最大字数
int maxR;//每屏最多显示的行数
int cr;//字体行距
Vector about;
Vector help;
public showContent(int wid,int hei,int charw,int charR,String[] aboutText,String[] helpText)
{
about=new Vector(0,1);
help=new Vector(0,1);
w=wid;h=hei;cw=charw;cr=charR;
rn=w/cw;
maxR=(h-20)/cr;
page=0;
for(int m=0;m<aboutText.length;m++)
{
int i=1+(aboutText[m].length()-1)/rn;
for(int p=0;p<i;p++)
{
if(p<i-1)
about.addElement((String)(aboutText[m].substring(p*rn,(p+1)*rn)));
else
about.addElement((String)(aboutText[m].substring(p*rn)));
}
}
for(int m=0;m<helpText.length;m++)
{
int i=1+(helpText[m].length()-1)/rn;
for(int p=0;p<i;p++)
{
if(p<i-1)
help.addElement((String)(helpText[m].substring(p*rn,(p+1)*rn)));
else
help.addElement((String)(helpText[m].substring(p*rn)));
}
}
maxPage=(help.size()-1)/maxR;
}
public void showAbout(Graphics g1,int x,int y)//显示关于
{
g1.setColor(0);
g1.fillRect(0,0,w,h);
g1.setColor(0xffffff);
for(int i=0;i<about.size();i++)
{
g1.drawString((String)(about.elementAt(i)),x,y+i*cr,0);
}
}
/*显示暂停*/
public void drawPause(Graphics g1)
{
g1.setColor(0);
g1.fillRect(w/2-30,h/2-6,60,30);
g1.setColor(0xffffff);
g1.drawRect(w/2-30,h/2-6,60,30);
g1.drawString("暂停中",w/2-20,h/2,0);
}
public void showHelp(Graphics g1,int x,int y)//显示帮助
{
g1.setColor(0);
g1.fillRect(0,0,w,h);
g1.setColor(0xffffff);
for(int i=0;i<maxR;i++)
{
if(page*maxR+i<help.size())
g1.drawString((String)(help.elementAt(page*maxR+i)),x,y+i*cr,0);
}
g1.setColor(0xffff00);
g1.drawString("按上下方向键翻页",20,h-20,0);
}
public void drawMenu(Graphics g1,int x,int y,int row,int w,String[] arrayStr,int no)//显示菜单
{
g1.setColor(0xffff00);
g1.fillRect(x,y,w,arrayStr.length*row);
g1.setColor(0xffffff);
g1.drawRect(x,y,w,arrayStr.length*row);
for(int m=0;m<arrayStr.length;m++)
{
if(no==m)
g1.setColor(0x0000ff);
else
g1.setColor(0);
g1.drawString(arrayStr[m],x+5,y+5+m*row,0);
}
}
}
/*游戏中的一些常用的框架*/
import javax.microedition.lcdui.*;
import java.util.Vector;
class showContent
{
int w,h;//屏幕的长和宽
int cw;//字体的宽
int page,maxPage;//帮助信息中的页码/最大页码
int rn;//每行最大字数
int maxR;//每屏最多显示的行数
int cr;//字体行距
Vector about;
Vector help;
public showContent(int wid,int hei,int charw,int charR,String[] aboutText,String[] helpText)
{
about=new Vector(0,1);
help=new Vector(0,1);
w=wid;h=hei;cw=charw;cr=charR;
rn=w/cw;
maxR=(h-20)/cr;
page=0;
for(int m=0;m<aboutText.length;m++)
{
int i=1+(aboutText[m].length()-1)/rn;
for(int p=0;p<i;p++)
{
if(p<i-1)
about.addElement((String)(aboutText[m].substring(p*rn,(p+1)*rn)));
else
about.addElement((String)(aboutText[m].substring(p*rn)));
}
}
for(int m=0;m<helpText.length;m++)
{
int i=1+(helpText[m].length()-1)/rn;
for(int p=0;p<i;p++)
{
if(p<i-1)
help.addElement((String)(helpText[m].substring(p*rn,(p+1)*rn)));
else
help.addElement((String)(helpText[m].substring(p*rn)));
}
}
maxPage=(help.size()-1)/maxR;
}
public void showAbout(Graphics g1,int x,int y)//显示关于
{
g1.setColor(0);
g1.fillRect(0,0,w,h);
g1.setColor(0xffffff);
for(int i=0;i<about.size();i++)
{
g1.drawString((String)(about.elementAt(i)),x,y+i*cr,0);
}
}
/*显示暂停*/
public void drawPause(Graphics g1)
{
g1.setColor(0);
g1.fillRect(w/2-30,h/2-6,60,30);
g1.setColor(0xffffff);
g1.drawRect(w/2-30,h/2-6,60,30);
g1.drawString("暂停中",w/2-20,h/2,0);
}
public void showHelp(Graphics g1,int x,int y)//显示帮助
{
g1.setColor(0);
g1.fillRect(0,0,w,h);
g1.setColor(0xffffff);
for(int i=0;i<maxR;i++)
{
if(page*maxR+i<help.size())
g1.drawString((String)(help.elementAt(page*maxR+i)),x,y+i*cr,0);
}
g1.setColor(0xffff00);
g1.drawString("按上下方向键翻页",20,h-20,0);
}
public void drawMenu(Graphics g1,int x,int y,int row,int w,String[] arrayStr,int no)//显示菜单
{
g1.setColor(0xffff00);
g1.fillRect(x,y,w,arrayStr.length*row);
g1.setColor(0xffffff);
g1.drawRect(x,y,w,arrayStr.length*row);
for(int m=0;m<arrayStr.length;m++)
{
if(no==m)
g1.setColor(0x0000ff);
else
g1.setColor(0);
g1.drawString(arrayStr[m],x+5,y+5+m*row,0);
}
}
}
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】