Java Graphics2D 透明背景
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16423503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Graphics2D transparent background
提问by Lajos Arpad
I have a Graphics2D
object and I want to set up the background of the object. It has a setBackground
method, which has a Color parameter. This way I can set the color of the background.
我有一个Graphics2D
对象,我想设置对象的背景。它有一个setBackground
方法,它有一个 Color 参数。这样我就可以设置背景的颜色。
My question is: how can I set the transparency of the background of the object? Can I somehow tell it to be completely transparent? Can I somehow tell it to be completely opaque? Can I somehow tell it to have 0.8 transparency/opacity? How can I set these values?
我的问题是:如何设置对象背景的透明度?我能以某种方式告诉它完全透明吗?我能以某种方式告诉它完全不透明吗?我能以某种方式告诉它具有 0.8 的透明度/不透明度吗?如何设置这些值?
I have seen that there are int predefined values called TRANSLUCENT
and OPAQUE
, but I am not sure how can I use them.
我已经看到有称为TRANSLUCENT
and 的int 预定义值OPAQUE
,但我不确定如何使用它们。
Maybe the correct usage is to call the constructor of Color with an int parameter?
也许正确的用法是使用 int 参数调用 Color 的构造函数?
采纳答案by Extreme Coders
You can construct a Color object by specifying a transparency. For example the following code constructs a RED color with 50% transparency
您可以通过指定透明度来构造 Color 对象。例如下面的代码构造了一个透明度为 50% 的红色
Color c=new Color(1f,0f,0f,.5f );
回答by RaptorDotCpp
You can call the constructor of Color in the following way:
您可以通过以下方式调用 Color 的构造函数:
Color c = new Color(r,g,b,a);
where a is the alpha (transparency) value.
其中 a 是 alpha(透明度)值。
As with all Java classes, you can find this information in the official API: http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html
与所有 Java 类一样,您可以在官方 API 中找到此信息:http: //docs.oracle.com/javase/7/docs/api/java/awt/Color.html
It's a really good resource and can spare you waiting for an answer on here.
这是一个非常好的资源,可以让您在此处等待答案。
回答by Abaab
Java is actually pretty good at this stuff, you can achieve transparency and much more. Here's some code for a simple transparent window I copiedfrom oracle:
Java实际上非常擅长这些东西,您可以实现透明度等等。这是我从 oracle复制的一个简单透明窗口的一些代码:
package misc;
import java.awt.*;
import javax.swing.*;
import static java.awt.GraphicsDevice.WindowTranslucency.*;
public class TranslucentWindowDemo extends JFrame {
public TranslucentWindowDemo() {
super("TranslucentWindow");
setLayout(new GridBagLayout());
setSize(300,200);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Add a sample button.
add(new JButton("I am a Button"));
}
public static void main(String[] args) {
// Determine if the GraphicsDevice supports translucency.
GraphicsEnvironment ge =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = ge.getDefaultScreenDevice();
//If translucent windows aren't supported, exit.
if (!gd.isWindowTranslucencySupported(TRANSLUCENT)) {
System.err.println(
"Translucency is not supported");
System.exit(0);
}
JFrame.setDefaultLookAndFeelDecorated(true);
// Create the GUI on the event-dispatching thread
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
TranslucentWindowDemo tw = new TranslucentWindowDemo();
// Set the window to 55% opaque (45% translucent).
tw.setOpacity(0.55f);
// Display the window.
tw.setVisible(true);
}
});
}
}
Look herefor more information.
看这里了解更多信息。
回答by Emad Saber
You may try this if you are using a JPanel : jPanel1.setOpaque(false);
如果您使用的是 JPanel,您可以试试这个: jPanel1.setOpaque(false);
回答by javier ivan castellanos perez
jPanel1.setBackground(new Color(0,0,0,200));
/*This will put a transparent black color on a panel, the important part of the code is: .setBackground(new Color(0,0,0,200));*/
回答by Ing. Javier OC
If what you want is to give a transparent effect use the Color properties to 4 variables:
如果您想要的是提供透明效果,请使用 Color 属性到 4 个变量:
this.setBackground (new Color (0,0,0, .5f));
this.setBackground (新颜色 (0,0,0, .5f));
this gives the backgroundthe RGB color of the first three parameters (*new Color (** 0,0,0, **. 5f)*) and the fourth is used to determine the percentage of opacity (opaque)
这为背景提供了前三个参数的 RGB 颜色(*new Color (** 0,0,0, **. 5f)*),第四个参数用于确定不透明度的百分比(opaque)
If you want the backgroundnot to be displayed, use the value null
如果不希望背景显示,请使用值null
this.setBackground (null);
this.setBackground (null);
Many use setOpaque (false); but that takes away the paddingnot the background.
许多使用 setOpaque (false); 但这带走了填充而不是背景。