title: Java - Use loaded Image multiple times
body: <p>I'm trying to load and a bunch of Images into a program and repaint them 60times per second.My Problem is that I get a CPU usage of over 20% and the program isn't even finished.</p>
  
  <p>This is the loop that load's the Image:</p>
  
  <pre><code>    for(int i = 0;i &lt; LevelCompiler.Objectlenght;i++) {
          LevelCompiler.objects[i].drawObjects(g);
      }
  </code></pre>
  
  <p>and this is the drawObjects methode:</p>
  
  <pre><code>g.drawRect(x, y, width, height);
      g.drawImage(TextureSystem.textureSystem(TextureID), x, y, width, height, null);
  </code></pre>
  
  <p>The textureSystem methode that load's the imgae:</p>
  
  <pre><code>    import java.awt.image.BufferedImage;
  import java.io.IOException;
  
  import javax.imageio.ImageIO;
  
  public class TextureSystem {
  static String TexturePath;
  static BufferedImage img;
  
  public static BufferedImage textureSystem(int tid) {
      TexturePath = "/textures/Objects/SObject+" + tid + ".jpg";
      try {
          img = ImageIO.read(ResourceLoader.load((TexturePath)));
      } catch (IOException e) {
          e.printStackTrace();
      }
      return img;
  
  }
  }
  </code></pre>
  
  <p>and this is the ResourceLoader from above</p>
  
  <pre><code>import java.io.InputStream;
  
  public final class ResourceLoader {
  
   public static InputStream load(String path) {
      InputStream input = ResourceLoader.class.getResourceAsStream(path);
      if (input == null) {
          System.out.println("error");
      }
      return input;
  }
  
  }
  </code></pre>
  
  <p>I assume that the high CPU usage is because I'm loading the image again and again.Any suggetions how for another methode?</p>