服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

一些实用的图形用户界面方法

这个函数已反复应用于多个手机应用软件平台
用法:参数定义:str――要分割的字符串
                        font――字体
                        rowmaxw――分割后每行宽度
         支持标示符:
         \n    换行
         \t     插入两个汉字长度的空格

public static final string[] clipstring(string str,font font,int rowmaxw){
  if(str == null)
   return null;
  if(rowmaxw < font.charwidth('哈'))
   rowmaxw = font.charwidth('哈');         
  int strid = 0;
  int roww = 0;
  vector strmanager = new vector();
  char ch = ' ';
  while(str.length() > strid){
   ch = str.charat(strid);
   switch(ch)
   {
   case '\n':
    strmanager.addelement(str.substring(0,strid));
    str = str.substring(strid+1);
    roww = 0;
    strid = 0;
    break;
   case '\t':
    stringbuffer sb = new stringbuffer(str);
    sb.deletecharat(strid);
    sb.insert(strid,"       ");
    str = sb.tostring();
    break;
   default:
    if(roww + font.charwidth(ch) > rowmaxw){
     strmanager.addelement(str.substring(0,strid));
     str = str.substring(strid);
     roww = 0;
     strid = 0;
    }else{
     roww += font.charwidth(ch);
     strid++;
    }
   }
  }
  strmanager.addelement(str);
  string[] o_str = new string[strmanager.size()];
  strmanager.copyinto(o_str);
  return o_str;


返回结果是一个已切割好的string数组,只要用一个循环打印出来就可以了

public static final void drawclipstring(graphics g,string[] clipstr,font font,int color,int x,int y){
  if(clipstr == null){
     system.out.println("drawclipstring");
     return;
  }
  int fonth = font.getheight();
  g.setfont(font);
  g.setcolor(color);
  for(int i=0;i<clipstr.length;i++)
     g.drawstring(clipstr[ i ],x,y+i*fonth,0);


参数定义:clipstr――先前分割好的字符串数组
               font――字体
               color――颜色
               x,y――打印的屏幕位置

注意,切割和打印函数的字体参数必须保持一致!

半透明技术(限midp2.0)

// 获得半透明图片,透明度从0到10共分为11个等级
public static final image alfimage(image img,int alf){
  if(img == null){
   system.out.println("alfimage");
   return null;
  }
  if(alf < 0)
   alf = 0;
  else if(alf > 10)
   alf = 10;
  int imgw = img.getwidth();
  int imgh = img.getheight();
  int[] rgbdata = new int[imgw*imgh];
  img.getrgb(rgbdata,0,imgw,0,0,imgw,imgh);
  int tmp = ((alf*255/10) << 24)|0x00ffffff;
  for(int i=0;i<rgbdata.length;i++)
   rgbdata &= tmp;
  image o_img = image.creatergbimage(rgbdata,imgw,imgh,true);
  return o_img;

灰度图转化函数


// 得到灰度图
public static final image grayimage(image img){
  if(img == null){
   system.out.println("grayalfimage");
   return null;
  }
  int imgw = img.getwidth();
  int imgh = img.getheight();
  int[] imgrgbdata = new int[imgw*imgh];
  img.getrgb(imgrgbdata,0,imgw,0,0,imgw,imgh);
  int alf = 0;
  int r = 0;
  int g = 0;
  int b = 0;
  int gray = 0;
  for(int i=0;i<imgrgbdata.length;i++){
   alf = (imgrgbdata >> 24) & 0xff;
   r = (imgrgbdata >> 16) & 0xff;
   g = (imgrgbdata >> 8) & 0xff;
   b = imgrgbdata & 0xff;
   gray = (r*77+g*151+b*28 + 128)>>8;
   imgrgbdata = (alf<<24)|(gray<<16)|(gray<<8)|gray;
  }
  return image.creatergbimage(imgrgbdata,imgw,imgh,true);
}

扫描关注微信公众号