2

私は自分のデータファイルがどこにあるかを中心に展開するニーズの収束を持っています。現在のアプリケーションでは、すべてのクラスファイルとデータファイルがJARに含まれています(Eclipse IDEを使用して構築)。

人々がこの情報を入手する方法はさまざまであるように思われることに気づきました。画像ファイル(グラフィックス)の場所へのパスが必要です。URLとしても必要ですoツールキット呼び出しを使用します。

Toolkit tk = frame.getToolkit();
image = tk.getImage(url.toFile());

しかし、URLなどの作成に問題があります。私はいくつかの異なる方法を試しました。この時点で、ファイルシステムのクラスファイルの隣にデータファイルを保持します。私は自分の仕事に別の関数を追加しています-デバッグで実行するときに/binディレクトリを削除します。

// in class BwServices at init:
try {
    rootDataPath = BwServices.class.getProtectionDomain()
        .getCodeSource().getLocation().getPath();
    rootDataPath = URLDecoder.decode(rootDataPath, "UTF-8");
        fileSystemAccess = true;
} 
if(rootDataPath.endsWith("/bin/"))  
    rootDataPath = rootDataPath.substring(0, rootDataPath.length() - 4);

後で...画像を取得するために行きますが、一部の通話が機能しません。理由がわかりません。

私は2つのことを試しました...

// 1
String s = "file://" + rootDataPath + d.toString() + fileName;   
url = frame.getClass().getResource(s);

// 2
    try {
        url = new URL(s);
    } catch (MalformedURLException e) {
        trace("getImage - can't get url: " + s);
        e.printStackTrace();
    } 

どちらにも問題があります。
さまざまな場所から画像を取得するための電話があります。'frame'は、クラスBwFrameの実行中の親フレームです。

私の道はどんな試みでもこのように出てきます...

rootDataPath:/ C:/ Users / Markgm / Documents / DEV / worksheet / bwp /

そのため、クラスファイルが存在する場所に関連するファイルについて、ツールキットのFileInputStreamまたはURLのいずれかを開く方法を探しています(デバッグ中の場合はいくつかのトリックがあります)。クライアント側のアプリの場合、これがこれです。いつかこれをアプレットとして実行したいと思うかもしれませんが、画像ファイルがどこにあるべきかわかりません。(50x75のトランプといくつかのボタンの画像)どんな助けやアドバイスも大歓迎です!

TIA、マーク

4

2 に答える 2

1
Toolkit tk = frame.getToolkit();

ToolkitJava 1.4以降、画像の読み込みには使用しないでください。代わりに使用してくださいImageIO.read(String/File/URL)。これは、戻る前に画像全体が確実に読み込まれるようにするブロック方法です。

image = tk.getImage(url.toString());

そして、実際の問題があります。URL(または)オブジェクトを作成したらFile、それを捨ててString表現を使用しないでください。さらに重要なStringことに、を表すことになっているFileが実際にはURL!を表すを提供しないでください。

私はコードスニペットの残りの混乱を読んでいませんでした(質問またはフォローアップの「回答」で)、あなたは将来SSCCEを投稿することを検討するかもしれません。

于 2011-08-20T16:31:32.120 に答える
0

貼り付けコードの使用から始まった、URL の使用をまったく使用しなくなりました。クラス ファイルから実行する場合と JAR ファイルから実行する場合の URL の動作は、すべての場合と同様に変更されました。だから、私がやったことのいくつかを示すコードがここにあり、何が起こるかについてのコメント (クラスファイルまたはjarファイル) があり、デバッグ時間が機能するように調整されています (最後から bin/ を切り取ります)。

        // root path - starts with jar file or pathspec
    try {
        rootDataPath = BwServices.class.getProtectionDomain()
                    .getCodeSource().getLocation().getPath();
        rootDataPath = URLDecoder.decode(rootDataPath, "UTF-8");
        if(rootDataPath.endsWith("BwPlayer.jar"))
            rootDataPath = rootDataPath.substring(0, rootDataPath.length() - 12);
        fileSystemAccess = true;
    } catch(SecurityException e) {
        trace("No file system access: " + e.getMessage());
        messageBox(frame, "BwServices", "Cannot access file system");
        rootDataPath = "";
    } catch (UnsupportedEncodingException e) {
        trace("Jar URL decode exception: "+ e.getMessage());
    } 
    if(rootDataPath.endsWith("/bin/"))  // remove bin/ portion if in debug
        rootDataPath = rootDataPath.substring(0, rootDataPath.length() - 4);
    trace("rootDataPath: "+rootDataPath);

上: init() 時間から。以下は、余分なデバッグ関連の行を含む getImage() 関数です。注: url.getFile() は、2 つの理由で直接機能しません。1 つは、jar から取り出したときに file:/ を持っていること、もう 1 つは、事前に記述されたルート / の下のフルパススペックを取得しないためです。

    static public Image getImage(Directory d, String fileName) {
    Image image = null;
    String s = d.toString() + fileName;
    /*
    // Note: Start with / required for this url call (w/o full pathsepc)
    URL url = parentFrame.getClass().getResource("/" + s);
    if(url == null) {
         trace(s + " - null url ");
         return null;
    }
    */
    String file = rootDataPath + s; // url.getFile();
    // end note
    String t = s = "getImage(" + file + ") ";

    Toolkit tk = parentFrame.getToolkit();
    if(tk == null)
        s = s + "NULL tk ";
    else {
        try {
            // full pathspec needed here 
            // url.getFile() returns with file:/ when running from the .jar file
            //  but not when running from .class file (so don't use it)
            s = t = "getImage(" + file + ") ";
            image = tk.getImage(file); //url.getFile());
            MediaTracker media = new MediaTracker(parentFrame);
            media.addImage(image, 0);
            try {
                media.waitForID(0);
            } catch(InterruptedException e) {
                s = s + e.getMessage();
            }       
            if(image == null) {
                // image = null;
                s = s + "NULL image ";
            } else if(image.getHeight(parentFrame) < 1) {
                s = s + "invalid height";
            }
        } catch (SecurityException e) {
            s = s + e.getMessage();
        }
    } 

    if(! s.equals(t)) {
        s = "file=" + file + "\n" + s;
        s = "rootDataPath=" + rootDataPath + "\n" + s;
        messageBox(parentFrame, "getImage()", s);
    }
    return image;
}
于 2011-08-20T15:46:01.073 に答える