Canvas clears when resizing window
<p>I am trying to make a paint program and I have this problem with my canvas, it clears every time I resize or maximize the window containing it. I really have no ideea where is the problem, the paint() and repaint() methods are not overriden in the canvas class, and I don't use any addapter for resizing the window. Any help would be much appreciated.</p>

<p>Here's the code:</p>

<pre><code>public class Plansa extends Canvas{
Image image;
Pencil pencil;
public Plansa(){
    this.setSize(800, 600);
    this.setBackground(Color.white);
    pencil = new Pencil(this);
    addMouseListener(pencil);
    addMouseMotionListener(pencil);
}
public Plansa(int width, int height){
    this.setBounds(0, 0, width, height);
    this.setBackground(Color.white);
    pencil = new Pencil(this);
    addMouseListener(pencil);
    addMouseMotionListener(pencil);
}
public Plansa(Image imag) {
    this.setBackground(Color.white);
    this.setSize(imag.getWidth(this), imag.getHeight(this));
    image = imag;
    this.image = imag;
    this.repaint();
    pencil = new Pencil(this);
    addMouseListener(pencil);
    addMouseMotionListener(pencil);  

        }


public Dimension getPreferredSize() { 
    if(image==null)  
        return new Dimension( 800, 600 );  
    int w = image.getWidth( this );  
    int h = image.getHeight( this );  
    return new Dimension( w, h );  
 }

}



public class Fereastra extends JFrame{
private Plansa plansa;

public Fereastra () {
    super( "***Painter***" ) ;
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() - this.getWidth())/2);
    int y = (int) ((dimension.getHeight() - this.getHeight())/2);
    this.setLocation(0, 0);
    this.setSize(dimension);
    plansa = new Plansa(this.getWidth(), this.getHeight());

    //...

    setDefaultCloseOperation(EXIT_ON_CLOSE);

    add (plansa, BorderLayout.CENTER ) ;
    pack();
}

}
</code></pre>
