Click to See Complete Forum and Search --> : Graphics - Canvas Repaint


Manik
September 20th, 2000, 04:57 AM
Hi,

My application calls a class derived from the Canvas class. When I click on a draw button in my application, a new instance of this class is created and a grid is redrawn.

I m facing a problem in this redrawing. Sometimes, when I click on the draw button, the paint method of the canvas class is executed, but it does not draw anything. However, if I minimise my window and get it up again, it redraws itself.. I don't know why sometimes the paint is executed and at other times it doesn't, since this kind of a behaviour is randomly observed.

If anyone knows the reason, please help... it is very urgent.

Thanks and regards,
Manik

dogbear
September 21st, 2000, 04:28 AM
Manik,

The answer is both simple and difficult:

Along with the paint() method, you also have the repaint() method, which simply calls the paint() again.
The other method to pay attention to is the update() method.
You will notice that the update() method takes a Graphics object as a parameter, just like the paint() method.

You must override these two other methods to properly control your drawing.
The difficulty with these three drawing methods is knowing which one is called and when.

To figure this out, simply do System.out.println within each of these methods. That way you can have a log of which method is called.

Hope this helps.

Regards,


dogBear

PS Please Rate this Response!!!!

Phill
September 21st, 2000, 04:58 AM
Hi
When you call repaint(), the JVM' event Thread
calls the Components update() method which
clears the Clipping rectangle in the Components
background color, and then calls the Components
paint method.
As dogbear suggested you should override update()
otherwise the Components super update() method
will be called, and that method wont do anything.

-----------------

public void paint(Graphics g){
drawYourStuff
}

public void update(Graphics g){
paint(g);
}




This is all you have to have in the update()
method.

Good luck
Phill.

Manik
September 22nd, 2000, 06:05 AM
Thanks Phill and Dogbear, for ur response...

I tried overriding the update method.. Actually what is happening in my case is that the paint method is being executed properly... that is all the graphics.draw statements r being executed, but it is not showing up on the screen...

this is a very random behaviour... it does the drawing most of the times, but once in around 20 times it just shows a blank background.. if i click on the canvas, everything reappears... i can't figure out why...

thanks and regards,
Manik