I'm having a strange problem whilst trying to do something that should
be fairly trivial.
My applet hierarchy looks like this (somewhat simplified):
JFrame (main window)
|
- JPanel1 (main panel within the frame)
|
- JLabel1 (used to show current coordinates)
- JPanel2 (used as a drawing canvas)
I have a complex drawing rendered in JPanel2. I then let the user select
things inside
it using a rubber-band rectangle. I draw the rubber-band in XOR painting
mode, so that I
do not have to keep redrawing the background - I can just overwrite the
reactangle to
erase it and then draw it in it's new position. This all works fine.
The problems start when I attempt to update JLabel1 to show e.g. the
current coordinates
of the rectangle. What happens is that the label is correctly updated,
but a copy of the
label is drawn into the top left of the JPanel2 area, damaging the
background image.
This would be no
problem if I re-rendered the background all the time, but that would
remove the need for
using XOR painting to speed things up (and Swing is slow enough as it
is).
At first I couldn't work out where this copy of the label was coming
from, but after a little
investigation it seems that this is "normal" Swing behaviour.
>From I understand, each Swing window (like a JFrame) has a single image
buffer that is used
for all the GUI rendering. The components are all drawn into this
buffer, and eventually
blitted to the screen to show the complete frame (normal Swing
double-buffering).
I believe that what is happening is this:
When I draw my complex background, it is actually drawn into the top
left (0,0) of the
Swing back-buffer, and then blitted to the correct place in the frame
when the GUI is
refreshed.
When I set the text in the label to reflect the size of the rubberband
box, Swing draws
the label into the top left of its background image buffer, which
currently contains my
background drawing. This damages the drawing.
Then I want to move the rubberband box around. Because I do not redraw
the scene, the
Swing backbuffer still contains the original, damaged drawing. My
rubberband box is drawn correctly, and the Swing double-buffering
mechanism then shows JPanel2, complete with
the copy of JLabel1 (that I obviously do not want).
The object that controls the window has a method called setStatus(String
text) which
simply sets the text in JLabel1. The object that handles JPanel2 calls
myparent.setStatus(text)
to update the label. I've tried doing it in the paint() method, the
mouse-event methods,
and using a SwingWorker thread to call setStatus, but they all produce
the same result.
(I hope I've explained this well enough for others to make sense of it,
because it
confused the hell out of me until I worked out what's going on.)
The workaround I have at the moment is to show the status in the the
JFrames title, but
that is not an ideal solution.
Am I doing something wrong here? Surely it is fairly easy to draw an XOR
rectangle around
a panel and update a label at the same time, without having to
completely repaint the background
each time?
Any hints would be much appreciated.
Adrian Barnett
=====================================================================
To subscribe/unsubscribe, send mail to [EMAIL PROTECTED]
Java 2D Home Page: http://java.sun.com/products/java-media/2D/