I see so many things wrong with that method...
1) you are missing the call to super.paint(g). Without it the previously painted contents will not be thrown away.
2) unless the Canvas is not part of a user interface or it's parent container has no layout manager the call to setSize does nothing.
3) setBackground shouldn't be called from the paint method but before it's ever called. This call could (it doesn't know for as far as I know but it could in the future) cause a repaint, which in your case would lead to a StackOverflowError.
In fact, only the if statement with contents is right.
Jesus Angeles wrote:
Ulf Dittmer wrote:What kind of object is "image" - can't you use that directly?