title: How to efficiently paint custom JPanel as ViewportView of JScrollPane?
body: <p>I have a <code>JScrollPane</code> displaying (as its viewport view) <code>MyPanel</code>, a subclass of <code>JPanel</code>. </p>
  
  <p><code>MyPanel</code> implements custom painting by overloading <code>paintComponent</code>. The total size of the displayable content of <code>MyPanel</code> is generally quite wide (meaning 50x to 200x wider than the size of the <code>JScrollPane</code> viewport) and using a <code>Timer</code>, I scroll horizontally to view different sections of the underlying <code>MyPanel</code>. I also allow using the scroll bar thumb to manually seek to a specific area of <code>MyPanel</code>.</p>
  
  <p>In my <code>paintComponent</code> implementation I am currently finding the portion of <code>MyPanel</code> that is currently visible in the view port using <code>JViewport#getVisibleRect</code>, and just painting that portion each time the view port position is changed. </p>
  
  <p>This works fine - but I end up repainting a significant percentage of the visible portion of <code>MyPanel</code> over and over as the timed scrolling only moves the view port 1/50 of the view port width at a time. Also, I generally end up scrolling through the entire horizontal extent of <code>MyPanel</code>, so I have to paint it all at least once anyway.</p>
  
  <p>That leads me to think about painting the entire contents of <code>MyPanel</code> just once (to a <code>BufferedImage</code>?) and then letting <code>JScrollPane</code> (or <code>JViewport</code>) handle clipping and blitting only the needed area of the <code>BufferedImage</code>. </p>
  
  <p>Intuitively this seems to me to be the most efficient way of handling this, and something that would be relatively common.</p>
  
  <p>As I investigate the Swing tutorials and other sources, I learn that Swing is already double buffered. If I try to force this on my own brute-force, independent of Swing functionality, it sounds like I'll end up with triple-buffering. </p>
  
  <p>I haven't found the recipe (if it exists) to exploit <code>JScrollPane</code> to do this for me.</p>
  
  <p>Is there an example available, or some direction as to how to do this (if possible)?</p>