1

I am trying to create a clock application for desktop in the language Processing 3 (java based) and I need to make the background transparent so you can see what's behind the clock (for example the desktop).

I tried this:

background(0, 0, 0, 0);

doesn't work.

Can anyone help me?

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
yoav sarfaty
  • 43
  • 1
  • 1
  • 8
  • See also ["How to get transparent JFrame"](https://stackoverflow.com/questions/28656647/how-to-get-transparent-jframe) – Andy Thomas May 24 '17 at 17:21
  • @AndyThomas It does not work in processing – yoav sarfaty May 24 '17 at 17:23
  • Please be more specific than saying it doesn't work. What exactly happened when you tried your code? What exactly happened when you tried Andy's suggestion? Did you really try Andy's suggestion, or did you look at it and not try it? – Kevin Workman May 24 '17 at 17:42

2 Answers2

1

You're going to have to get to the underlying window and set the transparency of that.

How you do that (and whether it's even possible) is going to depend on which renderer you're using, and what your computer is capable of.

Here's an example of how you might do that with the default renderer:

import processing.awt.PSurfaceAWT;
import processing.awt.PSurfaceAWT.SmoothCanvas;
import javax.swing.JFrame;

void setup() {
  size(200, 200);
  PSurfaceAWT awtSurface = (PSurfaceAWT) surface;
  SmoothCanvas smoothCanvas = (SmoothCanvas) awtSurface.getNative();
  JFrame jframe = (JFrame)smoothCanvas.getFrame();
  jframe.dispose();
  jframe.setUndecorated(true);
  jframe.setOpacity(.5f);
  jframe.setVisible(true);
}

void draw() {
  background(0, 128);
}

Please note that this is just example code, so you might have to play with it to get it to work with your computer and your renderer. But the general idea is there: you have to get to the underlying window, and then set the transparency of that.

If this doesn't work, you'll probably have better luck if you use Processing as a Java library instead of going through the Processing editor. Specifically you should be able to get to the underlying window before it's displayed.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • "No library found for processing.awt.PSurfaceAWT" – yoav sarfaty May 24 '17 at 17:45
  • @yoavsarfaty That's just a warning, not an error. And like I said, this is just example code. You're going to have to do some debugging and tinkering to get it to work on your computer. – Kevin Workman May 24 '17 at 17:46
  • @yoavsarfaty Again, it's just example code. You're going to have to play with it to get it to work on your computer. Can you please be more specific than just saying "it works. sort of" or saying it doesn't work at all? We can't see your computer, so we have no idea what you're looking at. – Kevin Workman May 24 '17 at 17:50
  • i cant move the window and everything is transparent not just the background – yoav sarfaty May 24 '17 at 17:54
  • @yoavsarfaty Uhh, yeah. That's by definition how it's going to behave. Have you tried reading up on transparent windows in Java? Which tutorials did you read? What did they tell you? – Kevin Workman May 24 '17 at 17:56
  • I just want the background to be transperent not the window – yoav sarfaty May 24 '17 at 17:59
  • @yoavsarfaty I understand what you want. My answer gives you a JFrame. Now that you have the JFrame, you can refine your search and narrow your question. Here is a promising result: https://stackoverflow.com/questions/14927980/how-to-make-a-transparent-jframe-but-keep-everything-else-the-same – Kevin Workman May 24 '17 at 18:01
0

I will try to give you some Java code maybe these help you (Transparent Notification frame):

import static java.awt.GraphicsDevice.WindowTranslucency.PERPIXEL_TRANSPARENT;
import static java.awt.GraphicsDevice.WindowTranslucency.TRANSLUCENT;

import java.awt.Color;
import java.awt.Font;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagLayout;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

/**
 *
 * @author Coder ACJHP
 */
public class NotifyMe extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JLabel label;
    public NotifyMe() {
        super("NotifyMe");
        setLayout(new GridBagLayout());
        addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                setShape(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(),15,15));
            }
        });

        setUndecorated(true);
        setSize(250, 80);
        setAlwaysOnTop(true);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        label = new JLabel();
        label.setFont(new Font("Adobe Arabic", Font.BOLD, 14));
        label.setBounds(0, 0, getWidth(), getHeight());

        label.setForeground(Color.RED);
        add(label);
    }

    public void setNotifiyNote(String note) {
        this.label.setText(note);
    }

    public static void main(String[] args) {

        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice gd = ge.getDefaultScreenDevice();
        final boolean isTranslucencySupported = gd.isWindowTranslucencySupported(TRANSLUCENT);

        if (!gd.isWindowTranslucencySupported(PERPIXEL_TRANSPARENT)) {
            System.err.println("Shaped windows are not supported");
            System.exit(0);
        }

        if (!isTranslucencySupported) {
            System.out.println("Translucency is not supported, creating an opaque window");
        }

        SwingUtilities.invokeLater(() -> {
            NotifyMe sw = new NotifyMe();

            if (isTranslucencySupported) {
                sw.setOpacity(0.7f);
            }
        });
    }
}
Coder ACJHP
  • 1,940
  • 1
  • 20
  • 34
  • 1
    Please note that this is a [tag:processing] question, and [Processing != Java](https://meta.stackoverflow.com/questions/321127/processing-java). Specifically, this code does not include the Processing window at all. Also please try to explain what code does instead of just pasting a bunch of code without an explanation. – Kevin Workman May 24 '17 at 17:47
  • @KevinWorkman you are right it's not processing code just I tried to give an example.I If there any problem I can delete my answer It not big problem. – Coder ACJHP May 24 '17 at 17:52
  • Well, the code doesn't really help the OP, since they're working in Processing. Processing handles its own window itself. So creating a new window doesn't really help in this case. – Kevin Workman May 24 '17 at 17:54