Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:56:55+00:00 2026-06-15T15:56:55+00:00

In my program I wanted to have a translucent white to transparent gradient on

  • 0

In my program I wanted to have a translucent white to transparent gradient on my JFrame to overlay a yellow background. This works fine and it needs to be a white to transparent because of how my settings for the program work for the user. However, when I take the program into college (JRE7 to my JRE6) the gradient goes white to blackish then transparent… It isn’t so bad until you start to increase the opacity of the white colour… is there anyway I can fix this?

here is the relevant code from the top of my JFrame code.

public class DictionaryGUI extends JFrame
{   
    protected JPanel pGradientPane;

    //Interface gradient specification
    private Color pInterfaceColour = new Color(255, 245, 62);
    protected int iDegreeWhite = 180
    protected int iDegreeBlack = 0

    DictionaryGUI(int iWidth, int iHeight)
    {
        /*General definitions*/
        super(String.format("French Verb Conjugator - Version %s", MainLauncher.version));
        setSize(iWidth, iHeight);
        new Menu(this);

        this.iWidth = iWidth;    
        this.iHeight = iHeight;

        getContentPane().setBackground(pInterfaceColour);
        pGradientPane = new JPanel(new GridBagLayout())
        {
            private static final long serialVersionUID = 1L;

            protected void paintComponent(Graphics pGraphics) 
            {
                Graphics2D pGraphicsGradientRender = (Graphics2D) pGraphics;
                pGraphicsGradientRender.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                GradientPaint pGradient = new GradientPaint(0, 0, new Color(255, 255, 255, iDegreeWhite), 0, getHeight(), new Color(0, 0, 0, iDegreeBlack));
                pGraphicsGradientRender.setPaint(pGradient);
                pGraphicsGradientRender.fillRect(0, 0, getWidth(), getHeight());
                super.paintComponent(pGraphics);
            }
        };
        pGradientPane.setOpaque(false);
        pGradientPane.setPreferredSize(new Dimension(iWidth - 16, iHeight - 62));
        /*components added to pGradientPane here!*/
        add(pGradientPane);
    }

And the mainclass aswell:

public class MainLauncher
{
    static int iHeight = 400;
    static int iWidth = 730;
    static String version = "0A3B6";

