好的,我发现(从一些与其他问题相关的答案中收集到的花絮)是我需要创建一个 URL 项,然后通过 ClassLoader 或类似方法填充它。
例如,一个人得到了他的东西:
String filename = getClass().getClassLoader().getResource("res/Kappa.png").toString();
String preTag="<PRE>filename is : "+filename+"</PRE>";
String imageTag="<img src=\""+filename+"\"/>";
kit.insertHTML(doc, doc.getLength(), preTag+imageTag, 0, 0, HTML.Tag.IMG);
同时,从这个提示中得出最终让我的东西运行“最好”的方法是:
URL url;
String keystring = "<img src=\"";
String file, tag, replace;
int base;
while (s.toLowerCase().contains(keystring)) { // Find next key (to-lower so we're not case sensitive)
//There are undoubtedly more efficient ways to do this parsing, but this miraculously ran perfectly the very first time I compiled it, so, you know, superstition :)
base = s.toLowerCase().indexOf(keystring);
file = s.substring(base + keystring.length(), s.length()).split("\"")[0]; // Pull the filename out from between the quotes
tag = s.substring(base, base + keystring.length()) + file + "\""; // Reconstruct the part of the tag we want to remove, leaving all attributes after the filename alone, and properly matching the upper/lowercase of the keystring
try {
url = GameModule.getGameModule().getDataArchive().getURL("images/" + file);
replace = "<img src=\"" + url.toString() + "\""; // Fully qualified URL if we are successful. The extra
// space between IMG and SRC in the processed
// version ensures we don't re-find THIS tag as we
// iterate.
} catch (IOException ex) {
replace = "<img src=\"" + file + "\""; // Or just leave in except alter just enough that we won't find
// this tag again.
}
if (s.contains(tag)) {
s = s.replaceFirst(tag, replace); // Swap in our new URL-laden tag for the old one.
} else {
break; // BR// If something went wrong in matching up the tag, don't loop forever
}
}
//BR// Insert a div of the correct style for our line of text.
try {
kit.insertHTML(doc, doc.getLength(), "\n<div class=" + style + ">" + s + "</div>", 0, 0, null);
} catch (BadLocationException ble) {
ErrorDialog.bug(ble);
} catch (IOException ex) {
ErrorDialog.bug(ex);
}
conversation.update(conversation.getGraphics()); //BR// Force graphics to update
我的包在 VASSAL(游戏平台)下运行,因此能够从 VASSAL 模块的数据存档(zip 文件)中提取内容对我来说是理想的,尽管当时我问这个问题我会很高兴找出我可以让它找到图像文件的任何地方,并将其从数据存档中取出是后来的延伸目标。
您可以在这里看到我的策略是让 html(可能由另一个模块设计者生成,而不是我)在源标签中引用简单的文件名(例如 src="dice.png"),然后我将其解析并替换如果成功,则将它们与 URL 查询的结果一起显示。
如果有人对原始问题的主旨有更完整的答案(HTMLEditorKit 到底在哪里寻找本地路径? - 默认本地路径是什么?如何让它在我当前的工作目录或目录中查找?应用程序正在运行,或者实际上是任何类似的本地)然后请在此处发布以供后代使用,因为尽管这让我运行,但我不觉得这是对原始问题的全面回答。