Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make JFrame background and JPanel transparent with only image showing

Hey I am trying to make a some kind of launcher and the "window" must be transparent because I want the image I am using to be the design of it if you understand what I mean. I tried to do setUndecorated(true); and setBackground(new Color(0, 0, 0, 0)); but it just looked weird. Here is a picture on how it looks: http://prntscr.com/2pqohq Here is my code:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * 
 * Our Launcher for the client
 * 
 * @author Daniel <Skype: daniel.gusdal>
 *
 * Current Date: 5. feb. 2014 Current Time: 14:29:54
 * Project: 742 client. File Name: Launcher.java
 *
 */
public class Launcher2 {

    public Launcher2() {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new ImagePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //frame.pack(); //had to remove, got an error from it... 
        frame.setUndecorated(true); //transparent
        frame.setBackground(new Color(0, 0, 0, 0)); //transparent
        frame.setVisible(true);
        frame.setSize(1080, 550);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new Launcher2();
            }
        });
    }

    @SuppressWarnings("serial")
    public class ImagePanel extends JPanel {

        BufferedImage img;

        public ImagePanel() {
            setLayout(new GridBagLayout());
            try {
                img = ImageIO.read(new File("C:/Users/Daniel/Pictures/Launcher3.png/"));
            } catch (MalformedURLException ex) {
                Logger.getLogger(Launcher2.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(Launcher2.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        /**
         * Draws the image and sets the image dimension
         */
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            //g.drawImage(img, 100, 100, 1080, 550, this);
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }

        /**
         * Sets the JPanel dimension
         */
        public Dimension getPreferredSize() {
            return new Dimension(1080, 550); 
        }
    }
}
like image 356
Daniel Avatar asked Feb 05 '14 15:02

Daniel


People also ask

Can you make a JPanel transparent?

You can simply create your jPanel using drag and drop, as you always do and then for changing the panel's color and making it transparent or semi-transparent you can use this code: panel. setBackground(new Color(0.0f, 0.0f, 0.0f, 0.5f));

How do you make a transparent background in Java?

Methods : setBackground(Color c) : method to set the background color to color c. color(int r, int g, int b, int alpha) : creates a new color with specified red, green, blue and alpha value. where alpha is the value of translucency where 255 is opaque and 0 is transparent .

How do I change the background color of a JPanel swing?

We can set a background color to JPanel by using the setBackground() method.


1 Answers

You need to set the ImagePanel to setOpaque(false)

 public ImagePanel() {
        setOpaque(false);

Also you were getting the exception because you need to setUndecorate(true) before you pack();

 JFrame frame = new JFrame();
 frame.add(new ImagePanel());
 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.setUndecorated(true);
 frame.pack(); 
 frame.setBackground(new Color(0, 0, 0, 0)); 
 frame.setVisible(true);

Those are the only two things I changed and it works. Also I got rid of the setSize()

Also use frame.setLocationRelativeTo(null); after pack() to center the frame.


Here's is an example (just for future readers, since I think this may be a popular question)

enter image description here

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.*;

public class CircleSplashScreen {

    public CircleSplashScreen() {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new ImagePanel());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setUndecorated(true);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setBackground(new Color(0, 0, 0, 0));
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                new CircleSplashScreen();
            }
        });
    }

    @SuppressWarnings("serial")
    public class ImagePanel extends JPanel {

        BufferedImage img;

        public ImagePanel() {
            setOpaque(false);
            setLayout(new GridBagLayout());
            try {
                img = ImageIO.read(new URL("http://www.iconsdb.com/icons/preview/royal-blue/stackoverflow-4-xxl.png"));
            } catch (IOException ex) {
                Logger.getLogger(CircleSplashScreen.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, getWidth(), getHeight(), this);
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(500, 500);
        }
    }
}
like image 83
Paul Samsotha Avatar answered Oct 16 '22 07:10

Paul Samsotha