📄 tools.java
字号:
//分割文字的工具类
import java.util.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class Tools {
private static int LT = Graphics.LEFT | Graphics.TOP;
private static int CC = Graphics.HCENTER | Graphics.VCENTER;
private static Font myFont=Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
/*
//根据指定字数分割文字成若干段
public static String[] splitStringOld(String msg,int len)
{
if(len<=0)return null;
if(msg==null || msg.equals(""))return new String[]{""};
Vector fontVC=new Vector();
int beginIndex=0;
while(beginIndex<msg.length())
{
int endIndex=beginIndex+len;
if(endIndex>msg.length())
{
endIndex=msg.length();
}
String sub=msg.substring(beginIndex,endIndex);
fontVC.addElement(sub);
beginIndex=endIndex;
}
if(fontVC.size()>0)
{
String[] st=new String[fontVC.size()];
for(int i=0;i<fontVC.size();i++)
{
st[i]=(String)fontVC.elementAt(i);
}
return st;
}
else
{
return null;
}
}
*/
//根据指定宽度分割文字成若干段
public static String[] splitString(String msg,int width)
{
if(width<=0)return null;
if(msg==null || msg.equals(""))return new String[]{""};
Vector fontVC=new Vector();
int beginIndex=0;
StringBuffer sb=new StringBuffer("");
while(beginIndex<msg.length())
{
sb.append(msg.charAt(beginIndex));
if(myFont.stringWidth(sb.toString())>width)
{
sb.deleteCharAt(sb.length()-1);
fontVC.addElement(sb.toString());
//msg=msg.substring(beginIndex);
sb.delete(0,sb.length());
beginIndex--;
}
beginIndex++;
}
if(sb.length()>0)
{
fontVC.addElement(sb.toString());
}
if(fontVC.size()>0)
{
String[] st=new String[fontVC.size()];
for(int i=0;i<fontVC.size();i++)
{
st[i]=(String)fontVC.elementAt(i);
}
return st;
}
else
{
return null;
}
}
//检查两个盒子是否相交
public static boolean checkBoxInter(int x1,int y1,int w1,int h1,int x2,int y2,int w2,int h2)
{
//x1,y1;x1+w1,y1;x1,y1+h1;x1+w1,y1+h1
//x2,y2;x2+w2,y2;x2,y2+h2;x2+w2,y2+h2
//System.out.println("x1="+x1+",y1="+y1+",w1="+w1+",h1="+h1+",x2="+x2+",y2="+y2+",w2="+w2+",h2="+h2);
if(x1>=x2 && x1<=x2+w2 && y1>=y2 && y1<=y2+h2)return true;
if(x1+w1>=x2 && x1+w1<=x2+w2 && y1>=y2 && y1<=y2+h2)return true;
if(x1>=x2 && x1<=x2+w2 && y1+h1>=y2 && y1+h1<=y2+h2)return true;
if(x1+w1>=x2 && x1+w1<=x2+w2 && y1+h1>=y2 && y1+h1<=y2+h2)return true;
if(x2>=x1 && x2<=x1+w1 && y2>=y1 && y2<=y1+h1)return true;
if(x2+w2>=x1 && x2+w2<=x1+w1 && y2>=y1 && y2<=y1+h1)return true;
if(x2>=x1 && x2<=x1+w1 && y2+h2>=y1 && y2+h2<=y1+h1)return true;
if(x2+w2>=x1 && x2+w2<=x1+w1 && y2+h2>=y1 && y2+h2<=y1+h1)return true;
return false;
}
//检查两个盒子是否相交
public static boolean checkBoxInter(Box box1,Box box2)
{
return checkBoxInter(box1.x,box1.y,box1.w,box1.h,box2.x,box2.y,box2.w,box2.h);
}
//取得图片对象
public static Image getImage(String str)
{
Image img=null;
try {
img = Image.createImage(str);
}
catch (Exception ex) {
System.out.println(ex);
}
return img;
}
//从一张图片中绘制一个区域到Canvas
public static void drawRegion(Graphics g,Image img,int x_src,int y_src,int width,int height,int x_dest,int y_dest,int anchor)
{
//System.out.println("g.getClipX()="+g.getClipX());
//System.out.println("g.getClipY()="+g.getClipY());
//System.out.println("g.getClipWidth()="+g.getClipWidth());
//System.out.println("g.getClipHeight()="+g.getClipHeight());
int clipX=g.getClipX();
int clipY=g.getClipY();
int clipWidth=g.getClipWidth();
int clipHeight=g.getClipHeight();
if(anchor==CC)
{
g.setClip(x_dest-width/2,y_dest-height/2,width,height);
g.drawImage(img,x_dest-width/2-x_src,y_dest-height/2-y_src,LT);
}
if(anchor==LT)
{
g.setClip(x_dest,y_dest,width,height);
g.drawImage(img,x_dest-x_src,y_dest-y_src,LT);
}
g.setClip(clipX,clipY,clipWidth,clipHeight);
}
public static Date getDateByDateStr(String dateStr)
{
int y=0;
int M=0;
int d=0;
int h=0;
int m=0;
int s=0;
int blankIndex=dateStr.indexOf(" ");
if(blankIndex==-1) //没有时分秒
{
int yIndex=dateStr.indexOf("-");
y=Integer.parseInt(dateStr.substring(0,yIndex));
int MIndex=dateStr.indexOf("-",yIndex+1);
M=Integer.parseInt(dateStr.substring(yIndex+1,MIndex));
d=Integer.parseInt(dateStr.substring(MIndex+1));
}
else
{
int yIndex=dateStr.indexOf("-");
y=Integer.parseInt(dateStr.substring(0,yIndex));
int MIndex=dateStr.indexOf("-",yIndex+1);
M=Integer.parseInt(dateStr.substring(yIndex+1,MIndex));
d=Integer.parseInt(dateStr.substring(MIndex+1,blankIndex));
int hIndex=dateStr.indexOf(":",blankIndex+1);
h=Integer.parseInt(dateStr.substring(blankIndex+1,hIndex));
int mIndex=dateStr.indexOf(":",hIndex+1);
m=Integer.parseInt(dateStr.substring(hIndex+1,mIndex));
s=Integer.parseInt(dateStr.substring(mIndex+1));
}
/*
System.out.println(dateStr);
System.out.println(y);
System.out.println(M-1);
System.out.println(d);
System.out.println(h);
System.out.println(m);
System.out.println(s);
*/
Calendar cal=Calendar.getInstance();
cal.set(Calendar.YEAR,y);
cal.set(Calendar.MONTH,M-1);
cal.set(Calendar.DAY_OF_MONTH,d);
cal.set(Calendar.HOUR_OF_DAY,h);
cal.set(Calendar.MINUTE,m);
cal.set(Calendar.SECOND,s);
Date date=cal.getTime();
return date;
}
public static String getUTFStr(byte[] args)
{
String str = "";
try {
str = new String(args, 0, args.length, "UTF-8");
}
catch (UnsupportedEncodingException ex)
{
//ex.printStackTrace();
}
return str;
}
public static byte[] getUTFByte(String str)
{
byte[] b = null;
try {
b = str.getBytes("UTF-8");
}
catch (UnsupportedEncodingException ex) {
b = str.getBytes();
}
return b;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -