Ashu
Ashu

Reputation: 21

JPanel with a background image which has some part transparent

My image is like a frame which is transparent in the middle. I want to make it as background image of a JPanel. I have done this but for the transparent part of image, white color is coming. I want to remove this white color so that the components below this Jpanel become visible.

My code for custom JPanel is

public class JPanelWithBackground extends JPanel {
private static final long serialVersionUID = 1L;
Image imageOrg = null;
Image image = null;
{
     addComponentListener(new ComponentAdapter() {
             public void componentResized(ComponentEvent e) {
             int w = JPanelWithBackground.this.getWidth();
             int h = JPanelWithBackground.this.getHeight();
             image = w>0&&h>0?imageOrg.getScaledInstance(w,h, 
             java.awt.Image.SCALE_SMOOTH):imageOrg;
             JPanelWithBackground.this.repaint();
   });
 }
 public JPanelWithBackground(Image image2) {
    imageOrg=image2;
    image=image2;
    setOpaque(false);
    }
    public void paintComponent(Graphics g) {
    super.paintComponent(g);
    if (image!=null) 
      g.drawImage(image, 0, 0, null);
   }
}

Upvotes: 0

Views: 1013

Answers (1)

Guillaume Polet
Guillaume Polet

Reputation: 47608

I don't have any particular problem with your code. The problem is probably that your image is not transparent as you expect.

Here is an example that seems to work perfectly (I only took the liberty to fix minor issues in your code):

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class JPanelWithBackground extends JPanel {
    private static final long serialVersionUID = 1L;
    Image imageOrg = null;

    public JPanelWithBackground(Image image2) {
        imageOrg = image2;
        setOpaque(false);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(imageOrg.getWidth(this), imageOrg.getHeight(this));
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (imageOrg != null) {
            System.err.println("painting");
            g.drawImage(imageOrg, 0, 0, getWidth(), getHeight(), this);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    JFrame frame = new JFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    JPanelWithBackground panel = new JPanelWithBackground(new ImageIcon(new URL(
                            "http://www.lemondedemario.fr/images/dossier/bowser/bowser.png")).getImage());
                    JPanel greenPanel = new JPanel(new BorderLayout());
                    greenPanel.setBackground(Color.GREEN);
                    greenPanel.add(panel);
                    frame.add(greenPanel);
                    frame.pack();
                    frame.setVisible(true);
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

Upvotes: 3

Related Questions