Trouble with a Simple Java Applet's Code for Displaying An Image

Started by
1 comment, last by xxDAFFOxx 12 years, 3 months ago
[font=comic sans ms,cursive]

Hi guys, I have encountered a problem with my code i am using to try and get a image to display on the screen.[/font]

[font=comic sans ms,cursive]

I am using the Netbeans 7.0.1 IDE and the newest version of JDK.[/font]

[font=comic sans ms,cursive]

This is my Code:[/font]

[Source Java]

import javax.swing.*;
import java.awt.*;


public class ShowImage extends JApplet
{
private Image image;

public void init()
{ image = getImage( getDocumentBase(), "nebula.jpg"); }

public void paint(Graphics g)
{ g.drawImage(image, 0, 0, this); }
}

[font=comic sans ms,cursive]The code doesn't come up with any errors or issues itself, the applet window appears when I run the code but no image is displayed, so I think it is something to do with where the image is placed. I shall include some screencaps to help you out. All help is appreciated, Thanks.[/font]

Ok Images wont upload so I will have to explain. The image and is in the Directory C:\Users\Ash\Documents\NetBeansProjects\Displaying an Image\src\nebula.jpg. This is also where the .java file is stored.

Advertisement
I think you want to replace getDocumentBase with getCodeBase, as the former returns the location of the document that contains the applet, and I am not entirely sure what this means in the context of your applet. getCodeBase() returns the location of the applet (but this is probably where NetBeans puts the .class file).
That worked perfectly. Thanks mate, I just wondered why the book I took the example from (I'm working on the examples in it, its called Killer Games Programming in Java by O'Reilly) would not work. Also I think the book uses JDK 5, couldnt find anything better, but maybe getDocumentBase() has been replaced with getCodeBase() or they expect me to have made a website to store the images and that on?

This topic is closed to new replies.

Advertisement