▼J2MEサンプルプログラム集▼
その他


文字列
    //文字列を任意の文字で分割
    public static String[] parseStr(String str,char sep) {
        int i,j,size;
        String[] result;
            
        //置換
        str=replaceStr(str,'\r',"");
    
        //最後尾に分割文字
        if (str.equals("")||str.charAt(str.length()-1)!=sep) str+=sep;
    
        //サイズを得る
        size=0;
        i=str.indexOf(sep);
        while (i>=0) {
            size++;
            i=str.indexOf(sep,i+1);
        }
    
        //分割する
        result=new String[size];
        size=0;
        j=0;
        i=str.indexOf(sep);
        while (i>=0) {
            result[size++]=str.substring(j,i);
            j=i+1;
            i=str.indexOf(sep,j);
        }
        return result;
    }
    
    //文字列の置換
    public static String replaceStr(String text,char from,String to) {
        if (text.indexOf(from)<0) return text;
        StringBuffer sb=new StringBuffer();
        for (int i=0;i<text.length();i++) {
            if (text.charAt(i)!=from) {
                sb.append(text.charAt(i));
            } else {
                sb.append(to);
            }
        }
        return sb.toString();
    }
    
    //ベクター⇒配列
    public static String[] vec2arr(Vector vec) {
        String[] arr=new String[vec.size()];
        for (int i=0;i<arr.length;i++) {
            arr[i]=(String)vec.elementAt(i);
        }
        return arr;
    }
    
    //配列⇒ベクター
    public static Vector arr2vec(String[] arr) {
        Vector vec=new Vector();
        for (int i=0;i<arr.length;i++) {
            vec.addElement(arr[i]);
        }
        return vec;
    }
    
    //文字列の左側を文字で埋める
    public static String fillStr(String text,int len,char c) {
        while (text.getBytes().length<len) text=c+text;
        return text;
    }
    
    //文字列の左側を空文字で埋める
    public static String fillStr(String text,int len) {
        return fillStr(text,len,' ');
    }
    
    //文字を幅で分割【DoJa】
    public static String[] divideStr(String str,int width) {
        Vector vec=new Vector();
        int i,w,sep;
        Font font=font=Font.getFont(Font.SIZE_TINY);
        w=font.stringWidth("a");
        while (true) {
            if (str.getBytes().length<=width/w) {
                vec.addElement(str);
                break;
            }
            sep=font.getLineBreak(str,0,str.length(),width);
            vec.addElement(str.substring(0,sep));
            str=str.substring(sep);
        }
        return vec2arr(vec);
    }
    
    //文字列を幅と改行で分割【DoJa】
    public static String[] divideStr(String str,int width,char sep) {
        Vector vec=new Vector();
        int i,j,k;
        String[] line,strs;
        line=parseStr(str,sep);
        for (i=0;i<line.length;i++) {
            strs=divideStr(line[i],width);
            for (j=0;j<strs.length;j++) {
                vec.addElement(strs[j]);
            }
        }
        return vec2arr(vec);
    }


ユーティリティ
    //乱数
    private static Random rand=new Random();
    public static int rand(int num) {
        return (rand.nextInt()>>>1)%num;
    }
        
    //スリープ
    public static void sleep(long time) {
        try {
            if (time>10) Thread.sleep(time);
        } catch (Exception e) {
        }
    }
    
    //メモリチェック
    private static Runtime runtime=Runtime.getRuntime();
    public static String checkMem() {
        return ((runtime.totalMemory()-runtime.freeMemory())/1024)+"KB/"+
            (runtime.totalMemory()/1024)+"KB";
    }
    
    //ダイアログの表示【DoJa】
    public static void showDialog(String title,String text) {
        Dialog dlg=new Dialog(Dialog.DIALOG_INFO,title);
        dlg.setFont(Font.getFont(Font.SIZE_SMALL));
        dlg.setText(text);
        dlg.show();
    }
    
    //ブラウザ起動【DoJa】
    private static void launchBrowser(IApplication a,String url) {
        try {
            a.launch(IApplication.LAUNCH_BROWSER,new String[]{url});
        } catch (Exception e) {
        }
    }


描画
    //太文字列の描画【DoJa】
    public static void drawBold(String str,int x,int y,
        int bgcolor,int forecolor) {
        g.setColor(bgcolor);
        g.drawString(str,x-1,y-1);
        g.drawString(str,x  ,y-1);
        g.drawString(str,x+1,y-1);
        g.drawString(str,x-1,y);
        g.drawString(str,x+1,y);
        g.drawString(str,x-1,y+1);
        g.drawString(str,x  ,y+1);
        g.drawString(str,x+1,y+1);
        g.setColor(forecolor);
        g.drawString(str,x,y);
    }


スクラッチパッド
    //スクラッチへの書き込み【DoJa】
    public static void writeScr(int scrIdx,int scrPos,byte[] data) 
        throws Exception {
        OutputStream out=null;
        try {
            out=Connector.openOutputStream(
                "scratchpad:///"+scrIdx+";pos="+scrPos);
            out.write(data);
            out.close();
        } catch (Exception e) {
            try {
                if (out!=null) out.close();
            } catch (Exception e2) {
            }
        }
    }

    //スクラッチからの読み込み【DoJa】
    public static byte[] readScr(int scrIdx,int scrPos,int length) 
        throws Exception {
        int size,len=0;
        byte[] w=new byte[10240];
        InputStream           in =null;
        ByteArrayOutputStream out=null;
        try {
            in=Connector.openInputStream(
                "scratchpad:///"+scrIdx+";pos="+scrPos);
            out=new ByteArrayOutputStream();
            while (true) {
                size=in.read(w);
                if (size<=0) break;
                if (len+size>=length) size=length-len;
                out.write(w,0,size);
                len+=size;
                if (len>=length) break;
            }
            in.close();
            out.close();
            return out.toByteArray();
        } catch (Exception e) {
            try {
                if (in !=null) in.close();
                if (out!=null) out.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }


通信
    //ネットからの読み込み【DoJa】
    public static byte[] readNet(String url) throws Exception {
        byte[] w=new byte[10240];
        int size;
        HttpConnection        c  =null;
        InputStream           in =null;
        ByteArrayOutputStream out=null;
        try {
            //ネットとの接続
            c=(HttpConnection)Connector.open(url,Connector.READ,false);
            c.setRequestMethod(HttpConnection.GET);
            c.connect();
            in =c.openInputStream();
            out=new ByteArrayOutputStream();

            //データの読み込み
            while (true) {
                size=in.read(w);
                if (size<=0) break;
                out.write(w,0,size);
            }

            //ネットとの切断
            out.close();
            in.close();
            c.close();
            return out.toByteArray();
        } catch (Exception e) {
            //例外処理
            try {
                if (out!=null) out.close();
                if (in !=null) in.close();
                if (c  !=null) c.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }


リソース
    //リソースからの読み込み【DoJa】
    public static byte[] readRes(String name) throws Exception {
        int size;
        byte[] w=new byte[10240];
        InputStream           in =null;
        ByteArrayOutputStream out=null;
        try {
            in =Connector.openInputStream("resource:///"+name);
            out=new ByteArrayOutputStream();
            while (true) {
                size=in.read(w);
                if (size<=0) break;
                out.write(w,0,size);
            }
            in.close();
            out.close();
            return out.toByteArray();
        } catch (Exception e) {
            try {
                if (in !=null) in.close();
                if (out!=null) out.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }


−戻る−