how do i clear all the shapes i've drawn on a panel using the graphics2d object?

Posted on 2011-02-20
Last Modified: 2012-05-11
how do i clear all the shapes i've drawn on a panel using the graphics2d object?
Question by:wrynn
LVL 47

Expert Comment

ID: 34940172
You can say clear Rect over the whole compouonent
LVL 47

Accepted Solution

for_yan earned 333 total points
ID: 34940178
g2d.clearRect(0,0,this.getWidth(), this.getHeight()) should work
LVL 92

Assisted Solution

objects earned 167 total points
ID: 34940229
typically you don't clear. for example each time the paintComponent() method of a JComponent is called the 'canvas' is already blank.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 47

Assisted Solution

for_yan earned 333 total points
ID: 34940446
I guess in order to claer your component in the class where you have
paint method you set up a boolean value, say,
and then  
you just say

  and in you paint(Graphics g) method first thing
you check this boolean
 and return if it is set:


  Then if you need to blank your component, you
just say:


and it  should clean the panel or canvas for you


Author Comment

ID: 36156857
I figured out that I had to draw a white rectangle over the entire screen at the beginning of my paint method.
LVL 27

Expert Comment

ID: 37645653
I've requested that this question be closed as follows:

Accepted answer: 0 points for wrynn's comment #36156857

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
LVL 47

Expert Comment

ID: 37645654
I think three comments should be considered to be a solution
ID: 34940178 (166 points)
ID: 34940229 (166 points)
ID: 34940446 (166 points)

Any one of these can be considered as an accepted solution, the other two
are assisted solution

The comment by wrynn  ID: 36156857
in fact repeats the solution which was mentioned above
LVL 27

Expert Comment

ID: 37646676
Fine with giving points to experts.  The solution used by the asker is not the same as the suggestions by for_yan, hence the original assignment to the asker.  But experts worked at it, so giving points to them is fine.  The first answer from objects is the correct one, but not what the asker used, so hard to say here.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert String matching to regex in java 4 51
How do I remove an object from a 3 40
SequenceInputStream example 3 19
Eclipse Java import and method not resolved 4 52
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question