【问题标题】:java: How to add Transparent Gradient Background to JFramejava:如何将透明渐变背景添加到 JFrame
【发布时间】:2011-07-20 17:49:22
【问题描述】:

java:我想对 JFrame 使用渐变风格的透明背景。 顶部透明度应为 100%,但向下时应继续降低,底部应为 20%

我知道我可以使用已经具有这种效果的图像,但我想提供主题工具并允许用户使用他们最喜欢的图像,但在运行时允许透明。

【问题讨论】:

  • 你说的是GradientPaint吗?
  • 我想使用渐变样式的透明度
  • +1 这是一个有趣的问题。我认为RescaleOp 会参与其中,但整个“渐变透明度”让我陷入了循环!我也会喜欢这个,所以我可以在下班后回来。 :)

标签: java swing jframe gradient


【解决方案1】:

Sun 在 6u10 中为 java 添加了对半透明背景的支持,但在 API 中没有正式描述。 在 Java 7 中,该功能通过 Window 类的 setBackground()、setOpacity() 和 setShape() 方法正式添加到 API。

该行为由 Oracle here 描述

在底部有一个渐变效果的代码示例。

该技术只有在底层操作系统窗口管理器支持时才有效。 X11 (Linux) 需要正确安装和配置合成窗口管理器。这在 Java 7 发行说明的known issues 和此bug 中有记录。

【讨论】:

  • 这意味着...它在 LInux 环境中不受支持...? @亚伦
  • @gumuruh 我已经更新了有关 Linux 支持的更多详细信息。根据您的配置,它可能受支持。
  • ic, ic, ic... 我也应该在 Mac 上尝试一下... 因为基本上我的旧 Mac 是 BSD ~linux 系列。 @Aaron,谢谢!
【解决方案2】:

只有 Mac OSX 在 Java 中提供透明/半透明框架。

如果您拥有一台 Mac,那么您应该首先将背景颜色设置为透明:

frame.setBackground(new Color(0, 0, 0, 0));

然后 -- 我认为(我还没有 Mac,但:P) -- 覆盖 paintComponents(Graphics) 方法:

public void paintComponents(Graphics g)
{
     Graphics2D g2 = (Graphics2D) g;
     g2.setPaint(new GradientPaint(
     new Point(0, 0), 
     new Color(0.0f, 0.0f, 0.0f, 0.0f), 
     new Point(0, getHeight()), 
     new Color(0.0f, 0.0f, 0.0f, 0.8f)));
     super.paintComponents(g);
}

【讨论】:

  • JFrame 没有 setOpaque(...) 方法。
【解决方案3】:

我实际上是在一个 JFrame 扩展类中工作,在 JFrame 中使用半透明渐变

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JRootPane;

public class JFrameGradient extends JFrame
{
private static final long serialVersionUID = 7565267933439533086L;
private Color initialColor;
private Color finalColor;
public static final int VERTICAL = 0;
public static final int HORIZONTAL = 1;
private int orientation = VERTICAL;

private int MAX_WIDTH = 1;
private int MAX_HEIGHT = 1;

private int xInitial = 0;
private int xFinal = 0;
private int yInitial = 0;
private int yFinal = 0;

private int initialRedComponent;
private int initialGreenComponent;
private int initialBlueComponent;

private int finalRedComponent;
private int finalGreenComponent;
private int finalBlueComponent;

private int delta = 1;
private String title;
private GraphicsConfiguration gc;
private JFrame frame;
private Graphics g;
private double transparency;
private JPanel gradientPane = new JPanel(true);
private boolean isColorChanged = false;



 public JFrameGradient( Color initialColor, Color finalColor , double transparency )
{
    super();
    this.frame = this;
    setTransparency(transparency);
    initClass();
    initColor(initialColor, finalColor);
}

 public JFrameGradient( String title , Color initialColor, Color finalColor , double transparency)
 {
     super(title);
     this.title = title;
     setTransparency(transparency);
     initClass();
     initColor(initialColor, finalColor);
 }

 public JFrameGradient( GraphicsConfiguration gc , Color initialColor, Color finalColor , double transparency)
 {
     super(gc);
     this.setGc(gc);
     setTransparency(transparency);
     initClass();
     initColor(initialColor, finalColor);
 }

