20111117

引用jar 內的檔案資源


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <applet code="demogame.GameRun.class" archive="JAppletDemo_v0.01.jar" width=800 height=600></applet>
</body>
</html>

假設JAppletDemo_v0.01.jar 包中,程序進入點為demogame 包中的GameRun.class ,且要取得block_a.png 的圖檔,並且圖檔並沒有被放入任何package中,使用

getClass().getResource("/block_a.png")

來取得資源,注意路徑要寫正確。

Applet路徑方面
getCodeBase()
無法取得package裡的資源,返回Applet Class所在目錄的路徑。在HTML文件的"Applet"標籤中的CODEBASE屬性值計算出來,若該屬性沒有設置則返回該HTML文件所在目錄。

getDocumentBase()
返回包含Applet的HTML文件所在目錄。

很難懂。

沒有留言: