0

I am working on a project where I want to have the screen go semi-transparent (think Google Picasa or Facebook photo viewer style) and then place a different JPanel over it that is completely opaque. I've looked at a lot of different solutions found online, but I haven't been able to get them to work the way I want. I tried to follow the directions given here

(how to set JFrame background transparent but JPanel or JLabel Background opaque?)

the closest, since it appears to show the same effect that I want to have. however, it still doesn't work.

Here's my code so far:

package test;

import java.awt.AlphaComposite;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TransTest {
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {             
            BGBase base = new BGBase();
            base.setVisible(true);
        }
    });
}

static class BGBase extends JFrame {
BGPanel transparent = new BGPanel();

BGBase() {
    Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

    setTitle("Transparent Frame");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(d);
//      setUndecorated(true);
    setBackground(new Color(0, 255, 0, 0));
    getContentPane().setBackground(Color.BLACK);
//      setLayout(new BorderLayout());

    //Something needs to go here?
}
}

static class BGPanel extends JPanel {
BGPanel() {
    setOpaque(false);
}

@Override
protected void paintComponent(Graphics g) {

    // Allow super to paint
    super.paintComponent(g);

    // Apply our own painting effect
    Graphics2D g2d = (Graphics2D) g.create();
    // 50% transparent Alpha
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));

    g2d.setColor(getBackground());
    g2d.fill(getBounds());

    g2d.dispose();
}
}
}

Thanks very much in advance!

Community
  • 1
  • 1
teuber789
  • 1,527
  • 16
  • 28

2 Answers2

0
//setBackground(new Color(0, 255, 0, 0));
//getContentPane().setBackground(Color.BLACK);

I want to have the screen go semi-transparent

You are specifying 0 for the alpha value which means it is completely transparent. Also you don't need to change the background of the content pane.

Just specify the color or the frame with transparency. Something like:

setBackground(new Color(255, 255, 255, 128));

and then place a different JPanel over it that is completely opaque

Then there is no need for a custom panel, because by default JPanel is opaque. The only thing you need to worry about is using the proper layout manager so the panel is displayed properly in the frame. And of course you actually have to add the panel to the frame.

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

Change

setOpaque(false);

to

setOpaque(true);

else your paintComponent method (in BGPanel class) won't be called at all.

Hope this helps.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45