0

I currently have a function which sets the background and have 1 button. The button is a PNG file and is transparent, I have done all the setOpaque stuff but the button still have a white background behind it. If anyone could help will be greatly appreciated! :)

I have attached my function below:

public JPanel createContentPane() throws IOException{

    //Full back pane
    JPanel fullGUI = new JPanel();
    fullGUI.setLayout(null);

    //background pane
    JPanel backgroundPane = new JPanel() {
        BufferedImage image = ImageIO.read(new File("UI/back2.jpg"));

        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(image, 0, 0, 1200, 750, this);
        }
    };

    backgroundPane.setLayout(null);
    backgroundPane.setLocation(0,0);
    backgroundPane.setSize(1200,750);
    fullGUI.add(backgroundPane);

    //button pane
    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(null);
    buttonPanel.setLocation(0, 250);
    buttonPanel.setSize(1200, 500);
    fullGUI.add(buttonPanel);


    JButton playButton = new JButton(new ImageIcon(("UI/play.png")));
    playButton.setLocation(399,47);
    playButton.setSize(405,308);
    playButton.setOpaque(false);
    playButton.setContentAreaFilled(false);
    playButton.setBorderPainted(false);
    playButton.setFocusPainted(false);
    buttonPanel.add(playButton);


    fullGUI.setOpaque(true);
    return fullGUI;


}
Vincent
  • 21
  • 1
  • 2
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson May 25 '14 at 11:49
  • Check if [this MCVE](http://stackoverflow.com/a/10862262/418556) works. For transparent buttons (L/R/U/D) the BG should be black, obtained from the panel behind the `GridLayout`. – Andrew Thompson May 25 '14 at 11:54
  • 1) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). 2) One way to get image(s) for an example is to hot-link to the images seen in [this answer](http://stackoverflow.com/a/19209651/418556). 3) A single blank line of white space in source code is *always* enough. Blank lines after `{` or before `}` are also typically redundant. .. (continued) – Andrew Thompson May 25 '14 at 11:58
  • (continued) .. 4) By the time of deployment, those resources will likely become an [tag:embedded-resource]. That being the case, the resource must be accessed by `URL` instead of `File`. See the [info page](http://stackoverflow.com/tags/embedded-resource/info) for the tag, for a way to form an `URL`. – Andrew Thompson May 25 '14 at 11:59

1 Answers1

-1

Have you tried setting the color to transparent? try this:

playButton.setBackground(new Color(0.0f, 0.0f, 0.0f, 0.3f);

This sets the R to 0, G to 0, B to 0, and alpha(transparency) to 0.3. Make sure to use float values to set the colours. the numbers can be tweaked to whatever you like. If you want to change the transparency, change the last number to higher for more opaque, and lower for less.

Good Luck!