Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Swing only has a concept of opaque or transparent components, it does not, by itself, know how to deal with a component that is opaque, but has a translucent background color. As far as Swing is concerned, the component is opaque, so it doesn't paint what's underneath the component.</p> <p>Normally, I would supply a <code>alpha</code> value which I would then apply to a solid background, but in this example, I'm simply filling the background with what ever background color you supply, so unless you supply a translucent color, it will be filled with a solid color.</p> <p><img src="https://i.stack.imgur.com/hAXHW.png" alt="enter image description here"></p> <pre><code>import java.awt.Color; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.GridBagLayout; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.Icon; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class TestTranslucentLabel { public static void main(String[] args) { new TestTranslucentLabel(); } public TestTranslucentLabel() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } try { TranslucentLabel label = new TranslucentLabel("This is a translucent label"); label.setBackground(new Color(255, 0, 0, 128)); label.setForeground(Color.WHITE); JLabel background = new JLabel(); background.setIcon(new ImageIcon(ImageIO.read(new File("/Users/swhitehead/Dropbox/MegaTokyo/Rampage_Small.png")))); background.setLayout(new GridBagLayout()); background.add(label); JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(background); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } catch (IOException exp) { exp.printStackTrace(); } } }); } public class TranslucentLabel extends JLabel { public TranslucentLabel(String text, Icon icon, int horizontalAlignment) { super(text, icon, horizontalAlignment); } public TranslucentLabel(String text, int horizontalAlignment) { super(text, horizontalAlignment); } public TranslucentLabel(String text) { super(text); } public TranslucentLabel(Icon image, int horizontalAlignment) { super(image, horizontalAlignment); } public TranslucentLabel(Icon image) { super(image); } public TranslucentLabel() { super(); } @Override public boolean isOpaque() { return false; } @Override protected void paintComponent(Graphics g) { Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(getBackground()); g2d.fillRect(0, 0, getWidth(), getHeight()); super.paintComponent(g2d); g2d.dispose(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload