1

Searching stackoverflow for the answer to this question only led me to other questions asking how to accomplish transparency on one or the other, none that came up had the issue of transparency functioning on one, but not the other. I'm sure by now you've been able to figure out the issue, but i'll explain in a little more depth... A friend and I are working on a java application and on running, a launcher is brought up. He uses a mac, and the JFrame was gotten to be transparent, however when I sync'd the project to my computer; A windows PC, The background of the JFrame was visible and the graphics on top flashed, kind of like a strobe light.

If anyone could provide any kind of insight it would be greatly appreciated, and if more information needs to be provided, I will try to go through program more deeply, and get more info.

private void setUpWindow(int width, int height) {

    setIconImage(images.get(1));
    setSize(width, height);
    setUndecorated(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setBackground(new Color(0,255,0,0));
    setLayout(new BorderLayout());

    addMouseListener(this);
    setResizable(false);
    setLocationRelativeTo(null);

    addComponents();
    requestFocus();
    setVisible(true);

}

private void addComponents() {
    setContentPane(new BackPanel());
    getContentPane().setBackground(Color.BLACK);
    setLayout(new BorderLayout());

    panel.setSize(screenSize);
    add(panel);
}

Window size is 1000x600

public class BackPanel extends JPanel {

    public BackPanel() {
        setOpaque(false);
        setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

    Graphics2D overGraphics2D = (Graphics2D)g.create();

    overGraphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.0f));
    overGraphics2D.setColor(Color.BLACK);
    overGraphics2D.fill(getBounds());
    overGraphics2D.dispose();
    }
}
public class DrawingPanel extends JPanel
{
    public DrawingPanel()
    {
        setVisible(true);
    }

    @Override
    protected void paintComponent(Graphics g)
    {
        super.paintComponent(g);
        refreshTimer.schedule(new refreshTimer(), 1);
    }
}

The referred refresh timer has a delay of '1' so that it is started when the launcher is drawn.

Also, ideas from this; how to set JFrame background transparent but JPanel or JLabel Background opaque? were used in setting up the transparency.

Community
  • 1
  • 1

1 Answers1

0

The referred refresh timer has a delay of '1' so that it is started when the launcher is drawn.

This is a problem with cross platform drawing because different operating systems have different "resolutions". You need to draw/call the method to draw inside that paintComponent() method. You also need to make the window full screen (or kinda fake full screen) by getting the screen size then setting the window full screen. Like this:

//in the constructor after declaring the frame undecorated
setSize(Toolkit.getDefaultToolkit().getScreenSize());

Hope that helps!

BitNinja
  • 1,477
  • 1
  • 19
  • 25