Java Graphics2D 透明背景

23

我有一个Graphics2D对象,想要设置该对象的背景。该对象有一个setBackground方法,需要传入一个Color参数来设置背景颜色。

我的问题是:如何设置对象背景的透明度?我能否将其完全透明?我能否将其完全不透明?我能否将其透明度设置为0.8?我如何设置这些值?

我看到有一些预定义的int值称为TRANSLUCENTOPAQUE,但我不确定如何使用它们。

也许正确的用法是使用带有int参数的Color构造函数吗?


2
颜色有四个参数r、g、b、a。a是Alpha或透明度组件,您需要设置它。 - Extreme Coders
将r=0,g=0,b=0设置将创建白色背景,但将a=1设置将使其透明。 - user2277872
2
是的,我现在可以看到。我们如何定义“a”?它是一个介于0和255之间的值吗?0表示它是不透明的,255表示它是透明的? - Lajos Arpad
我该如何定义一个透明度为0.8的白色?我该如何定义一个透明的白色?我该如何定义一个不透明的白色? - Lajos Arpad
7个回答

23
您可以通过指定透明度来构造一个颜色对象。例如,以下代码使用50%的透明度构造了一个红色的颜色。
Color c=new Color(1f,0f,0f,.5f );

1
我该如何设置我的Graphics2D的背景?有一个setBackground函数,但它并没有设置背景。generator.setBackground(Settings.canvasColor),其中Settings.canvasColor是Color.BLUE,但最终画布仍然是白色的。 - Lajos Arpad
4
我还没有看到任何有效的方法来将Graphics2D对象的背景设置为指定的颜色(虽然它有一个方法可以这样做,但该方法没有效果),因此我用给定的颜色绘制了一个矩形以填充画布。 - Lajos Arpad
@Lajos:这意味着:#setColor() + #fillRect(),对吗? - Campa
1
@LajosArpad:setBackground方法与setColor方法类似,只设置画笔的颜色。如果要用背景色填充整个画布,请使用clear - wchargin

6
您可以按照以下方式调用Color的构造函数:
Color c = new Color(r,g,b,a);

其中a代表透明度。

与所有Java类一样,您可以在官方API中找到此信息:http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html

这是一个非常好的资源,可以避免您在这里等待答案。


2
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));*/

2
如果您正在使用JPanel,则可以尝试以下方法: jPanel1.setOpaque(false);

1

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);
            }
        });
    }
}

查看这里获取更多信息。


1
我正在使用一个Graphics2D对象,而不是JFrame。 - Lajos Arpad

0

如果您想要实现透明效果,请使用颜色属性设置4个变量:

this.setBackground(new Color(0, 0, 0, .5f));

这将为背景设置RGB颜色的前三个参数(*new Color(** 0,0,0,** .5f)*),第四个参数用于确定不透明度的百分比(opaque)。

如果您希望背景不显示,请使用值null

this.setBackground(null);

许多人使用setOpaque(false);但这会去除填充而不是背景


0
使用颜色的构造函数,可以这样写:
Color color = new Color(152,251,152, 50);

数值50表示透明度。


被接受的答案提出了类似的想法。 - Lajos Arpad
如果你看到了,那么@LajosArpad使用另一个构造函数。 - Arefe
不是的,它使用相同的构造函数,该构造函数接收四个浮点数,请参见:https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#Color(float,%20float,%20float,%20float) - Lajos Arpad

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接