title: Java optimizing painting a background image for a game
body: <p>I'm working on recreating "Legend of Zelda a Link to the Past" as part of an assignment.</p>
  
  <p>And I've got the following problem, the game world is a BufferedImage of which I subImage() the part I need for the current location of the player. The game works but uses 80 / 110 percent of the CPU. A profile revealed that the culprit is the drawing of the image.</p>
  
  <p>So I figured I put the background in a separate JPanel from the Player, enemies etc JPanel. 
  Render them on top off each other (JLayeredPane) and repaint the background panel far less often. </p>
  
  <p>But how do I do this how do I tell swing to draw one panel x times a sec and the other y times? If you have a better way of optimizing let me know.</p>
  
  <p>Here's what I've got at the moment:</p>
  
  <pre><code>public class Main extends JFrame
  {
      private ZeldaGame game = new ZeldaGame();
      private View view = new View(game);
      private BackgroundView bckView = new BackgroundView(game);
      private Controller ctl = new Controller(game, view, bckView, this);
  
      public Main()
      {
          setLayout(null);
  
          view.setBounds(0, 0, game.getWidth(), game.getHeight());
          bckView.setBounds(0, 0, game.getWidth(), game.getHeight());
  
          JLayeredPane pane = new JLayeredPane();
          pane.add(bckView, 1);
          pane.add(view, 0);
  
          setLayeredPane(pane);
  
          setSize(game.getWidth(), game.getHeight());
          setResizable(false);
          setLocationRelativeTo(null);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
          setVisible(true);
      }
  
      public static void main(String[] args)
      {
          new Main();
      }
  }
  </code></pre>
  
  <p>Thank You. </p>