0

I am having a problem with my image not being displayed on my applet, here is the code, all pertaining to the image;

 Image globe;

//Adding Image in init()
Image globe = getImage (getCodeBase (), "C:/Users/Andrew/Downloads/Computer Science/globe.jpg");
//later in code

 public void paint (Graphics g)
    {
        g.drawString ("'The Best Travel Agency in the world' - John Travelta", 400, 675);
        g.drawImage(globe, 0, 100, this);
    } // paint method

    public boolean action (Event e, Object o)
    {
        if (e.target == DomRep)
        {
            String DomRepBox = JOptionPane.showInputDialog ("Please Enter your name: ");
        }
        return true;
    }

this is the error I am receiving;

java.lang.NullPointerException
    at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
    at sun.java2d.pipe.DrawImage.copyImage(Unknown Source)
    at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
    at sun.java2d.SunGraphics2D.drawImage(Unknown Source)
    at CPT.paint(CPT.java:129)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)
2
  • 2
    You should avoid using absolute paths, such as C:/Users/Andrew/Downloads/Computer Science/globe.jpg, especially when using applets. Instead, you need to use a relative path based on the relationship between the image and the applet Commented Jan 19, 2014 at 22:34
  • Why code an applet? If it is due to spec. by teacher, please refer them to Why CS teachers should stop teaching Java applets. Commented Jan 20, 2014 at 1:59

2 Answers 2

1

I don't understand what the getImage() method is, but normally you import images like this:

try {
    globe = ImageIO.read(new URL("URL_OF_FILE.png"));
} catch (IOException e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks! Alright, now I am getting a "throwIOException" error. Any ideas?
Given that this is an applet, referencing the image through an absolute path is a bad idea. Given that this is applet, you won't have access to the underlying file system, so you, unless it's singed and the local permission allow it, you won't be able to use File.
You have to surround it with a try catch. I modified the above code to incorporate it. MadProgrammer brings up a good point too. Store your images in your program's working directory.
Worked perfectly! And thanks for the advice, will do!
ImageIO.read(new File(.. That will fail as soon as the applet is deployed. Applets and files don't mix. To load a File, the applet would need to be fully trusted, and even then the image would need to be present on disks of the the user's PC as opposed to the server.
|
1
Image globe = getImage (getCodeBase(), "relative/path/to/globe.jpg");

Should work just fine for an applet. If the applet is in the same directory as the HTML, this is even easier.

Image globe = getImage (getDocumentBase(), "globe.jpg");

2 Comments

I've also seen ImageIO.read((getCodeBase(), "relative/path/to/globe.jpg")) but that's probably going beyond the scope of the question ;)
@MadProgrammer That is better for error reporting, I just thought I'd stick closer to the form of the original post. Also, those relative paths ore often tricky for newbies, hence the 2nd suggestion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.