How to make more than one panel transparent?

Asked

Viewed 426 times

4

Implementing the answer of this issue, i managed to make a panel transparent. However, in order to organize the way I want, I wanted to use more JPanels, with different layout managers.

But when I do that, they don’t get transparent. How could I do that ?

Example of the problem:

inserir a descrição da imagem aqui

The image is just this blue sky, this gray/white background, is of the component.

When I use a single panel:

inserir a descrição da imagem aqui

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class Teste {

    public Teste() {
        JWindow jWindow = new JWindow();
        jWindow.setBackground(new Color(0, 0, 0, 0));
        jWindow.setContentPane(new Pane());
        jWindow.pack();
        jWindow.setVisible(true);
        jWindow.setLocationRelativeTo(null);
    }

    class Pane extends JPanel {

        private BufferedImage leaf;

        public Pane() {
            //setLayout(new BorderLayout());

            JPanel borderPainel = new JPanel();
            borderPainel.setLayout(new BorderLayout());
            JPanel gridPainel = new JPanel();
            gridPainel.setLayout(new GridLayout(2, 1));

            gridPainel.add(new JLabel("Label 01"));
            gridPainel.add(new JLabel("Label 02"));

            borderPainel.add(gridPainel, BorderLayout.SOUTH);
            add(borderPainel);

            try {
                leaf = ImageIO.read(getClass().getResource("/imagens/icon.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            gridPainel.setOpaque(false);
            borderPainel.setOpaque(false);
            //setOpaque(false);

        }

        @Override
        public Dimension getPreferredSize() {
            return leaf == null ? new Dimension(200, 200) : new Dimension(leaf.getWidth(), leaf.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (leaf != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(leaf, 0, 0, this);
                g2d.dispose();
            }
        }
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
        new Teste();
    }
}
  • your code is not [mcve] because of the image, if possible, provide the image so that the production is as faithful as possible of the problem.

  • Overwrite the Paint method from the panel below by calling the parent Paint by a child panel Components Paint

  • Barely ask why you’re using jwindow? I imagine you should use Jframe

  • @Sveen the way Paint should not be overwritten.

  • The method is not protected against overwriting, and I’ve done it countless times

  • @Just because it’s not protected doesn’t mean it’s the right thing to do. Java does not block anything, if Voce wants to do wrong, it is on account of the programmer this kind of thing.

  • @Articuno could be any image, for example, if you put a ball, it will stick to the rectangular background (component pattern), instead of becoming transparent, plus I will add a print

  • @Javinha is important Voce provide your or an equivalent senao to error production may be different than Voce is facing

  • I executed here and did not understand the problem of the code. What needs to be transparent?

  • @Articuno I edited there, I also had a question about why he did not respect the position of the components, but I will wait for a direction on transparency first.

  • @Edited article !

Show 6 more comments

1 answer

3


I made 3 changes to the code:

  • altered JWindow for JFrame, since you are working with the swing API, use components of this API and not the awt;

  • I’ve warned you several times in other posts, but I warn you again: ALWAYS dispatch graphical application of this API to the ;

  • I just cracked the line setOpaque(false); which makes the JFrame see-through as well.

The code went like this:

import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;

import javax.imageio.ImageIO;

public class PainelTransparenteTest {

    public PainelTransparenteTest() {
        JFrame jWindow = new JFrame();
        jWindow.setBackground(new Color(0, 0, 0, 0));
        jWindow.setContentPane(new Pane());
        jWindow.pack();
        jWindow.setVisible(true);
        jWindow.setLocationRelativeTo(null);
    }

    class Pane extends JPanel {

        private BufferedImage leaf;

        public Pane() {
            //setLayout(new BorderLayout());

            JPanel borderPainel = new JPanel();
            borderPainel.setLayout(new BorderLayout());
            JPanel gridPainel = new JPanel();
            gridPainel.setLayout(new GridLayout(2, 1));

            gridPainel.add(new JLabel("Label 01"));
            gridPainel.add(new JLabel("Label 02"));

            borderPainel.add(gridPainel, BorderLayout.SOUTH);
            add(borderPainel);

            try {
                //leaf = ImageIO.read(getClass().getResource("/imagens/icon.png"));
                leaf = ImageIO.read(new URL("https://i.stack.imgur.com/i5lmv.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            gridPainel.setOpaque(false);
            borderPainel.setOpaque(false);
            setOpaque(false);

        }

        @Override
        public Dimension getPreferredSize() {
            return leaf == null ? new Dimension(200, 200) : new Dimension(leaf.getWidth(), leaf.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (leaf != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                g2d.drawImage(leaf, 0, 0, this);
                g2d.dispose();
            }
        }
    }

    public static void main(String[] args) {
        JFrame.setDefaultLookAndFeelDecorated(true);
       SwingUtilities.invokeLater(() ->  new PainelTransparenteTest());
    }
}

And the result:

inserir a descrição da imagem aqui

The trick here was to set up for that main screen(the JFrame in this case) is not opaque either, hence it will follow the background of the internal components. So it was enough to uncomment the informed line.

  • one more thing, what I did wrong, so the Abels don’t get set in the SOUTH position?

  • @Javinha already there is a doubt completely different from the question. But I suppose the problem is in this panel class that Voce created and within it created two more panels. The tip to identify what I can give is to put edges on all panels of different colors, so Oce will see the limits of each and see what may be causing this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.