📄 showcontent.java
字号:
/*游戏中的框架以及功能类*/
import javax.microedition.lcdui.*;
import java.util.Vector;
import java.util.Random;
import java.io.*;
import javax.microedition.media.*;
import javax.microedition.rms.*;
class showContent extends Canvas
{
int w,h;//屏幕的长和宽
int cw;//字体的宽
int page,maxPage;//帮助信息中的页码/最大页码
int rn;//每行最大字数
int maxR;//每屏最多显示的行数
int cr;//字体行距
int no;
int menuN;//菜单选项数
String pType;//手机型号
public static Random rd=new Random();
Font big=Font.getFont(0,0,0);
Font small=Font.getFont(0,0,8);
Font mini=Font.getFont(0,0,16);
Vector about;
Vector help;
RecordStore sa=null;
public final String[] mediaType=new String[]{"audio/x-wav","audio/midi","audio/mpeg"};
public showContent(String phoneType,int wid,int hei,int charw,int charR,int n,String[] aboutText,String[] helpText)
{
pType=phoneType;
menuN=n;
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*2);
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 setMenu(int i)
{
menuN=i;
no=0;
}
/*画Command*/
public void drawCMD(Graphics g,String s,int type)
{
if(type==0)//在左
g.drawString(s,0,h-20,0);
else
g.drawString(s,w,h-20,24);
}
public void showAbout(Graphics g1,int x,int y,boolean fillBack,int r,int g,int b)//显示关于
{
if(fillBack)
{
g1.setColor(r,g,b);
g1.fillRect(0,0,w,h);
}
g1.setFont(small);
g1.setColor(0xffffff);
for(int i=0;i<about.size();i++)
{
drawStringShadow(g1,0,0xffffff,0xffffff,(String)(about.elementAt(i)),x,y+i*cr,0);
}
}
/*按键控制*/
public void keyPressed(int i,int type)//type为1时是菜单中的按键,type为2时是帮助中的按键
{
switch(getGameAction(i))
{
case Canvas.UP:
if(type==1)
{
if(no>0)
no--;
else
no=menuN-1;
}
else
{
if(page>0)
page--;
}
break;
case Canvas.DOWN:
if(type==1)
{
if(no<menuN-1)
no++;
else
no=0;
}
else
{
if(page<maxPage)
page++;
}
break;
default:
break;
}
}
/*画小图*/
public void drawClipImage(Graphics g,Image a,int clipx,int clipy,int clipw,int cliph,int offx,int offy)
{
int x,y,cw,ch;
x=g.getClipX();
y=g.getClipY();
cw=g.getClipWidth();
ch=g.getClipHeight();
g.setClip(clipx,clipy,clipw,cliph);
g.drawImage(a,clipx-offx,clipy-offy,0);
g.setClip(x,y,cw,ch);
}
public void paint(Graphics g)
{
}
/*取随机数*/
public static int rand(int n)
{
return((rd.nextInt()>>>1)%n);
}
/*判断软键*/
public int softKey(int i)
{
if(i==-6||i==-21||i==21)
return 0;
else
if(i==-7||i==22||i==-22)
return 1;
else
return -1;
}
/*判断某一点是否在屏幕内*/
public boolean inScreen(int x,int y,int ow,int oh)
{
if((x+ow)>0&&x<w&&(y+oh)>0&&y<h)
return true;
else
return false;
}
/*取绝对值*/
public int abs(int i)
{
return i>=0?i:-i;
}
/*MOTO的按键转换*/
public int keyTrans(int i)
{
if(pType.equals("MOTO"))
{
if(i==-6)
return -2;
else
if(i==-2)
return -3;
else
if(i==-5)
return -4;
else
return i;
}
else
return i;
}
/*显示暂停*/
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,boolean fillBack,int r,int g,int b)//显示帮助
{
if(fillBack)
{
g1.setColor(r,g,b);
g1.fillRect(0,0,w,h);
}
g1.setFont(small);
g1.setColor(0xffffff);
for(int i=0;i<maxR;i++)
{
if(page*maxR+i<help.size())
drawStringShadow(g1,0,0xffffff,0xffffff,(String)(help.elementAt(page*maxR+i)),x,y+i*cr,0);
}
g1.setColor(0xffff00);
drawStringShadow(g1,0xffff00,0,0,"按上下方向键翻页",20,h-20,0);
}
/*画黑白字*/
public void drawStringShadow(Graphics g, int mainColor, int topColor, int bottomColor, String str, int x, int y, int anchor)
{
// if(topColor >= 0)
// {
g.setColor(topColor);
g.drawString(str,x-1,y,anchor);
g.drawString(str,x,y-1,anchor);
g.drawString(str,x-1,y-1,anchor);
// }
// if(bottomColor >= 0)
// {
g.setColor(bottomColor);
g.drawString(str,x+1,y,anchor);
g.drawString(str,x,y+1,anchor);
g.drawString(str,x+1,y+1,anchor);
// }
g.setColor(mainColor);
g.drawString(str,x,y,anchor);
}
/*得到指定透明度的一张图的透明图*/
public Image createARGBImage(Image src,int Alpha)
{
int[] rgb=new int[src.getWidth()*src.getHeight()];
src.getRGB(rgb,0,src.getWidth(),0,0,src.getWidth(),src.getHeight());
for(int i=0;i<rgb.length;i++)
rgb[i]+=Alpha;
return(Image.createRGBImage(rgb,src.getWidth(),src.getHeight(),true));
}
/*画菜单*/
public void drawMenu(Graphics g1,int x,int y,int row,int w,boolean showBack,int r,int g,int b,String[] arrayStr)//显示菜单
{
int color=0;
if(showBack)
{
g1.setColor(r,g,b);
g1.fillRect(x,y,w,arrayStr.length*row);
}
g1.setFont(small);
g1.setColor(0);
g1.drawRect(x,y,w,arrayStr.length*row);
g1.setColor(0xffffff);
for(int i=1;i<3;i++)
g1.drawRect(x-i,y-i,w+i*2,arrayStr.length*row+i*2);
g1.setColor(0);
g1.drawRect(x-3,y-3,w+6,arrayStr.length*row+6);
for(int m=0;m<arrayStr.length;m++)
{
if(no==m)
color=0x0000ff;
else
color=0x0;
drawStringShadow(g1,color,0xffffff,0xffffff,arrayStr[m],x+5,y+5+m*row,0);
}
}
// /*画排行榜*/
// public void drawRank(Graphics g1,int x,int y,int rankNum,boolean fillBack,int r,int g,int b)
// {
// if(fillBack)
// {
// g1.setColor(r,g,b);
// g1.fillRect(0,0,w,h);
// }
// int[] rank;
// g1.setFont(mini);
// g1.drawString("高分记录",w/2,30,17);
// g1.drawString("名次",x,y,0);
// g1.drawString("分数",x+50,y,0);
// g1.setFont(small);
// g1.setColor(-1);
// for(int i=0;i<rankNum;i++)
// {
// System.out.println(i);
// g1.drawString(String.valueOf(i+1),x+5,y+10+i*20,0);
// rank=readIntData(i+1);
// g1.drawString(String.valueOf(rank[0]),x+50,y+10+i*20,0);
// System.out.println(rank.length);
// }
// }
// /*
// 以下是动画特效部分*/
//
// /*百叶窗效果
// 参数说明:type:百叶窗类型(0为横向,1为纵向)
// wn:窗口数量
// iamge:图片
// count:帧序号
// rgb:百叶窗颜色*/
// public void drawBaiye(Graphics g1,Image image,int wn,int count,int rgb)
// {
// g1.drawImage(image,0,0,0);
// g1.setColor(rgb);
// for(int i=0;i<wn;i++)
// g1.fillRect(i*(w/wn),0,w/wn-count,h);
// }
//
// /*地砖铺开效果
// 参数说明:
// tilew:地砖数量
// count:帧序号
// rgb:地砖颜色*/
// public void drawRectTrans(Graphics g1,Image image,int tilew,int count,int rgb)
// {
// g1.drawImage(image,0,0,0);
// g1.setColor(rgb);
// for(int i=0;i<w/tilew;i++)
// for(int j=0;j<h/tilew;j++)
// {
// g1.fillRect(i*tilew,j*tilew,tilew-count,tilew-count);
// }
// }
//
// /*圆形展开效果
// 参数说明:
// count:帧序号
// rgb:圆形颜色*/
// public void drawRoundTrans(Graphics g1,Image image,int count,int rgb)
// {
// g1.drawImage(image,0,0,0);
// g1.setColor(rgb);
// g1.fillArc(w/2-h/2+count,count,(h/2-count)*2,(h/2-count)*2,0,360);
// }
//
// /*图片翻转效果*/
// public void drawTransImage(Graphics g1,Image image,int x,int y,int type)
// {
// if(type==0)//左右翻转
// {
// for(int i=0;i<image.getWidth();i++)
// drawClipImage(g1,image,x+i,y,1,image.getHeight(),image.getWidth()-i,0);
// }
// else
// {
// for(int i=0;i<image.getHeight();i++)
// drawClipImage(g1,image,x,y+i,image.getWidth(),1,0,image.getHeight()-i);
// }
// }
/*
以下是音效部分*/
/*创建播放器*/
public Player createPlayer(String s,int loop)
{
Player player;
try
{
InputStream is=getClass().getResourceAsStream(s);
String t=s.substring(s.indexOf('.')+1,s.length());
if(t.equals("mid"))
player=Manager.createPlayer(is,mediaType[1]);
else
if(t.equals("wav"))
player=Manager.createPlayer(is,mediaType[0]);
else
if(t.equals("mp3"))
player=Manager.createPlayer(is,mediaType[2]);
else
return null;
}
catch(Exception e)
{
return null;
}
player.setLoopCount(loop);
return player;
}
/*播放声音*/
public void playSound(Player p)
{
try
{
System.out.println(p.getState());
if(p.getState()!=400)
p.start();
}
catch(Exception e)
{
}
}
/*暂停声音*/
public void pauseSound(Player p)
{
try
{
if(p.getState()==400)
p.stop();
}
catch(Exception e)
{
}
}
/*停止声音*/
public void stopSound(Player p)
{
System.out.println("stopsound");
try
{
if(p.getState()==400)
p.stop();
if(!pType.equals("NOKIA"))
p.deallocate();
}
catch(Exception e)
{
System.out.println(e);
}
}
/*关闭声音*/
public void closeSound(Player p)
{
try
{
stopSound(p);
p.close();
}
catch(Exception e)
{
}
}
// /*以下是存储类*/
//
// /*整形转化成字节数组*/
// public byte[] int2byte(int a)
// {
// return(new byte[]{(byte)(a>>24&0xff),(byte)(a>>16&0xff),(byte)(a>>8&0xff),(byte)(a&0xff)});
// }
//
// public void saveData(int a,int b)
// {
// saveData(int2byte(a),b);
// }
//
// public void saveData(int[] a,int b)
// {
// byte[] temp=new byte[a.length*4];
// for(int i=0;i<a.length;i++)
// System.arraycopy(int2byte(a[i]),0,temp,i*4,4);
// saveData(temp,b);
// }
//
// /*创建指定记录数的RMS
// 参数type指定此记录集的记录类型
// 1:普通整型记录
// 2:数据流类型记录*/
// public void createData(int recordNum,int type)
// {
// try
// {
// sa=RecordStore.openRecordStore("temp",true);
// for(int i=0;i<recordNum;i++)
// {
// if(type==1)
// sa.addRecord(new byte[]{(byte)0,(byte)0,(byte)0,(byte)0},0,4);
// else
// sa.addRecord(new byte[]{(byte)0},0,1);
// }
// close();
// }
// catch(Exception e)
// {
// }
// }
//
// /*保存暂存数据
// 将记录保存到b的位置*/
// public void saveData(byte[] a,int b)
// {
// try
// {
// if(tempOpen())
// {
// sa=RecordStore.openRecordStore("temp",true);
// sa.setRecord(b,a,0,a.length);
// close();
// }
// else
// {
// sa=RecordStore.openRecordStore("temp",true);
// sa.addRecord(a,0,a.length);
// close();
// }
// }
// catch(java.lang.NullPointerException npe)
// {
// }
// catch(javax.microedition.rms.RecordStoreException rec)
// {
// }
// }
//
// /*检测暂存数据库是否打开*/
// public boolean tempOpen()
// {
// boolean o=false;
// try
// {
// sa=RecordStore.openRecordStore("temp",true);
// RecordEnumeration re=sa.enumerateRecords(null,null,false);
// if(re.hasNextElement())
// o=true;
// else
// o=false;
// close();
// }
// catch(RecordStoreException rse)
// {
// }
// return(o);
// }
//
// /*字节数组转化成整数*/
// private int byte2int(byte[] tempdata,int off)
// {
// return((tempdata[off+0]<<24&0xff000000)+(tempdata[off+1]<<16&0x00ff0000)+(tempdata[off+2]<<8&0x0000ff00)+(tempdata[off+3]&0x000000ff));
// }
//
// /*读入暂存数据*/
// public int[] readIntData(int sn)
// {
// int[] tempStack;
// byte[] tempBytes;
// tempBytes=readByteData(sn);
// tempStack=new int[tempBytes.length/4];
// for(int m=0;m<tempStack.length;m++)
// {
// tempStack[m]=byte2int(tempBytes,m*4);
// //System.out.println(tempStack[m]);
// }
// return(tempStack);
// }
// public byte[] readByteData(int sn)
// {
// byte[] tempBytes;
// try
// {
// sa=RecordStore.openRecordStore("temp",true);
// tempBytes=new byte[sa.getRecordSize(sn)];
// tempBytes=sa.getRecord(sn);
// close();
// return(tempBytes);
// }
// catch(RecordStoreException rse)
// {
// System.out.println(rse);
// return null;
// }
// }
// /*删除数据库纪录*/
// public void delete()
// {
// try
// {
// RecordStore.deleteRecordStore("temp");
// }
// catch(RecordStoreException e)
// {
// }
// }
// /*关闭数据库
// (每次打开数据库使用后要及时关闭数据库,避免删除数据库操作出错)*/
// public void close()
// {
// try
// {
// if(sa!=null)
// {
// sa.closeRecordStore();
// }
// }
// catch (RecordStoreException ex)
// {
// }
// }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -