Search code examples
javaimageperformanceprocessing-efficiencymemory-efficient

most efficient way to set an image


There must be some more efficient way to set an image isn't there? Or is this as good as it gets?

     public Image setImage(String img){
        ImageIcon imageIcon = new ImageIcon(setURL("Images/" + img));
        Image image;
          image = imageIcon.getImage();
          //imgWidthTemp = image.getWidth(null);
          //imgHeightTemp = image.getHeight(null);
          return image; 
     }

    public URL setURL(String url){
       String Url = url;
       try{
        URL imgURL = getClass().getClassLoader().getResource(url);
        if(imgURL != null){
           return imgURL;
        }
       }catch(NullPointerException e){}
         return null;
     }

I really don't need the setURL() method, it just made things cleaner and easier to handle. I just want to know if there is a more efficient way of setting a custom image if at all. It is still a lot of code to write even without those two methods. Does it get any simpler? I mean why can't we just make a new image instead of first making an ImageIcon and then saying Image myImage = ImageIcon.getImage(). Not complaining, but that would be alot of code without a method or two if you have 2000+ images to set(take minecraft for example huh? that's quite a few images if I says so myself). And videogames mostly just have tons of images rather than the code being only like 30% of the code sometimes.


Solution

  • It is not clear what you are really trying to do here, but the Java Tutorial has a section on working with images that includes material on loading images. Read it for code that shows the recommended way to do it.

    I'm not an expert on this, but I would expect that the direct way recommended by the tutorial will be faster than the indirect approach of using ImageIcon. (But I haven't benchmarked it!!)


    There are other things wrong with your code. In particular, the setUrl method is just bizarre.

    • The name setUrl is a misnomer. The method is not "setting" anything. Rather, it is attempting to map a pathname to a resource URL.

    • The try-catch is unnecessary. Nothing in that code should be throwing an NPE.

    • If you remove the unnecessary try-catch, then the logic boils down to this:

          URL imgURL = getClass().getClassLoader().getResource(url);
          if (imgURL != null) {
             return imgURL;
          } else {
             return null;
          }
      

      That's crazy / silly stuff! Just do this:

          return getClass().getClassLoader().getResource(url);
      

    Finally, there is an important point that you need to understand about practical optimization.

    It is a well known fact that people's intuition on whether a program will be fast or slow, and what makes it fast or slow ... is poor. Rather than relying on your intuition (i.e. guessing what the problems are, or are going to be), it is better to do the following:

    1. Get your program working.
    2. Measure how fast it is doing a real task. This is called benchmarking.
    3. Ask yourself if it is already fast enough. If it is, then don't waste your time optimizing.
    4. Use a profiler to find out where the performance hotspots or bottlenecks in the code are.
    5. Optimize the sections of code that have been identified as bottlenecks. Don't spend time optimizing code that isn't a bottleneck, because that effort will be wasted.