[英]Java Graphics2D transparent background
I have a Graphics2D
object and I want to set up the background of the object.我有一个Graphics2D
对象,我想设置对象的背景。 It has a setBackground
method, which has a Color parameter.它有一个setBackground
方法,该方法有一个 Color 参数。 This way I can set the color of the background.这样我就可以设置背景的颜色。
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?我能以某种方式告诉它具有 0.8 的透明度/不透明度吗? How can I set these values?如何设置这些值?
I have seen that there are int predefined values called TRANSLUCENT
and OPAQUE
, but I am not sure how can I use them.我已经看到有称为TRANSLUCENT
和OPAQUE
int 预定义值,但我不确定如何使用它们。
Maybe the correct usage is to call the constructor of Color with an int parameter?也许正确的用法是使用 int 参数调用 Color 的构造函数?
You can construct a Color object by specifying a transparency.您可以通过指定透明度来构造 Color 对象。 For example the following code constructs a RED color with 50% transparency例如下面的代码构造了一个透明度为 50% 的红色
Color c=new Color(1f,0f,0f,.5f );
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.这是一个非常好的资源,可以让您在此处等待答案。
如果您使用的是 JPanel,您可以试试这个: jPanel1.setOpaque(false);
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));*/
Java is actually pretty good at this stuff, you can achieve transparency and much more. Java实际上非常擅长这些东西,您可以实现透明度等等。 Here's some code for a simple transparent window I copied from oracle:这是我从 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);
}
});
}
}
If what you want is to give a transparent effect use the Color properties to 4 variables:如果您想要的是提供透明效果,请将颜色属性用于 4 个变量:
this.setBackground (new Color (0,0,0, .5f)); this.setBackground (新颜色 (0,0,0, .5f));
this gives the background the 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 background not to be displayed, use the value null如果不希望背景显示,请使用值null
this.setBackground (null); this.setBackground (null);
Many use setOpaque (false);许多使用 setOpaque (false); but that takes away the padding not the background .但这带走了填充而不是背景。
Use the constructor of the color like this:像这样使用颜色的构造函数:
Color color = new Color(152,251,152, 50);
The value 50 is for the transparency.值 50 用于透明度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.