title: Stop Java canvas from clearing on repaint
body: <p>The following code just brings up a canvas in a window which just fills its content with red. However, when resizing the window it flickers a lot, because before each repaint the canvas appears to clear itself. After a bit of trivial searching it seemed to be because the <code>update()</code> method called <code>g.clearRect()</code>, but I've overriden that now and the flicker remains there, the canvas is still clearing itself before repaint.</p>
  
  <p>I've played around with double buffering and that as far as I can tell that doesn't seem to fix things - I'm not sure how helpful it'd be anyway since the issue appears to be more with preventing the canvas from clearing before its repaint.</p>
  
  <p>As an aside before everyone rushes in and suggests it, I <em>have</em> to use Canvas in this instance, not JPanel, because at a different point in time I'm using the same Canvas for native video playing with VLCJ.</p>
  
  <pre><code>public class MyCanvas extends Canvas {
  
      @Override
      public void update(Graphics g) {
          paint(g);
      }
  
      @Override
      public void paint(Graphics g) {
          //By the time we get here, the canvas has been cleared to its background colour
          g.setColor(Color.RED);
          g.fillRect(0, 0, getWidth(), getHeight());
      }
  
      public static void main(String[] args) {
          JFrame frame = new JFrame();
          MyCanvas c = new MyCanvas();
          frame.add(c);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);
      }
  }
  </code></pre>