I need to load an image in my program, but I keep getting this error:

C:\Documents and Settings\Remy B\Desktop\programming\Java\Java Projects\NurseGUI\GraphPanel.java:30: cannot resolve symbol
symbol  : variable danger 
location: class GraphPanel
        danger = getImage(getDocumentBase(), "danger.jpg");

I'm assuming this is because you can only use getDocumentBase with applets? I googled, but I couldn't find a way to do the same thing if you aren't using an applet.

Recommended Answers

All 3 Replies

Try using System.getProperty("user.dir") or System.getProperty("home.dir") if you want the current directory or if you want the directory where that class file also resides, then try getClass().getClassLoader().findResource("danger.jpg") Read the API for System and ClassLoader for a more detailed description.

Thank you.

However, I think I may have misdiagnosed my problem. Since I can't seem to use getImage or getAudioClip even when I specify the file path directly. When I went to sun's documentation about those commands, they come up under the "applet" class? What should I do to load images/sounds if I don't want to make the program an applet?

Thanks

For Audio check out the AudioInputStream class and its getAudioInputStream(File) and getClip() methods, and Clip's open(AudioInputStream) and play()/loop() methods.

For an Image see the read(File) method of ImageIO.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.