2017-05-24 37 views
0

我正在尝试在处理3(基于Java)的语言中为桌面创建一个时钟应用程序,并且我需要使背景透明,以便您可以查看时钟后面的内容例如桌面)。在处理语言中制作透明背景(应用程序)

我尝试这样做:

background(0, 0, 0, 0); 

不起作用。

任何人都可以帮助我吗?

+0

参见[ “如何得到透明的JFrame”(https://开头计算器.com/questions/28656647/how-to-get-transparent-jframe) –

+0

@AndyThomas它在处理中不起作用 –

+0

请更具体地说,它不起作用。当你尝试你的代码时究竟发生了什么?当你尝试Andy的建议时究竟发生了什么?你真的尝试过安迪的建议吗,还是你看过而不尝试? –

回答

1

你将不得不到底层窗口并设置它的透明度。

如何做到这一点(以及是否有可能)将取决于您使用的渲染器以及您的计算机的能力。

这里有一个如何做到这一点与默认渲染一个例子:

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编辑器,则可能会有更好的运气。具体来说,您应该能够在显示之前进入底层窗口。

+0

“没有找到processing.awt.PSurfaceAWT库” –

+0

@yoavsarfaty这只是一个警告,而不是一个错误。就像我说的,这只是示例代码。你将不得不做一些调试和修补,以使其在你的电脑上运行。它的工作原理是 –

+0

。 那种,我不能移动窗口 –

0

我会尽量给你一些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); 
      } 
     }); 
    } 
} 
+1

请注意,这是一个[标签:processing]问题,[Processing!= Java](https://meta.stackoverflow.com/questions/321127/processing-java)。具体来说,这个代码根本不包括Processing窗口。另外请尝试解释什么代码,而不是仅仅粘贴一堆代码而没有解释。 –

+0

@KevinWorkman你是对的它不处理代码只是我试图给一个例子。如果有任何问题,我可以删除我的答案这不是很大的问题。 –

+0

那么,代码并没有真正帮助OP,因为他们在Processing中工作。处理自己处理它自己的窗口。所以在这种情况下创建一个新窗口并没有什么帮助。 –