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?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • 2
    Color takes four parameter r,g,b,a. a is the Alpha or transparency component that you would want to set. – Extreme Coders May 07 '13 at 15:59
  • setting the r=0, g=0, b=0 will create the background to be white, but making the a=1 will put it as transparent – user2277872 May 07 '13 at 16:03
  • 2
    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? – Lajos Arpad May 07 '13 at 16:05
  • 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? – Lajos Arpad May 07 '13 at 16:07

7 Answers7

23

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 );
Extreme Coders
  • 3,441
  • 2
  • 39
  • 55
  • 1
    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. – Lajos Arpad May 07 '13 at 21:01
  • 4
    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. – Lajos Arpad May 07 '13 at 22:46
  • @Lajos: ...which means: `#setColor()` + `#fillRect()` right? – Campa Jan 12 '16 at 08:49
  • 1
    @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. – wchargin Mar 14 '16 at 20:29
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.

Campa
  • 4,267
  • 3
  • 37
  • 42
RaptorDotCpp
  • 1,425
  • 14
  • 26
2

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

Emad Saber
  • 21
  • 1
  • 1
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));*/
Bugs
  • 4,491
  • 9
  • 32
  • 41
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.

Abaab
  • 571
  • 1
  • 4
  • 15
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.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
0

Use the constructor of the color like this:

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

The value 50 is for the transparency.

Arefe
  • 11,321
  • 18
  • 114
  • 168