    public static void main(String[] args)
    {
    try 
    {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels())
        {
            if ("Nimbus".equals(info.getName()))
            {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {e.printStackTrace();}
    DictionaryGUI window = new DictionaryGUI(iWidth, iHeight);
    window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    window.setLocationByPlatform(true);
    window.setVisible(true);
}

Is it just some difference between JRE6 and JRE7? should I make the bottom colour to white aswell? (was black incase people want to darken the colour at the bottom.)

I can post some screenshots tommorrow if anybody needs them….

What the gradient should look like

What the gradient actually looks like for some

Thanks
Jamie

EDIT:
I changed the second (transparent) colour in the gradient to white and it fixes the problem. However, I am still troubled to why the transparent black colour shows through in the middle? it must be something to do with JRE7 because thats where it occurs… maybe they changed something with how transparency in gradients work. Does anybody know how to eliminate this problem while keeping the colour black?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:56:57+00:00Added an answer on June 15, 2026 at 3:56 pm

    The problem with the code is this line:

    GradientPaint pGradient = new GradientPaint(0, 0, new Color(255, 255, 255, iDegreeWhite), 0, getHeight(), new Color(0, 0, 0, iDegreeBlack));
    

    should be this:

    GradientPaint pGradient = new GradientPaint(0, 0, new Color(255, 255, 255, iDegreeWhite), 0, getHeight(), new Color(255, 245, 62, iDegreeWhite));
    

    Looking back at your question, I see you’ve basically found the solution – but it’s a little different. Here’s why:

    When blending the colors in the gradient, your blending all aspects of the color: RBGA

    You see, until you reach the full second color, you are mixing black into the color gradient and that mix won’t be at the full transparency. So 20% of the way down the page, you’ll have this color: 204,204,204,144 (that’s 80% white, 20% black, and 56% opaque).

    The easiest solution is to avoid translucency completely if you’re not using it – just blend from the light yellow at the top to the dark yellow at the bottom. It takes less resources this way too.

    But since you’re using transparency, the solution I’ve provided uses transparency as well. You’ll be blending from the white to the yellow using a consistent transparency.

    If you blend from white to white (transparent), you’ll have the same problem as before only with white (which will be less noticeable since it’s one of the colors you’re using): The gradient will have a white “streak” until the second color reaches full transparency.

    As far as why it acts different on different JVMs, I’d guess that Oracle may have changed the way alpha’s are blended. Better alpha support seems to be something they’ve been working on for a while, and this is a logical step in that direction. I don’t have any proof on this statement though – it’s just based on other changes I’ve seen with alpha’s (like transparent windowing).

    EDIT
    This SSCCE demos both the problem and the solution:

    import java.awt.*;
    import javax.swing.*;
    import javax.swing.UIManager.LookAndFeelInfo;
    
    
    public class TransparencyDemo extends Box{
    
        protected JPanel pGradientPane;
    
        //Interface gradient specification
        private Color pInterfaceColour = new Color(255, 245, 62);
        protected int iDegreeWhite = 180;
        protected int iDegreeBlack = 0;
    
        public TransparencyDemo() {
            super(BoxLayout.X_AXIS);
            setOpaque(true);
    
            //Incorrect Solution
            pGradientPane = new JPanel(new GridBagLayout())
            {
                private static final long serialVersionUID = 1L;
    
                protected void paintComponent(Graphics pGraphics) 
                {
                    Graphics2D pGraphicsGradientRender = (Graphics2D) pGraphics;
                    pGraphicsGradientRender.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    GradientPaint pGradient = new GradientPaint(0, 0, new Color(255, 255, 255, iDegreeWhite), 0, getHeight(), new Color(0, 0, 0, iDegreeBlack));
                    pGraphicsGradientRender.setPaint(pGradient);
                    pGraphicsGradientRender.fillRect(0, 0, getWidth(), getHeight());
                    super.paintComponent(pGraphics);
                }
            };
            pGradientPane.setOpaque(false);
            add(pGradientPane);
    
            //Correct Solution
            JPanel pGradientPane2 = new JPanel(new GridBagLayout())
            {
                private static final long serialVersionUID = 1L;
    
                protected void paintComponent(Graphics pGraphics) 
                {
                    Graphics2D pGraphicsGradientRender = (Graphics2D) pGraphics;
                    pGraphicsGradientRender.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                    GradientPaint pGradient = new GradientPaint(0, 0, new Color(255, 255, 255, iDegreeWhite), 0, getHeight(),  new Color(255, 245, 62, iDegreeWhite));
                    pGraphicsGradientRender.setPaint(pGradient);
                    pGraphicsGradientRender.fillRect(0, 0, getWidth(), getHeight());
                    super.paintComponent(pGraphics);
                }
            };
            pGradientPane2.setOpaque(false);
            add(pGradientPane2);
    
    
            setBackground(pInterfaceColour);
    
        }
    
        public static void main(String[] args){
            try {
                 for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                    if ("Nimbus".equals(info.getName())) {
                       UIManager.setLookAndFeel(info.getClassName());
                       break;
                    }
                 }
              } catch (Exception e) {
                 e.printStackTrace();
              }
    
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.add(new TransparencyDemo());
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a program which will serialize and de-serialize, it does this fine
I wanted to write a program that test if two files are duplicates (have
I have a huge text file and I wanted to write a program which
I have a program that I run through the command line and I wanted
I have this complex iterations program I wrote in TI Basic to perform a
I have a program that draws a random triangle. I wanted to add a
I have a php program that needs a file that exists in a certain
Good day! I am developing a program using JavaFX SDK. I wanted to have
this is a very basic program I wanted to write a VB script which
i have C++ program exporting log files as HTML table and I wanted to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.