0

I'd like to display a GlassPane with a sort of blur effect. Broadly speaking, something like this:

enter image description here

I tried using as GlassPane a JPanel with transparent background. Here's the code of my custom panel.

JPanel glassPane = new JPanel(){
    @Override
    public void paint(java.awt.Graphics g) {
        Graphics2D g2 = (Graphics2D)g.create();         

        g2.setRenderingHint(java.awt.RenderingHints.KEY_ANTIALIASING,java.awt.RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setComposite(java.awt.AlphaComposite.SrcOver.derive(0.5f));
        g2.setPaint(myColor);
        g2.fillRect(0,0,this.getWidth(),this.getHeight());
        g2.dispose();
    };
}

The problem is that when I load the GlassPane I have a sort of overlapping issue. I.e. it seems that all the elements of the below UI (the ones displayed in the panel below the just loaded glass pane) are randomly mirrored on the GlassPane. This happens not only in GlassPane, but every time I try to use trnsparent colors with Graphics2d. This is what i get:

enter image description here

Any idea on how to create a transparent GlassPane avoiding this annoting issue?

Roberto
  • 243
  • 1
  • 5
  • 15
  • Have you been checking [this question](http://stackoverflow.com/questions/12127422/inner-transparent-selection-window-in-java-using-glasspane?rq=1) (and it's answers)? It seems to be somewhat similar. – el_chupacabra Mar 07 '16 at 12:45
  • @el_chupacabra, I looked at that question before posting mine, but is not the same issue. My issue is about how to make transparent backgorund avoiding strange unexpected painting. – Roberto Mar 07 '16 at 12:58
  • 2
    Components must obey the opacity contract - that is, if your component is translucent, [isOpaque()](https://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#isOpaque%28%29) *must* return `false`. Otherwise the area below it does not necessarily get painted (and may contain garbage from temporary buffers). – kiheru Mar 07 '16 at 13:52
  • @kiheru oh wow! That makes sense and perfectly works. I would've never figured it out. Thank you very much. – Roberto Mar 07 '16 at 14:23
  • `something like this:` - so why don't you use the [Disabled Glass Pane](https://tips4java.wordpress.com/2008/11/07/disabled-glass-pane/) where you found the image? `I would've never figured it out` - well that is exactly what the code in the `DisabledGlassPane` class does. So why didn't you start with something that was working and modify the existing code? That is make a change and still see if it works. If not, then you know the change you made caused the problem. Why reinvent the wheel from scratch if you don't understand the concept? – camickr Mar 07 '16 at 15:52
  • You mean perhaps something like [this](http://stackoverflow.com/questions/23215415/create-an-transparent-rectangle-over-blurred-background-in-jframe/23232618#23232618)? The core problem you're having is the fact that you've broken the paint chain by not calling `super.paint`, but you should be using `paintComponent` anyway – MadProgrammer Mar 08 '16 at 06:12

0 Answers0