Issue with Graphics.setAlpha [message #222138] |
Fri, 25 August 2006 10:53  |
Eclipse User |
|
|
|
Originally posted by: cricard.businessobjects.com
Hello everybody,
I currently have a problem displaying some text on an alpha-blended figure.
At first, I thought the draw2D Label was doing something bad in its
paintFigure(), so I tried to minimize the risk by only writing the two or
three instructions I was interested in... to no avail.
All I want to do is have a translucent rectangular figure that displays some
text, and reacts to clicks (e.g. each one toggles its selection state,
rendered out by appropriately turning it opaque or not).
Here's the rather straightforward code:
class ToggleFigure extends Figure {
public void ToggleFigure () {
setForegroundColor(ColorConstants.red);
setBackgroundColor(ColorConstants.lightGray);
setBorder(new LineBorder());
setOpaque(false);
}
@Override
protected boolean isMouseEventTarget() {
return true;
}
@Override
public void handleMousePressed(MouseEvent event) {
setOpaque(! isOpaque());
}
@Override
public void paintFigure(Graphics graphics) {
graphics.setAlpha(100);
if(isOpaque())
graphics.fillRectangle(bounds);
graphics.translate(bounds.x, bounds.y);
graphics.drawString("Text", 0, 0);
}
}
The attached .png depicts my instanciating two such figures, and what
happens when I click on the first figure, then on the second: the latter
displays a badly scaled "Text" string.
Clicking on it again and again (thus selecting and unselecting it) doesn't
get rid of the unappropriate scaling, although repaint() is obviously
called.
However, if right after the bug I repeat the whole operation (click first
then second), or provoke an update by dragging another window over the badly
displayed text, the painting is OK again... until the next bug.
Removing the graphics.setAlpha(100) instruction solves the problem, at the
expense of alpha-blending of course.
However, drawing the string *then* filling the rectangle (something the
Label class cannot afford because it must do its job independently of alpha
painting) works fine under all circumstances:
@Override
public void paintFigure(Graphics graphics) {
graphics.setAlpha(100);
graphics.translate(bounds.x, bounds.y);
graphics.drawString("Text", 0, 0);
graphics.translate(-bounds.x, -bounds.y);
if(isOpaque())
graphics.fillRectangle(bounds);
}
Moral of the story: don't rely too much on alpha-blended Labels, and even if
it's the case pay extra attention to the order in which you call
fillRectangle and drawString (or drawText, for that matter).
Anyway, it would be greatly appreciated to have this bug solved, because
colleagues have already experienced several seemingly related issues (for
which a workaround fortunately exists).
Christophe.
Attachment: ZoomBug.png
(Size: 1.34KB, Downloaded 255 times)
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04921 seconds