Java Graphics2D transparent background
Asked Answered
Q

7

23

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.

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?

I have seen that there are int predefined values called TRANSLUCENT and OPAQUE, but I am not sure how can I use them.

Maybe the correct usage is to call the constructor of Color with an int parameter?

Quinquepartite answered 7/5, 2013 at 15:57 Comment(4)
Color takes four parameter r,g,b,a. a is the Alpha or transparency component that you would want to set.Melisenda
setting the r=0, g=0, b=0 will create the background to be white, but making the a=1 will put it as transparentLossa
Yes, I can see that now. How can we define "a"? Is that a value between 0 and 255, 0 meaning it is opaque, 255 meaning it is transparent?Quinquepartite
How can I define a white color, which has a transparency of 0.8? How can I define a white color, which is transparent? How can I define a white color, which is opaque?Quinquepartite
M
24

You can construct a Color object by specifying a transparency. For example the following code constructs a RED color with 50% transparency

Color c=new Color(1f,0f,0f,.5f );
Melisenda answered 7/5, 2013 at 16:4 Comment(4)
And how can I set the background of my Graphics2D? There is a setBackground function, but it doesn't set the background. generator.setBackground(Settings.canvasColor), where Settings.canvasColor is Color.BLUE, but the canvas is still white at the end of the day.Quinquepartite
I haven't seen any good solution to set the background of the Graphics2D object to a color (it has a method to do that, but the method doesn't have any effect), so I have drawn a rectangle with the given color to fill the canvas.Quinquepartite
@Lajos: ...which means: #setColor() + #fillRect() right?Irritant
@LajosArpad: The setBackground method, much like the setColor method, only sets the color of the paintbrush. Use clear if you want to fill the whole canvas with the background color.Uninterrupted
M
6

You can call the constructor of Color in the following way:

Color c = new Color(r,g,b,a);

where a is the alpha (transparency) value.

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

It's a really good resource and can spare you waiting for an answer on here.

Matronna answered 7/5, 2013 at 16:3 Comment(0)
S
2

You may try this if you are using a JPanel : jPanel1.setOpaque(false);

Subversion answered 2/7, 2014 at 12:8 Comment(0)
D
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));*/
Donata answered 4/4, 2018 at 7:40 Comment(0)
J
1

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 copied from 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 here for more information.

Jaquelinejaquelyn answered 7/5, 2013 at 16:5 Comment(1)
I am using a Graphics2D object, not a JFrame.Quinquepartite
G
0

If what you want is to give a transparent effect use the Color properties to 4 variables:

this.setBackground (new Color (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 )

If you want the background not to be displayed, use the value null

this.setBackground (null);

Many use setOpaque (false); but that takes away the padding not the background.

Goethite answered 7/5, 2020 at 0:51 Comment(0)
A
0

Use the constructor of the color like this:

Color color = new Color(152,251,152, 50);

The value 50 is for the transparency.

Auricula answered 29/3, 2021 at 3:22 Comment(3)
The accepted answer presents a similar idea.Quinquepartite
@LajosArpad that uses another constructor if you see.Auricula
No, it uses the same constructor, that receives four floats, see: docs.oracle.com/javase/7/docs/api/java/awt/…Quinquepartite

© 2022 - 2024 — McMap. All rights reserved.