 public JFrameGradient( GraphicsConfiguration gc , String title , Color initialColor, Color finalColor , double transparency)
 {
     super( title , gc );
     this.title = title;
     this.setGc(gc);
     setTransparency(transparency);
     initClass();
     initColor(initialColor, finalColor);
 }

 public JFrameGradient(  double transparency)
 {
     this( Color.black , Color.white , transparency);
 }

 public JFrameGradient(String title , double transparency)
 {
     this( title , Color.black , Color.white , transparency);
 }

 public JFrameGradient( GraphicsConfiguration gc , double transparency)
 {
     this( gc , Color.black , Color.white , transparency);
 }

 public JFrameGradient( GraphicsConfiguration gc , String title , double transparency)
 {
     this( gc , title , Color.black , Color.white , transparency);
 }

 public JFrameGradient()
 {
     this( Color.black , Color.white , 1);
 }

 public JFrameGradient(String title)
 {
     this( title , Color.black , Color.white , 1);
 }

 public JFrameGradient( GraphicsConfiguration gc)
 {
     this( gc , Color.black , Color.white , 1);
 }

 public JFrameGradient( GraphicsConfiguration gc , String title)
 {
     this( gc , title , Color.black , Color.white , 1);
 }

public Color getInitialColor()
{
    return initialColor;
}

public Color getFinalColor()
{
    return finalColor;
}

public void setInitialColor(Color initialColor)
{
    this.initialColor = initialColor;
    isColorChanged = true;
    if(isVisible())
    {
        repaint();
    }
}

public void setFinalColor(Color finalColor)
{
    this.finalColor = finalColor;
    isColorChanged = true;
    if(isVisible())
    {
        repaint();
    }
}

public String getTitle()
{
    return title;
}

public double getTransparency()
{
    return transparency;
}

public void setTransparency(double transparency)
{
    if( ( transparency >= 0.0) && (transparency <= 1.0) )
    {
        this.transparency = transparency;
    }
    else
    {
        this.transparency = 1.0;
    }
}

/**
 * @return the gc
 */
public GraphicsConfiguration getGc()
{
    return gc;
}

/**
 * @param gc the gc to set
 */
private void setGc(GraphicsConfiguration gc)
{
    this.gc = gc;
}

public int getOrientation()
{
    return orientation;
}

public void setOrientation(int orientation)
{
    this.orientation = orientation;
    if( isVisible())
    {
        repaint();
    }

}


private void initColor(Color initialColor, Color finalColor)
{
    this.initialColor = initialColor;
    this.finalColor = finalColor;

    this.initialRedComponent = initialColor.getRed();
    this.initialGreenComponent = initialColor.getGreen();
    this.initialBlueComponent = initialColor.getBlue();

    this.finalRedComponent = finalColor.getRed();
    this.finalGreenComponent = finalColor.getGreen();
    this.finalBlueComponent = finalColor.getBlue();

}

private void initClass()
{
    if( this != null)
    {
        frame = this;
        frame.add(gradientPane);
        frame.pack();
    }
}

@Override
public void paint(Graphics g)
{
    super.paint(g);
    paintGradient(g);
    Rectangle mask = null;

    for( Component c : getComponents() )
    {
        if( c instanceof JRootPane )
        {
            JRootPane rootPane = (JRootPane) c;
            rootPane.setDoubleBuffered(true);
            for( Component cRootPane : rootPane.getComponents())
            {
                 if( cRootPane instanceof JLayeredPane)
                 {
                    JLayeredPane cLayerPanels = (JLayeredPane) cRootPane;
                    cLayerPanels.setDoubleBuffered(true);
                    for( Component cLayerPanel : cLayerPanels.getComponents() )
                    {
                        if( cLayerPanel instanceof JPanel)
                        {
                            JPanel cPanels = (JPanel) cLayerPanel;
                            cPanels.setDoubleBuffered(true);

                            for( Component cPanel : cPanels.getComponents() )
                            {
                                if( cPanel instanceof JPanel)
                                {
                                    JPanel cPanels2 = (JPanel) cPanel;
                                    cPanels2.setDoubleBuffered(true);
                                     mask = frame.getBounds();
                                    for( Component cPanel2 : cPanels2.getComponents() )
                                    {
                                        mask.union(cPanel2.getBounds());
                                        cPanel2.paint(cPanel2.getGraphics());
                                    }

                                }
                            }
                        }
                    }
                }
            }
        }
    }
    g.setClip(mask);
}


private void paintGradient(Graphics g)
{
    if( isColorChanged == true)
    {
        initColor(initialColor, finalColor);
        isColorChanged = false;
    }

    if ( orientation == VERTICAL)
    {
        if ( this.getWidth() != 0 )
        {
            MAX_WIDTH = this.getWidth();
        }

        if ( this.getHeight() != 0)
        {
            MAX_HEIGHT = this.getHeight();
            xFinal = MAX_WIDTH;
            delta = MAX_HEIGHT;
        }

    } else if( orientation == HORIZONTAL )
    {
        if ( this.getHeight() != 0 )
        {
            MAX_HEIGHT = this.getHeight();
        }

        if ( this.getWidth() != 0)
        {
            MAX_WIDTH = this.getWidth();
            yFinal = MAX_HEIGHT;
            delta = MAX_WIDTH;
        }

    }

    if ( (this.initialColor != null ) && (this.finalColor != null) )
    {

        if ( delta == 0)
        {
            delta = 1;
        }

         if( orientation == VERTICAL)
         {
             for( int n = 0 ; n < MAX_HEIGHT ; n++)
             {
                 calculateColor(g, n);
                 g.drawLine(xInitial, n, xFinal, n);
             }
         } else if (orientation == HORIZONTAL)
         {
            for( int n = 0 ; n < MAX_WIDTH ; n++)
             {
                 calculateColor(g, n);
                 g.drawLine(n , yInitial, n , yFinal);
                 delta = Math.max( MAX_HEIGHT , MAX_WIDTH);
              }
          }



     }

    }

private void calculateColor(Graphics g, int n)
{
    int redComponent = 0;
    int greenComponent = 0;
    int blueComponent = 0;

    redComponent = interpolateComponent(n, initialRedComponent, finalRedComponent, delta);
     greenComponent = interpolateComponent(n, initialGreenComponent, finalGreenComponent, delta);
     blueComponent = interpolateComponent(n, initialBlueComponent, finalBlueComponent, delta);

     if( redComponent > 255)
     {
         redComponent = 255;
     } else if ( redComponent < 0)
     {
         redComponent = 0;
     }

     if( greenComponent > 255)
     {
         greenComponent = 255;
     } else if ( greenComponent < 0)
     {
         greenComponent = 0;
     }

     if( redComponent > 255)
     {
         blueComponent = 255;
     } else if ( redComponent < 0)
     {
         blueComponent = 0;
     }

     g.setColor( new Color(redComponent , greenComponent , blueComponent , (int) (255 * transparency) ));
}

private int interpolateComponent( int x , int xInit , int xFinal , int delta )
{
    int returnValue = 0 ;

    if ( xInit < xFinal ) {
        double slope =  (xFinal - xInit) / (double) delta ;

        returnValue = (int) ( xInit + slope * x );
    }
    else if( xFinal < xInit ) {
        double slope =  (xInit - xFinal) / (double) delta ;

        returnValue = (int) ( xInit - slope * x );
    } else {
        returnValue = xInit;
    }


    return returnValue;

}

@Override
public void setVisible(boolean aFlag)
{
    if (aFlag == true)
    {
        g = gradientPane.getGraphics();
        paint(g);
        frame.pack();
    }

        super.setVisible(aFlag);

}
public void setPreferredSize(int width, int height)
{
    setPreferredSize(new Dimension(width, height));
}

}

【讨论】:

  • 谢谢.. 这实际上效果很好,让我可以将渐变应用到 JFrame 组件。然而,当框架加载时,框架上绘制的所有组件都是不可见/隐藏的,我需要将鼠标悬停在它们上以使其可见。你能弄清楚为什么会这样吗?添加组件后,我确实验证并重新绘制,但没有太大帮助。
猜你喜欢
  • 1970-01-01
  • 2013-03-22
  • 2011-02-01
  • 2014-05-28
  • 1970-01-01
  • 2012-05-21
  • 2012-07-27
  • 2015-09-02
  • 2013-06-05
相关资源
最近更新 更多