what path to use to get from an outer class an image which is in the P5 data folder

edited September 2015 in Questions about Code

i have an app (P5, osX 10.6.8) which has a class extending JFrame class.

In this class i create a JFrame instance: let us call it "frame". Not any problem.

Then i want to add a panel to this frame So i code:

     JPanel panel = new JPanel();
     panel.setLayout(new BorderLayout());

    panel.setPreferredSize(new Dimension(wid, heigh));// these parameters are passed by constructor.

Not any problem: the frame appears (after having add pack() and setVisible()) with size and location as required.

Then i want to add an IMAGE to my panel; this image IS in the data folder from the sketch; i have also put a copy near the class itself at sketch root.

and i code:

    JLabel label = new JLabel();
    label.setIcon(new ImageIcon("/data/myImage.jpg");
    //or:(trying..)
    label.setIcon(new ImageIcon("myImage.jpg");
    //or
     label.setIcon(new ImageIcon("data/myImage.jpg");

then, as usually:

    this.frame.getContentPane().add(panel);
    this.frame.pack();
    this.frame.setVisible(true); 

the frame appears but not the image. Not any error.

In order to understand why, i add:

System.out.println(new File("myImage.jpg").exists());

it returns false (of course i have tried changing the path, without success)…

So i have add another method in order to see where the class is looking at:

            private void whereAreYouLookingAt(){
            try{
             File file = new File(".");  
             File[] files = file.listFiles();  
             System.out.println("Current dir : " + file.getCanonicalPath());
             for (int fileInList = 0; fileInList < files.length; fileInList++)  
             {  
                  System.out.println(files[fileInList].toString());  
                }
            }catch(IOException ex){
            }

            }

it runs and print all files which are present in my Applications folder (osX)!!! of course the image is not here... but WHY is it pointing there??? Is it because here is the Processing folder???? - i think so. Not sure...

After that (in order to verify again) i add::

System.out.println(new File("data/myImage.jpg").getAbsolutePath());

it returns: /Applications/data/myImage.jpg

Finally, in order to be sure that my code is good (it fires no error…) i have HARDCODED the path to myImage.jpg (sketch folder, data folder…) and the image appears in the frame!

Of course this is not a good solution... If anybody can explain what to do in this type of case (what path to use in an outer class which uses Image java class and not PImage) thanks in advance.

PS: i have found a workaround but i want to understand. thanks in advance.

Tagged:

Answers

  • PS: i have written "hardcoded"; i must add that this "hardcoded path" was returned calling loadPath(""); and obviously (for me!) this path cannot work on another computer than mine one; but perhaps, when compiling, P5 changes it to relative??? - i ll try

Sign In or Register to comment.