lochi
lochi

Reputation: 880

Reset Graphics in JPanel

Following Code Draws a structure of a molecule. If I don't pass a molecule the last structure continue to show up in the JPanel. How do I reset this to a blank canvas when there is no molecule. In other words what should I put inside else{} of drawMolecule() method?

Upvotes: 1

Views: 2468

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You could create a new BufferedImage in the else or you could get the BufferedImage's Graphics2D component, set its background color via setBackground(Color c) and then clear the image via clearRect(...) then dispose of the Graphics2D object when done via its dispose() method.

In fact, on review of your code I don't see you doing this. Don't forget to dispose of the Graphics or Graphics2D resource when done using them, if you've created them yourself (not if obtained from the JVM say via its paint or paintComponent method).

Upvotes: 2

Related Questions