-2

The image "Pic.jpg" is originally in "C:\Users\qwerty\Documents\NetBeansProjects\SelfTestX\src\Java\image".

It didn't work. I read up on getResource() and it actually reads from where the .class files are stored at.

So, I copied the same image and pasted it at "C:\Users\qwerty\Documents\NetBeansProjects\SelfTestX\build\classes\Java\image"

It didn't work either.

Any help is greatly appreciated.

I listed the essential code below. Hopefully it is short enough.

            public void setImage() throws IOException{
            URL img=getClass().getResource("image/Penguins3.jpg");
            BufferedImage bi=ImageIO.read(img);
            int w=bi.getWidth();
            int h=bi.getHeight();
            int count=0;
            for(int i=0;i<3;i++){
                for(int j=0;j<3;j++){
                    BufferedImage wi=bi.getSubimage(i*w/3,j*h/3, w/3, h/3);
                    Image sc=bi.getScaledInstance(puzpiece.getWidth()/3,


                   puzpiece.getHeight()/3, Image.SCALE_AREA_AVERAGING);
                    setupImage(count++,sc);
                }
            }
            }


            private void setupImage(int a,Image wi) {
            button[a]=new JButton(new ImageIcon(wi));
            }
iridescent
  • 305
  • 4
  • 14

1 Answers1

1

If your project does not have a separate resource folder, place Pic.jpg into the same folder where the java file (.java) of the current class is (or the proper subfolder in the source tree structure). IDE should copy it where expected, in majority of cases by default.

Complex projects have configured folders just for resources but I assume this is not the case.

Audrius Meškauskas
  • 20,936
  • 12
  • 75
  • 93
  • Yup, I did that and it still didn't work. I think it could because of something that happened downstream like what the other person who answered mentioned.. ( the other answer got deleted? ) – iridescent Jan 15 '13 at 10:31
  • I made some edits to code and now getImage() is giving errors. Full code in this link -- http://pastebay.net/1174312 – iridescent Jan 15 '13 at 12:18
  • One of the possible explanations I see, the path image/Pic.jpg may not make a complete path. Have you tried Java/image/Pic.jpg and which package declaration, if any, do you have for your class? – Audrius Meškauskas Jan 15 '13 at 15:01