0

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!

1 Answers1

2
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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433