Swing: hovering over a shortcut on a button on a translucent JPanel

In my problem, I have an opaque JPanel and another JPanel that is translucent (translucent) that sits on the first JPanel. When I added radio buttons to the top panel of JPanel. The problem is that every time I find the mouse over the label area of ​​each switch (and every time I move the mouse from the label), it gets darker and darker.

package trial; import java.awt.Color; import javax.swing.ButtonGroup; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JRadioButton; public class Test { public static void main(String arg[]){ JFrame rootframe = new JFrame("Test panel"); rootframe.setSize(800, 550); rootframe.setExtendedState(JFrame.MAXIMIZED_BOTH); JPanel basePanel = new JPanel(); //fills rootFrame basePanel.setOpaque(true); basePanel.setBackground(Color.yellow ); JPanel panelContainingRadioButtons = new JPanel();//wraps radio buttons panelContainingRadioButtons.setOpaque(true); panelContainingRadioButtons.setBackground(new Color(0,0,0,100) ); ButtonGroup buttonGroup1 = new ButtonGroup(); JRadioButton jRadioButton1 = new JRadioButton(); jRadioButton1.setText("Text A..............................."); jRadioButton1.setOpaque(false); jRadioButton1.setForeground( Color.white); buttonGroup1.add(jRadioButton1); JRadioButton jRadioButton2 = new JRadioButton(); jRadioButton2.setOpaque(false); jRadioButton2.setForeground( Color.white); buttonGroup1.add(jRadioButton2); jRadioButton2.setText("Text B......................."); JRadioButton jRadioButton3 = new JRadioButton(); jRadioButton3.setOpaque(false); jRadioButton3.setForeground( Color.white); buttonGroup1.add(jRadioButton3); jRadioButton3.setText("Text C................................"); panelContainingRadioButtons.add(jRadioButton1); panelContainingRadioButtons.add(jRadioButton2); panelContainingRadioButtons.add(jRadioButton3); basePanel.add(panelContainingRadioButtons); rootframe.add(basePanel); rootframe.setVisible(true); } } 

I believe that this is not a problem with the switches, because in another case, I noticed that in the same conditions, if I added JLabel to the top JPanel and added listeners to the top panel, so that the color of the jLabel text will change when you mouse over and reset to the original color, when the mouse exits, the text will be redrawn in different places, as shown in the image below: -

http://s13.postimage.org/6yn3cw48n/Untitled.png

If necessary, I will send this code too. I think this is the same problem as in both cases.

+6
source share
3 answers

You get these artifacts, probably due to the transparent color that is used for the background. JComponents do not support transparent colors as background colors. Here is a good article from @camickr that explains the problem in detail and also provides an alternative solution.

+8
source

Your result is not unexpected, since the Graphics2D composite is AlphaComposite.SRC_OVER by default. If you need a different result, you will need to use a different mode; AlphaComposite.SRC , for example, is not additive. Related examples can be found here , and here .

+4
source

Instead of using red, green, blue and alpha. For example: setBackground (new color (236, 233, 216, 220)); use setBackground (new color (236,233,216)); which is red, green, blue. It will work just fine.

+1
source

Source: https://habr.com/ru/post/920646/


All Articles