【问题标题】:Java Swing Matte Border with Linear Gradient?带有线性渐变的Java Swing Matte边框?
【发布时间】:2012-04-21 15:33:43
【问题描述】:

我尝试了以下示例来应用线性渐变:GradientPaintDemo2D

效果很好,但我希望将渐变颜色应用到 Java Swing 遮罩边框中。我试过这个:

javax.swing.BorderFactory.createMatteBorder(1, 50, 1, 50, color)

但这仅适用于应用一个类型的Color,而不是GradientPaint

众所周知,GradientPaint 包含完全混合的两种颜色,如上面链接的示例所示。那么这个案例的替代答案是什么?

【问题讨论】:

    标签: java swing colors border gradient


    【解决方案1】:

    对于这种特定情况,您需要创建自己的边框。
    这是渐变边框类的示例:

    public static class GradientBorder implements Border
    {
        private Insets margin;
    
        public GradientBorder ( int top, int left, int bottom, int right )
        {
            super ();
            margin = new Insets ( top, left, bottom, right );
        }
    
        public void paintBorder ( Component c, Graphics g, int x, int y, int width, int height )
        {
            Graphics2D g2d = ( Graphics2D ) g;
            g2d.setPaint ( new GradientPaint ( x, y, Color.RED, x + width, y, Color.BLUE ) );
    
            Area border = new Area ( new Rectangle ( x, y, width, height ) );
            border.subtract ( new Area ( new Rectangle ( x + margin.left, y + margin.top,
                    width - margin.left - margin.right, height - margin.top - margin.bottom ) ) );
            g2d.fill ( border );
        }
    
        public Insets getBorderInsets ( Component c )
        {
            return margin;
        }
    
        public boolean isBorderOpaque ()
        {
            return true;
        }
    }
    

    当然,您可以指定任何其他渐变方向,颜色等。您也可以将它们的初始化放入构造函数中(如果需要)。要使用它,您只需要设置边框(在任何 JComponent 继任者中):

    jComponent.setBorder ( new GradientBorder ( 25, 50, 25, 50 ) );
    

    基本上,您可以使用任何颜色/动画/形状等创建任何您喜欢的边框

    顺便说一句 - isBorderOpaque() 方法应该返回 true,如果你想创建半透明边框(例如半透明颜色,圆角形状等),否则你将不得不处理组件重绘问题。

    【讨论】:

      【解决方案2】:

      用新的MatteBorder(int, int, int, int, javax.swing.Icon)怎么样

      //Another example:
      import java.awt.*;
      import java.awt.geom.*;
      import javax.swing.*;
      
      public class GradientMatteBorderTest {
        public JComponent makeUI() {
          Icon icon = new Icon() {
            @Override public void paintIcon(Component c, Graphics g, int x, int y) {
              Graphics2D g2  = (Graphics2D)g.create();
              Point2D start  = new Point2D.Float(0f, 0f);
              Point2D end    = new Point2D.Float(99f, 0f);
              float[] dist   = {0.0f, 0.5f, 1.0f};
              Color[] colors = { Color.RED, Color.YELLOW, Color.GREEN };
              g2.setPaint(new LinearGradientPaint(start, end, dist, colors));
              g2.fillRect(x, y, 100, 10);
              g2.dispose();
            }
            @Override public int getIconWidth()  { return 100; }
            @Override public int getIconHeight() { return 10;  }
          };
          JLabel label = new JLabel("GradientMatteBorder");
          label.setBorder(BorderFactory.createMatteBorder(10,5,10,0,icon));
          JPanel p = new JPanel(new BorderLayout());
          p.setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
          p.add(label, BorderLayout.NORTH);
          return p;
        }
        public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
              createAndShowGUI();
            }
          });
        }
        public static void createAndShowGUI() {
          JFrame f = new JFrame();
          f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          f.getContentPane().add(new GradientMatteBorderTest().makeUI());
          f.setSize(320, 240);
          f.setLocationRelativeTo(null);
          f.setVisible(true);
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-05-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多