2

I have set my the container JPanel's background color partially transparent. I have a JLabel on it with a PNG image that has a transparent background. I have another image as well which is shown when mouse is over the label. I use the following to change the icon

private void settingsMouseEntered(java.awt.event.MouseEvent evt) {
    this.settings.setIcon(new ImageIcon(getClass().getResource("/Resources/settingsIconHover.png")));
}

private void settingsMouseExited(java.awt.event.MouseEvent evt) {
    this.settings.setIcon(new ImageIcon(getClass().getResource("/Resources/settingsIcon.png")));
}

It changes the opacity a little bit every time the icon is updated. Eventually the background isn't transparent anymore.

I have set the JPanel's background with the following code

content.setBackground(new Color(74, 137, 227, 60));

The JPanel is in a JFrame which has also a specific background

setBackground(new Color(74, 137, 227, 80));

Here's an image

enter image description here

What is wrong?

MikkoP
  • 4,864
  • 16
  • 58
  • 106
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Feb 03 '13 at 10:57
  • It's difficult to say with so little code. It might be that re-validating the container and repainting would fix the problem, but this is just a guess. On a side-note, don't create a new `ImageIcon` object every time the mouse enters or exists! Store the two images as fields instead, and load them only once (e.g. in the constructor). – gcvt Feb 03 '13 at 11:09
  • It's a problem with partial transparency and the JVM not completely repainting dirty regions on the screen. – Hovercraft Full Of Eels Feb 03 '13 at 11:21
  • Thanks @HovercraftFullOfEels! I added `repaint()` in the methods above and it fixed the problem. Please, post it as an answer. Thanks to @Dejan too, I did what you said about loading images only once. – MikkoP Feb 03 '13 at 11:28
  • additionally, ensure all containers' opacity is set to false (JPanel's default is true which might lead to painting artefacts with not fully opaque colors/icons) – kleopatra Feb 03 '13 at 14:00

3 Answers3

2

It changes the [translucency] a little bit every time the icon is updated… What is wrong?

The default composite mode for a platform's concrete implementation of Graphics2D is AlphaComposite.SRC_OVER, which may produce the effect you describe. A similar problem, due to repeated calls to repaint(), is examined here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2

See Backgrounds With Transparency for an explanation about what might be happening and one potential solution.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

When you apply a alpha color to a component like this

content.setBackground(new Color(74, 137, 227, 60));

And

setBackground(new Color(74, 137, 227, 80));

You are not effecting the opacity property of the component. This means the repaint manager doesn't know it needs to repaint the content underneath it (because as far its concerned, the are opaque).

Try setting the panels to transparent as well, using JComponent#setOpaque passing it a false value.

Then read the link supplied by camickr

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366