Gradient problems in Java

后端 未结 5 768
滥情空心
滥情空心 2021-01-04 05:52

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 transpa

5条回答
  •  -上瘾入骨i
    2021-01-04 06:19

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

提交回复
热议问题