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:
The image is just this blue sky, this gray/white background, is of the component.
When I use a single panel:
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.
– user28595
Overwrite the Paint method from the panel below by calling the parent Paint by a child panel Components Paint
– Sveen
Barely ask why you’re using jwindow? I imagine you should use Jframe
– user28595
@Sveen the way Paint should not be overwritten.
– user28595
The method is not protected against overwriting, and I’ve done it countless times
– Sveen
@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.
– user28595
@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
@Javinha is important Voce provide your or an equivalent senao to error production may be different than Voce is facing
– user28595
I executed here and did not understand the problem of the code. What needs to be transparent?
– user28595
@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.
– Javinha
@Edited article !
– Javinha