Ok, here is a simple applet i built to test putting applets on a website
Class file:
package loadingImages;
import java.awt.*;
import java.applet.*;
import java.net.*;
public class DrawImage extends Applet{
//Image variable
private Image image;
private Image image2;
private Image image3;
private Image image4;
private String construction;
private URL getURL(String filename){
URL url = null;
try{
url = this.getClass().getResource(filename);
}catch(Exception e){}
return url;
}
//applet init event
public void init(){
image = getImage(getURL("pilenliten.png"));
image2 = getImage(getURL("Pilen_drogad.png"));
construction = "Under construction :D";
}
//applet paint event
public void paint(Graphics g){
//create g2d instance
Graphics2D g2d = (Graphics2D) g;
//Fill bg
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getSize().width, getSize().height);
//draw image
g2d.drawImage(image, 0, 0, this);
g2d.drawImage(image2, 512, 0, this);
g2d.setColor(Color.RED);
g2d.drawString(construction, 300, 240);
}
}
Exception:
java.lang.NullPointerException
at sun.awt.image.URLImageSource.<init>(Unknown Source)
at sun.applet.AppletImageRef.reconstitute(Unknown Source)
at sun.misc.Ref.get(Unknown Source)
at sun.applet.AppletViewer.getCachedImage(Unknown Source)
at sun.applet.AppletViewer.getImage(Unknown Source)
at java.applet.Applet.getImage(Unknown Source)
at spriteTest.ImageEntity.load(ImageEntity.java:65)
at spriteTest.SpriteTest.init(SpriteTest.java:28)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
I know why it get’s an exception, it’s because it can’t find pilenliten.png and Pilen_drogad.png, it only runs if i export it to a .jar, then open it with 7zip and add pilenliten.png and Pilen_drogad.png to the folder with the class files in the .jar, then start the .jar with a html file.