Link to home
Start Free TrialLog in
Avatar of HoJu
HoJu

asked on

Keeping the background when painting a JPanel

I've derived a class from JPanel, and I want to draw in it and keep its background black, not gray. Calling

setBackground(Color.black);

in the constructor works fine if I don't override paint(). But if I do override paint(), the background color disappears and I can't figure out how to just keep it around and draw over it. Short of simply drawing a big rectangle over the whole panel, what should I be doing to keep the background color?
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of heyhey_
heyhey_

There is a very nice article at Swing connection (javasoft) about painting in AWT and Swing.

take a look at it.
the best answer to your question is:
you should never override paint() in Swing components - you should override paintComponent().