我的需求集中在我的数据文件所在的位置。当前应用程序在一个 JAR 中包含所有类和数据文件(使用 Eclipse IDE 构建)。
我注意到人们似乎可以通过多种方式获取这些信息。我需要我的图像文件所在的路径(图形)。我还需要它作为使用工具包调用的 URL。
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);
后来……我去取图,有些电话打不通,不知道为什么
我试过两件事......
// 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();
}
其中任何一个都有问题。
我有电话从不同的地方获取图像。“框架”是整个执行过程中的父框架,在 BwFrame 类中。
在任何尝试中,我的道路都是这样的......
根数据路径:/C:/Users/Markgm/Documents/DEV/workspace/bwp/
因此,我正在寻找方法来打开工具包的 FileInputStream 或 URL,以获取与类文件所在位置相关的文件(以及调试时的一些技巧)。对于客户端应用程序,这就是它。有一天,我可能想将它作为 Applet 运行,但我不知道图像文件必须在哪里。(50x75 扑克牌和一些按钮图像)感谢任何帮助或建议!
TIA,马克