Reputation: 39
There is a similar question (Java Applet Cannot Locate Resources), but base
gives me an error, and asks me for the creation of a variable.
I have an applet that I embedded into HTML with:
<object type="application/x-java-applet" height="600" width="800">
<param name="code" value="net.me.applet.WindowMain" />
</object>
The applet launches, and attempting to change the contentpane's color works too, so that means that the link with the HTML might not have errors.
Then, I linked my assets like so:
ImageIcon left = new ImageIcon(getCodeBase(), "Arrow_Left.png");
The PNG is in the same directory as my applet. The images don't show, so I assume that the assets were not found. I also tried doing "/Arrow_Left.png"
, without avail.
The directory structure is this:
/ root directory
index.html
(dir)net
| me
| applet
| WindowMain.class
When I developed the applet in Eclipse, I made a package with the compilation unit, and a folder called "assets" in the src folder, and I accessed those for testing purposes with ImageIcon("src/assets/Arrow_Left.png")
Upvotes: 1
Views: 99
Reputation: 168825
I put the PNG in the same dir as my WindowMain.class
Then the string needs to be the relative path from the code base. Something like:
ImageIcon left = new ImageIcon(getCodeBase(), "net/me/applet/Arrow_Left.png");
But definitely heed the advice to use ImageIO.read(..)
which provides better feed-back.
Upvotes: 1