How to clearJPanel?

  • Thread starter Adrian M. Hanslik
  • Start date
A

Adrian M. Hanslik

Hello there,

I've got a JPanel on which I draw GeneralPaths.
As it's a bit too complex sometimes,
I'd like to clean the JPanel on which I draw
before I draw a new GeneralPath...

The code below shows my current solution...

<CODE>
super.paintComponent(g);
((Graphics2D) g).scale(zoom, zoom);

g.setColor(Color.white);
g.clearRect(0, 0, 800, 600);

double dummy = width / (2 * Math.PI + Double.MIN_VALUE);

BasicStroke simple = new BasicStroke((float) (1 / dummy));
((Graphics2D) g).setStroke(simple);

((Graphics2D) g).setColor(new Color(128, 128, 128));
((Graphics2D) g).draw(grid);
((Graphics2D) g).draw(door);

((Graphics2D) g).setColor(Color.blue);
((Graphics2D) g).draw(path);
</CODE>

What's wrong with that?
I also tried fillRect instead of clearRect,
but it didn't worked, either.

Kindest regards
Adrian
 
R

Rhino

Adrian M. Hanslik said:
Hello there,

I've got a JPanel on which I draw GeneralPaths.
As it's a bit too complex sometimes,
I'd like to clean the JPanel on which I draw
before I draw a new GeneralPath...

The code below shows my current solution...

<CODE>
super.paintComponent(g);
((Graphics2D) g).scale(zoom, zoom);

g.setColor(Color.white);
g.clearRect(0, 0, 800, 600);

double dummy = width / (2 * Math.PI + Double.MIN_VALUE);

BasicStroke simple = new BasicStroke((float) (1 / dummy));
((Graphics2D) g).setStroke(simple);

((Graphics2D) g).setColor(new Color(128, 128, 128));
((Graphics2D) g).draw(grid);
((Graphics2D) g).draw(door);

((Graphics2D) g).setColor(Color.blue);
((Graphics2D) g).draw(path);
</CODE>

What's wrong with that?
I also tried fillRect instead of clearRect,
but it didn't worked, either.
What do you mean "it didn't work"? Did you get compile errors? Runtime
errors? Or did it just not behave the way you wanted? If you got errors,
please specify what they messages said.

Rhino
 
C

Chris Smith

Adrian M. Hanslik said:
I've got a JPanel on which I draw GeneralPaths.
As it's a bit too complex sometimes,
I'd like to clean the JPanel on which I draw
before I draw a new GeneralPath...

Sounds like you are doing painting all wrong to begin with. You
shouldn't be clearing your screen in paintComponent. That happens for
you with no effort on your part. To "clear" the screen, you would just
arrange things so that the next call to paintComponent won't draw
anything. Then you'd call repaint().

If that's not working, then you need to post enough code to tell how
you're doing the painting. A simple self-contained example would be
best.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top