9 Replies Latest reply on May 2, 2018 8:59 PM by stinkfoot70

    Problems with exporting to jar with images in eclipse - getting instability

    stinkfoot70

      I made a cheesy pong game with images that pop up and other distractions. I accessed my images as so:

       

      zappa = new ImageIcon("/images/zappa2.jpg");

       

      (yes, a pony-tailed pic of frank zappa pops up to block part of the screen and then starts moving around as further distraction)

       

      I tried to export as a jar and a runnable jar, but either way, the program worked fine but no pictures showed up in my game.

       

      I read a bunch of stackoverflow forums and found something that semi-worked. I changed the path to:

       

      zappa = new ImageIcon(getClass().getResource("/images/zappa2.jpg"));

       

      When I exported to a jar, the pictures showed in the game! However, whenever I comment out my old path and use the new getResource path, the game becomes very unstable. It only loads a rare number of times out of a set of attempts. In both eclipse and the exported jar, most of the times I try to load the program, I just get an empty frame, and on some occasions, I get game play, but my paddle is unresponsive to the mouse.

       

      I want the stability of my first path, but the pictures exporting correctly of my second path.

       

      Suggestions?

       

      Also, what do I want: jar or runnable jar? This is my first GUI program. Previously, I've made text programs, exported to jar, and ran them thru terminal.

       

      Thank you, core

        • 1. Re: Problems with exporting to jar with images in eclipse - getting instability
          morgalr

          If you want you program to run, then you want a runnable JAR file.

           

          I cannot really comment on your problems with the animation without seeing your code.

           

          IMO: you have chosen the worst way to load your images--using ImageIcons, painting BufferedImages, that have been loaded with ImageIO, onto the Graphics contest of a JPanel is a much better way to go.

          • 2. Re: Problems with exporting to jar with images in eclipse - getting instability
            stinkfoot70

            Thanks, but none of the code I've searched since reading this is working. Could you provide some sample code for if I made a source folder called resources and inside of that I made a regular folder called images, and my jpg is inside the images folder?

             

            Does that sound right or am I getting a little folder crazy?

             

            You response sounds like the most hopeful advice I've gotten so far, so I'd really like to make this work.

            • 3. Re: Problems with exporting to jar with images in eclipse - getting instability
              stinkfoot70

              currently I'm using this line after those imageicon commands:

               

              zappa.paintIcon(this, g, 250, 250);

               

              I'm assuming that the command for displaying a buffered image would be different?

              • 4. Re: Problems with exporting to jar with images in eclipse - getting instability
                stinkfoot70

                actually, I'd like to change those last 2 questions. I found some code online that's working for me.

                 

                I made an images folder in the project and copied my image in and this worked:

                String path = "images/zappa2.jpg";

                 

                However, this doesn't export correctly (works fine in eclipse). I assume that's because I need to export with the image inside of source folder. How would I change that file path to get the same picture that's inside of a source folder called "resource", which inside has a regular folder called images? I've tried a bunch of varieties of "/src/resources/images/zappa2.jpg", but can't find the winning combination of folder names.

                • 5. Re: Problems with exporting to jar with images in eclipse - getting instability

                  actually, I'd like to change those last 2 questions. I found some code online that's working for me.

                   

                  I made an images folder in the project and copied my image in and this worked:

                  String path = "images/zappa2.jpg";

                   

                  However, this doesn't export correctly (works fine in eclipse). I assume that's because I need to export with the image inside of source folder. How would I change that file path to get the same picture that's inside of a source folder called "resource", which inside has a regular folder called images? I've tried a bunch of varieties of "/src/resources/images/zappa2.jpg", but can't find the winning combination of folder names.

                  You are REALLY doing yourself a disservice by not reading the documentation and using an official site for getting your information and sample code.

                   

                  The Java Tutorials has trails that cover ALL of the basic functionality and includes examples with WORKING code.

                   

                  For example a SIMPLE web search for 'the java tutorialss icons' returns the page for the trail about how to use icons in Java.

                  https://docs.oracle.com/javase/tutorial/uiswing/components/icon.html

                  Generally, applications provide their own set of images used as part of the application, as is the case with the images used by many of our demos. You should use the ClassgetResource method to obtain the path to the image. This allows the application to verify that the image is available and to provide sensible error handling if it is not. When the image is not part of the application, getResource should not be used and the ImageIcon constructor is used directly.

                  See how that paragraph tells you EXACTLY how to 'obtain the path to the image'?

                   

                  Try the example code in the tutorial. Then try applying that method to your own code.

                  • 6. Re: Problems with exporting to jar with images in eclipse - getting instability
                    stinkfoot70

                    Thanks for the link, but I've read this one before. I was using ImageIcon and it was working inside of Eclipse, but the images wouldn't export. The first reply to my question had me change tracks towards bailing on ImageIcon in place of using BufferedImages.

                     

                    Are you suggesting I return to an ImageIcon approach?

                    • 7. Re: Problems with exporting to jar with images in eclipse - getting instability

                      Thanks for the link, but I've read this one before.

                      But it doesn't sound like you did what I suggested. Did you?

                      Try the example code in the tutorial. Then try applying that method to your own code.

                      Reread that code example and look closely at HOW it works and the classes and methods it is using.

                       

                      Are you suggesting I return to an ImageIcon approach?

                      Again - READ the code example - I provided an excerpt from it. Did you see this? Did you try it?

                      You should use the Class getResource method to obtain the path to the image.

                      EVERYTHING in a jar file (classes, resources, files) is accessed RELATIVELY TO THE CLASSPATH of the jar file.

                       

                      That is what the 'getResource' method is used for.

                       

                      THERE ARE NO SHORTCUTS!

                       

                      For whatever reason many people, perhaps you too, just don't want to read the documentation, tutorials or the Java API to try to LEARN about functionality. They just start writing code and then can't understand why it doesn't work the way they think it should.

                       

                      A typical comment is 'I tried that example after making a few simple changes'. Well - you need to try WORKING code without making ANY changes. Then you have a baseline. Then make your 'simple changes' and when it doesn't work you know for certain it was due to one of your changes.

                       

                      Here is the doc link for the 'getResource' method.

                      https://docs.oracle.com/javase/7/docs/api/java/lang/Class.html#getResource(java.lang.String)

                       

                      It explains how the class loader is used to load classes/resources based on the classpath that has been established.

                       

                      If you can't get the resources into the jar file (export you say) in Eclipse then that is an Eclipse problem. Netbeans does it just fine.

                       

                      I suggest you try to adopt the methods the experts use:

                       

                      1. read the documentation for the functionality you want to learn about

                      2. find a tutorial/book that has WORKING code and actually try the examples on your own machine and environment

                      3. stay on step #2 until the examples work for you. If they don't work find out why and fix it

                      4. save/archive the ENTIRE PROJECT for the working examples so you can always go back to a KNOWN baseline if things go wrong later

                      5. make SMALL changes to the example to EVENTUALLY reach the code you need for your use case

                      • 8. Re: Problems with exporting to jar with images in eclipse - getting instability
                        stinkfoot70

                        I'll read back through that article and give it another shot. Honestly, I'm teaching AP Computer Science in a high school for the first time. I learned through codeHS last year and took a few AP seminars in the summer. However, once the school year started, things got hectic and I've only been able to grab from piles of resources trying to put fires out. This course is making me hustle so hard that it leave little time to expand any of the math courses I teach.

                         

                        From the chaos I'm existing in, I appreciate you stressing a return to official documentation. My resource research has devolved into band-aids.

                        • 9. Re: Problems with exporting to jar with images in eclipse - getting instability
                          stinkfoot70

                          morgalr,

                           

                          I switched to BufferedImage, figured out the path issue, and am now rocking! Thank you very much for the guidance!