我正在尝试在language Processing 3(基于java)中为桌面创建一个时钟应用程序,我需要使背景透明,这样你就可以看到时钟背后的东西(例如桌面)。
我试过这个:
background(0, 0, 0, 0);不起作用。
有谁可以帮我?
发布于 2017-05-25 01:42:09
你必须找到底层的窗口并设置它的透明度。
如何做到这一点(以及它是否可能)将取决于您使用的渲染器,以及您的计算机的能力。
以下是如何使用默认渲染器执行此操作的示例:
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);
}请注意,这只是示例代码,因此您可能需要使用它才能使其与您的计算机和渲染器一起工作。但是一般的想法是:你必须到达底层窗口,然后设置它的透明度。
如果这不起作用,那么如果使用Processing作为Java库,而不是使用Processing编辑器,可能会更走运。具体地说,您应该能够在底层窗口显示之前到达它。
发布于 2017-05-25 01:46:34
我会试着给你一些Java代码,也许这些对你有帮助(透明通知框架):
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);
}
});
}
}https://stackoverflow.com/questions/44164902
复制相似问题