java - 矩形渐变

标签 java draw gradient

我正在尝试为我的用户界面中的某些内容实现一个draw(Graphics2D g) 方法。不是swing组件,是我自己画的。我正在尝试创建像这张图片一样的渐变边框

gradient border

我怎样才能画出类似于那个的渐变?

最佳答案

你并没有真正指定你想要什么,但如果它是我认为的,那么它有点类似于我在my answer to "How to make gradient border of an image using java?"中写的.

这里的技巧是不要创建单一的渐变颜料,也不要填充单一的形状。相反,必须用渐变填充的区域分为 8 个部分:四个边和四个角。边缘充满了简单的渐变颜料。角落里充满了径向渐变颜料。然后它只是关于摆弄坐标,但是当内部形状被指定为 Rectangle2D 时,这可以通过一种相当通用的方式来完成。

但是,结果可能如下所示:

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.RadialGradientPaint;
import java.awt.Shape;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.geom.RoundRectangle2D;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class RectangularGradientTest
{
    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    private static void createAndShowGUI()
    {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.getContentPane().add(new RectangularGradientTestPanel());
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}


class RectangularGradientTestPanel extends JPanel
{
    @Override
    protected void paintComponent(Graphics gr)
    {
        super.paintComponent(gr);
        Graphics2D g = (Graphics2D)gr;
        Rectangle2D r = new Rectangle2D.Double(100,100,200,100);
        draw(g, r, 75);

        Shape rr = new RoundRectangle2D.Double(80,80,240,140,20,20);
        g.setColor(Color.BLACK);
        g.fill(rr);
    }

    @Override
    public Dimension getPreferredSize()
    {
        if (isPreferredSizeSet())
        {
            return super.getPreferredSize();
        }
        return new Dimension(400,300);
    }


    private static void draw(Graphics2D g, Rectangle2D r, double s)
    {
        Color c0 = new Color(255,0,0);
        Color c1 = new Color(255,0,0,0);

        double x0 = r.getMinX();
        double y0 = r.getMinY();
        double x1 = r.getMaxX();
        double y1 = r.getMaxY();
        double w = r.getWidth();
        double h = r.getHeight();

        // Left
        g.setPaint(new GradientPaint(
            new Point2D.Double(x0, y0), c0,
            new Point2D.Double(x0 - s, y0), c1));
        g.fill(new Rectangle2D.Double(x0 - s, y0, s, h));

        // Right
        g.setPaint(new GradientPaint(
            new Point2D.Double(x1, y0), c0,
            new Point2D.Double(x1 + s, y0), c1));
        g.fill(new Rectangle2D.Double(x1, y0, s, h));

        // Top
        g.setPaint(new GradientPaint(
            new Point2D.Double(x0, y0), c0,
            new Point2D.Double(x0, y0 - s), c1));
        g.fill(new Rectangle2D.Double(x0, y0 - s, w, s));

        // Bottom
        g.setPaint(new GradientPaint(
            new Point2D.Double(x0, y1), c0,
            new Point2D.Double(x0, y1 + s), c1));
        g.fill(new Rectangle2D.Double(x0, y1, w, s));

        float fractions[] = new float[] { 0.0f, 1.0f };
        Color colors[] = new Color[] { c0, c1 };

        // Top Left
        g.setPaint(new RadialGradientPaint(
            new Rectangle2D.Double(x0 - s, y0 - s, s + s, s + s), 
            fractions, colors, CycleMethod.NO_CYCLE));
        g.fill(new Rectangle2D.Double(x0 - s, y0 - s, s, s));

        // Top Right
        g.setPaint(new RadialGradientPaint(
            new Rectangle2D.Double(x1 - s, y0 - s, s + s, s + s), 
            fractions, colors, CycleMethod.NO_CYCLE));
        g.fill(new Rectangle2D.Double(x1, y0 - s, s, s));

        // Bottom Left
        g.setPaint(new RadialGradientPaint(
            new Rectangle2D.Double(x0 - s, y1 - s, s + s, s + s), 
            fractions, colors, CycleMethod.NO_CYCLE));
        g.fill(new Rectangle2D.Double(x0 - s, y1, s, s));

        // Bottom Right
        g.setPaint(new RadialGradientPaint(
            new Rectangle2D.Double(x1 - s, y1 - s, s + s, s + s), 
            fractions, colors, CycleMethod.NO_CYCLE));
        g.fill(new Rectangle2D.Double(x1, y1, s, s));
    }

}

关于java - 矩形渐变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24717893/

相关文章:

java - 弃用一组相关方法的最佳做法是什么?

java - 我无法通过 ssh 连接到 mysql,我不知道为什么使用 Java

java - Guava 加载缓存 : Why use refreshAfterWrite and expireAfterWrite together

css - 是否有 “official”/标准 CSS3 渐变语法?

java - java中是否提供了任何钩子(Hook)来注册具有垃圾收集的线程?

javascript - JS 随机化 lineTo

c++ - 在白色背景上绘制球体(C++ 和 OpenGL)

delphi - 如何保留确定的表格区域不被剪切?

css - safari 浏览器的底部边框渐变支持

html - CSS 渐变 + 噪声作为 div 背景填充