title: Java applet stutters on first time drawing a BufferedImage (Java2D)
body: <p>I am writing a game as a java applet using the Java2D libraries along with the BufferedImage class. The game stutters (2-3 seconds) the very first time an image is drawn. The BufferImage files are all loaded before the game begins, using this method:</p>
  
  <pre><code>bufferedImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);       
      try {
             URL url = new URL(a.getCodeBase(), "images//smoke5frames.png");
             bufferedImage = ImageIO.read(url);           
      } catch (IOException e) { e.printStackTrace(); }        
      mSmoke = bufferedImage;
  </code></pre>
  
  <p>To draw the image, I am using this method:</p>
  
  <pre><code>public void draw(Graphics2D g) {    
  g.drawImage(mTextures[iCurrentFrame], mAffineTransform, null); }
  </code></pre>
  
  <p>The images in question are SheetedSprites, typically cut from a sprite sheet using:</p>
  
  <pre><code>for each frame in animation...
  
  myBufferedImage.getSubimage(i * iFrameWidth, 0, iFrameWidth, iHeight);
  </code></pre>
  
  <p>Which is done during game play when the entity is created. Removing this line did not solve the problem, so I am assuming (perhaps poorly) getSubimage() isn't expensive enough to cause the issue.</p>
  
  <p>I could loop though each and every frame of animation and draw them all for a quick-fix, but I'd like to understand more about the problem and why it happens. </p>
  
  <p>The kicker is that once the SheetedSprite has been drawn once, you can refresh the page and the problem doesn't occur again. The game runs perfectly smoothly after everything has been drawn before. This is the case in both IE and Chrome (I haven't tried it on any other browsers). As a side note, the problem never occurs in Eclipse's Applet Viewer.</p>
  
  <p>My best guess is that the images are somehow cached by the browser, though really I'm stumped. I haven't tried loading/drawing images with a different set of graphical libraries either (Slick2D, LWJGL, etc). Thanks for any suggestions!</p>