Re: Translucent AWT button: the parent doesn't redraw
Alexander Farber wrote:
My problem is that I can't get the background cleared and thus
I see the artifacts from the previously drawn button states.
It is not very clear what your actual problem is (you want to have some
transparency, but then you claim you still see-through). But there are a
few obvious things in the code.
Also I don't want to use Swing as my application is actually
an applet (a russian card game at http://preferans.de )
which should run in all browsers, including MSIE
If I am not mistaken, the MSJVM can't deal with translucent images.
AFAIR the ability to handle transparent images in some way was added in
Java 1.2 with the Java 2D API.
public void update(Graphics g) {
paint(g);
}
The original Canvas.update() first clears the background (fills it with
the background color). Since you claim you can't get the background
cleared, this overridden method might be one reason.
Canvas is a heavyweight component. All heavyweight components are
opaque. That is, they have to paint the entire screen-estate occupied by
them. If you remove the cleaning of the background in update(), you have
to ensure that your paint() redraws the entire area occupied by your
Canvas. Each and every pixel.
if (state != PRESSED) {
g.drawImage(shadow, OFFSET, OFFSET, this);
g.drawImage((state == ENABLED ? dark : lite),
0, 0, this);
// draw the label over the translucent image
g.drawString(label, h / 3, h * 2 / 3);
} else {
g.drawImage((state == ENABLED ? dark : lite),
OFFSET, OFFSET, this);
// draw the label over the translucent image
g.drawString(label, OFFSET + h / 3,
OFFSET + h * 2 / 3);
}
You sometimes leave OFFSET pixel in horizontal and vertical direction
unpainted, see above.
public Dimension getMinimumSize() {
FontMetrics metrics = getFontMetrics(getFont());
getFontMetrics() can return null under some circumstances. You might
want to safeguard against this.
public Dimension getPreferredSize() {
FontMetrics metrics = getFontMetrics(getFont());
See above.
public void update(Graphics g) {
paint(g);
}
See above. Frame is also a heavyweight. If you remove the background
clearing from update() you have to ensure that each and every pixel of
the Frame is painted by the Frame in some way.
public void paint(Graphics g) {
System.err.println("paint(" + g +
"), w = " + getSize().width +
", h = " + getSize().height);
g.clearRect(0, 0, getSize().width, getSize().height);
First you remove the operation from update(), then you add it in
paint()? Doesn't make much sense.
//super.paint(g);
Calling super.paint() is necessary, otherwise the children will not be
painted.
/Thomas
--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/