0

I am trying to show multiple pngs with transparent background in a JFrame but they always have a white background.

Here's my code:

JFrame frame = this;
frame.setUndecorated(true);

back1 = new ImageIcon("res/launcher/back_1.png");
JLabel label = new JLabel(back1);
label.setOpaque(false);
label.repaint();
frame.add(label);

play_btn = new ImageIcon("res/launcher/play_btn.png");
label = new JLabel(play_btn);
label.setOpaque(false);
label.repaint();
frame.add(label);

frame.setTitle("Schach");
frame.setSize(WIDTH, HEIGHT);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2); // JFrame in middle of screen
frame.setVisible(true);

frame.repaint();
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
luis
  • 37
  • 7
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). E.G. The code in [this answer](https://stackoverflow.com/a/10862262/418556) hot links to an image embedded in [this question](https://stackoverflow.com/q/10861852/418556). 3) `frame.setLocation(dim.width / 2 - this.getSize().width / 2, dim.height / 2 - this.getSize().height / 2); // JFrame in middle of screen` could be replaced by .. – Andrew Thompson Jun 19 '21 at 04:56
  • `frame.setLocationRelativeTo(null);` (much shorter). 4) Application resources will become embedded resources by the time of deployment, so it is wise to start accessing them as if they were, right now. An [tag:embedded-resource] must be accessed by URL rather than file. See the [info. page for embedded resource](http://stackoverflow.com/tags/embedded-resource/info) for how to form the URL. – Andrew Thompson Jun 19 '21 at 04:57

1 Answers1

0

Fixed it like this:

JLabel back1 = new JLabel(new ImageIcon("res/launcher/back_1.png"));
        back1.setLayout( new BorderLayout() );
        JLabel knight = new JLabel(new ImageIcon("res/launcher/play_btn.png"));
        back1.add(knight);
        frame.add(back1);
luis
  • 37
  • 7