Making a program without a window border (and no background, just the shape)

A simplified example using an image from a URL:

// Display an image without window border and transparent background
// Author: glv
// Date:   2023-10-15
// Insight gleaned from various sources; seek and you shall find.

import java.awt.*;
import javax.swing.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.net.URL;

JFrame frame;
ImageIcon img;
BufferedImage bimg;

void setup()
  {     
  surface.setVisible(false);  
  
  String url = "https://upload.wikimedia.org/wikipedia/commons/thumb/c/cb/Processing_2021_logo.svg/240px-Processing_2021_logo.svg.png";
  try
    {
    bimg = ImageIO.read(new URL(url));
    img = new ImageIcon(bimg);        // BufferedImage from url
    }
  catch(IOException ex)
    {
    String imgPath = sketchPath() + "/data/test.png"; // Backup image
    img = new ImageIcon(imgPath);
    }
    
  frame = new JFrame();
  frame.setPreferredSize(new Dimension(img.getIconWidth(), img.getIconHeight()));  
    
  frame.add(new JLabel(img));
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setUndecorated(true);
  frame.setBackground(new Color(0, 0, 0, 0));
  frame.pack();
  frame.setVisible(true); 
  }

You can create your own image and modify code as necessary.

:)

2 Likes