[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2409
  • Last Modified:

using image paths in Java within NetBeans or another IDE

Hi,

I'm working with an application that requires me to load image files from a folder. The current code uses the following statements to set image paths,

 loadImage("../images/background.jpg", null);

This is supposed to allow a user to place the images folder at the same level as the working directory. For example, if the working directory is,

c:\test\program

then the images folder would be placed at,

c:\test\images

The problem is that I've tried this in NetBeans and other IDEs and it will not load the image files. In NetBeans, for example, I would place the image files in the run folder det for the project, but this still will not load the images. Does anybody know why this might be happening, and how to solve this problem?

Thanks for the help
0
nhay59
Asked:
nhay59
  • 12
  • 6
1 Solution
 
zzynxSoftware engineerCommented:
What is the working directory of netBeans?
Whare did you create the images directory?
0
 
zzynxSoftware engineerCommented:
>> What is the working directory of netBeans?
In version 3.6 you can set this in Tools > Options

e.g. if the working directory is C:\work
You should have a C:\Images directory containing your image files
0
 
zzynxSoftware engineerCommented:
>> In NetBeans, for example, I would place the image files in the run folder det for the project
Incorrect I'm afraid. See my previous comment.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
nhay59Author Commented:
Hi,

For this project, the source package folder is set to c:\game and the .java files are stored in c:\game\invaders. I've placed the images folder in c:\game\images.

Thanks
0
 
nhay59Author Commented:
Hi,

I've just checked, and the working directory for this project is set to c:\game. I then have the .java files for the code in c:\game\invaders and the images folder is c:\game\images. It still will not load the images.

Thanks for the help
0
 
zzynxSoftware engineerCommented:
>> the source package folder is set to
>> the .java files are stored in
Doesn't matter.
All that counts is **the working directory** of NetBeans (or any other IDE)
0
 
zzynxSoftware engineerCommented:
>> the working directory for this project is set to c:\game
Then you need to place your image files in the directory C:\images
0
 
zzynxSoftware engineerCommented:
>> loadImage("../images/background.jpg", null);

1) Working dir              = C:\game
2) ..                              = C:\
3) /images                    = C:\images
0
 
nhay59Author Commented:
Hi,

Thanks for the reply. I've now set the working directory in Netbeans to c:\game\invaders, this is where the .java files are located. I have then moved the images folder to c:\game\images. However, the images will still not load when I run the program. Have I missed something, or is there something wrong with the code for the program?

Thanks
0
 
zzynxSoftware engineerCommented:
>> I've now set the working directory in Netbeans to c:\game\invaders, this is where the .java files are located.
The working directory of NetBeans can be whatever you want. Unrelated to where your java files are.
But, c:\game\invaders is OK.

>> loadImage("../images/background.jpg", null);
I can't find any loadImage() method that takes two parameters.
Can you tell me what class has that method?
0
 
nhay59Author Commented:
Hi,

Here is the method for loadImage. It is within the Images class for this program,

public Image loadImage(String path, Color c)
  {
    Image image = Toolkit.getDefaultToolkit().getImage(path);
    addToMediaTracker(image, path);
    if(c!=null)
    {
      image = GeneralUtil.setTransparentColorOld(image, c);
    }
    waitForImage(image);

    int pos = path.lastIndexOf("/");
    if(pos>=0) images.put(path.substring(pos+1), image);
    return image;
  }

Thanks for the help.
0
 
zzynxSoftware engineerCommented:
Could you try this (Swing) code snippet?
For me it works as expected.
That is: 1) If the file note.gif is in C:\work\pix it is displayed
             2) if the file is not, well... it is not displayed


          ImageIcon icon = new ImageIcon("pix/note.gif");
          JOptionPane.showConfirmDialog(null, "Hello", "Title", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, icon);
0
 
zzynxSoftware engineerCommented:
So, have a MyTester.java file containing:

public class MyTester {

      public static void main(String args[]) {
          ImageIcon icon = new ImageIcon("images/background.jpg");
          JOptionPane.showConfirmDialog(null, "Hello", "Title", JOptionPane.OK_OPTION, JOptionPane.INFORMATION_MESSAGE, icon);
      }
}

and run it. Tell me if you see the image or not
0
 
zzynxSoftware engineerCommented:
Sorry. You'll need

import javax.swing.*;
0
 
nhay59Author Commented:
Hi,

If I set the image path to ../images/background.jpg then the window loads correctly with hello and the buttons.

Thanks for the help
0
 
zzynxSoftware engineerCommented:
>> If I set the image path to ../images/background.jpg then the window loads correctly with hello and the buttons.
OK. That proves that the path is correct and everything (incluing NetBeans working directory) works as expected.
Now, you have to find out why your code doesn't work.

Remark: it's not because you don't *see* the image in your app, that it is not found by loadImage()
0
 
nhay59Author Commented:
Hi,

That's great. One problem knocked off the list.

Thanks for all the help
0
 
zzynxSoftware engineerCommented:
Thanx 4 axxepting
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 12
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now