Jump to content
  • Advertisement
Sign in to follow this  
xxDAFFOxx

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

This topic is 2544 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!