I am having problems with my new Java applet.
public Speler() {
this.x = 10;
this.y = 470;
hitBox = new Rectangle( x, y, 52, 10 );
spaceShip = new ImageIcon( "images/spaceship.png" );
}
In my project's src folder, I have some .png images which need to be loaded in. In Eclipse AppletViewer this works just fine, however in my browser it does not.
I have already searched the internet and tried signing it, however this didn't help.
Any help would be appreciated, however I just started programming in Java, so I don't know an awful lot!
new ImageIcon( "images/spaceship.png" );
That constructor presumes the String
represents a File
path. It cannot work for an applet from a web site since a File
can only ever point to a resource on the client computer where the applet is running.
For an applet, instead access resources by URL
. The URL might be constructed relative to the code base or document base of the applet, or from a Jar on the run-time class-path of the applet. If an applet is digitally signed and declares all-permissions
, it can even reach across sites to fetch images, as long as the external site allows hot-linking.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.