transparent jframe background?

how can I set the background of a JFrame to make it transparent without making everything I draw to it also transparent.

for example if I add a canvas to a JFrame and set the background of the JFrame to the color new Color(0, 0, 0, 0), if I draw anything to the canvas it will be completely transparent as well.

EDIT: well that was a pain but I got it -.-

and it looks beautiful :’) so happy

to make your post actually useful for others, you might want to post how you solved it.

Ok I’m a little self conscious though. :-\

public static final int WIDTH = 437;
public static final int HEIGHT = 127;
public static BufferedImage buffer = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);

...

JFrame detached = new JFrame();
detached.setSize(WIDTH, HEIGHT);
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
detached.setLocation((dim.width - WIDTH) / 2, (dim.height - HEIGHT) / 2);
detached.setUndecorated(true);
detached.setBackground(new Color(0, 0, 0, 0));

JPanel panel = new JPanel() {

	private static final long serialVersionUID = 1L;

			@Override
			public void paintComponent(Graphics g_) { // call panel.repaint() as necessary
				Graphics2D g2 = (Graphics2D) buffer.getGraphics();
				g2.setComposite(AlphaComposite.getInstance(AlphaComposite.CLEAR));
				g2.fillRect(0, 0, WIDTH, HEIGHT);
				g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
				draw(g2); // draw the actual menu
				g2.dispose();

				Graphics2D g = (Graphics2D) g_;
				g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC)); // only draws source, effectively clearing old image
				g.drawImage(buffer, 0, 0, null);
				g.dispose();
			}

};
panel.addMouseListener(MenuMouseListener.instance);
panel.addMouseMotionListener(MenuMouseListener.instance);
panel.setIgnoreRepaint(true);

detached.add(panel);

detached.setVisible(true);

...

// This is how I handled dragging because the JFrame is now undecorated

	private static class MenuMouseListener implements MouseListener, MouseMotionListener {

		public static final MenuMouseListener instance = new MenuMouseListener();

		private MenuMouseListener() {
			
		}

		private int locX;
		private int locY;

		@Override
		public void mouseDragged(MouseEvent e) {
			if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) == MouseEvent.BUTTON1_DOWN_MASK) {
				detached.setLocation(e.getXOnScreen() - locX, e.getYOnScreen() - locY);
			}
		}

		@Override
		public void mouseMoved(MouseEvent e) {
			
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			if (e.getModifiers() == MouseEvent.BUTTON1_MASK) {
				locX = e.getX();
				locY = e.getY();
			}
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			
		}
		
	}

EDIT: Actually you can just clear the old graphics by only drawing the source using AlphaComposite.SRC :slight_smile: