Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用java制作图像的渐变边框?_Java_Image Processing_Graphics_Gradient_Bufferedimage - Fatal编程技术网

如何使用java制作图像的渐变边框?

如何使用java制作图像的渐变边框?,java,image-processing,graphics,gradient,bufferedimage,Java,Image Processing,Graphics,Gradient,Bufferedimage,如何将图像边框设置为渐变。我在谷歌上搜索了很多,但没有找到正确的建议。任何人都可以帮助我 任何建议请…创建一个相同大小的透明图像,绘制白色边框并使其模糊。然后将其复制到要使用渐变边框的图像顶部 参考: 希望有帮助 这是一个有趣的问题。我首先认为应该有一个简单的解决方案,使用一些Graphics#drawRoundRect调用适当的Paint,但它不是那么简单 但是,以下示例中实现了一种解决方案: 图像按原样绘制为新图像。然后对边和角进行喷漆。这些由矩形组成。一条边的每个矩形都填充有一个Gra

如何将图像边框设置为渐变。我在谷歌上搜索了很多,但没有找到正确的建议。任何人都可以帮助我


任何建议请…

创建一个相同大小的透明图像,绘制白色边框并使其模糊。然后将其复制到要使用渐变边框的图像顶部

参考:


希望有帮助

这是一个有趣的问题。我首先认为应该有一个简单的解决方案,使用一些
Graphics#drawRoundRect
调用适当的
Paint
,但它不是那么简单

但是,以下示例中实现了一种解决方案:

图像按原样绘制为新图像。然后对边和角进行喷漆。这些由矩形组成。一条边的每个矩形都填充有一个
GradientPaint
,它在“完全透明”和“完全不透明”之间插值。类似地,角点的矩形用相同颜色之间的
RadialGradientPaint
插值填充。这些都是用
AlphaComposite.DstOut
构图规则绘制的,因此图像的实际像素慢慢地向边界“混合”

(棋盘格图案仅在组件的背景中绘制,以强调其朝向边界处的透明像素)

导入java.awt.AlphaComposite;
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.GradientPaint;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.GridLayout;
导入java.awt.MultipleGradientPaint.CycleMethod;
导入java.awt.RadialGradientPaint;
导入java.awt.geom.Point2D;
导入java.awt.geom.Rectangle2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.SwingUtilities;
公共类边界测试
{
公共静态void main(字符串[]args)
{
SwingUtilities.invokeLater(新的Runnable()
{
@凌驾
公开募捐
{
createAndShowGUI();
}
});
}
私有静态void createAndShowGUI()
{
JFrame f=新的JFrame();
f、 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BuffereImage输入=空;
尝试
{
input=ImageIO.read(新文件(“KCR0B.jpg”);
}
捕获(IOE异常)
{
e、 printStackTrace();
}
BuffereImage输出=模糊边界(输入,20);
f、 getContentPane().setLayout(新的GridLayout());
f、 getContentPane().add(新建图像面板(输入));
f、 getContentPane().add(新的ImagePanel(输出));
f、 包装();
f、 setLocationRelativeTo(空);
f、 setVisible(真);
}
专用静态BuffereImage模糊边框(BuffereImage输入,双边框)
{
int w=input.getWidth();
inth=input.getHeight();
BuffereImage输出=新的BuffereImage(
w、 h,buffereImage.TYPE_INT_ARGB);
Graphics2D g=output.createGraphics();
g、 drawImage(输入,0,0,空);
g、 setComposite(AlphaComposite.DstOut);
颜色c0=新颜色(0,0,0255);
颜色c1=新颜色(0,0,0,0);
双cy=边界;
双cx=边界;
//左
g、 setPaint(新GradientPaint(
新点2D.双(0,cy),c0,
新点2d.Double(cx,cy,c1));
g、 填充(新矩形2D.双(
0,cy,cx,h-cy-cy);
//对
g、 setPaint(新GradientPaint(
新点2D.双(w-cx,cy),c1,
新点2d.Double(w,cy,c0));
g、 填充(新矩形2D.双(
w-cx,cy,cx,h-cy-cy);
//顶
g、 setPaint(新GradientPaint(
新点2D.双精度(cx,0),c0,
新点2d.Double(cx,cy,c1));
g、 填充(新矩形2D.双(
cx,0,w-cx-cx,cy);
//底部
g、 setPaint(新GradientPaint(
新点2D.双(cx,h-cy),c1,
新点2d.Double(cx,h,c0));
g、 填充(新矩形2D.双(
cx,h-cy,w-cx-cx,cy);
//左上角
g、 setPaint(新的RadialGradientPaint(
新矩形2D.双(0,0,cx+cx,cy+cy),
新的浮点[]{0,1},新的颜色[]{c1,c0},CycleMethod.NO_CYCLE));
g、 填充(新矩形2D.Double(0,0,cx,cy));
//右上角
g、 setPaint(新的RadialGradientPaint(
新的二维双矩形(w-cx-cx,0,cx+cx,cy+cy),
新的浮点[]{0,1},新的颜色[]{c1,c0},CycleMethod.NO_CYCLE));
g、 填充(新矩形2D.Double(w-cx,0,cx,cy));
//左下角
g、 setPaint(新的RadialGradientPaint(
新的二维双矩形(0,h-cy-cy,cx+cx,cy+cy),
新的浮点[]{0,1},新的颜色[]{c1,c0},CycleMethod.NO_CYCLE));
g、 填充(新矩形2D.Double(0,h-cy,cx,cy));
//右下角
g、 setPaint(新的RadialGradientPaint(
新的二维双矩形(w-cx-cx、h-cy-cy、cx+cx、cy+cy),
新的浮点[]{0,1},新的颜色[]{c1,c0},CycleMethod.NO_CYCLE));
g、 填充(新矩形2D.双(w-cx,h-cy,cx,cy));
g、 处置();
返回输出;
}
静态类ImagePanel扩展了JPanel
{
私有最终缓冲图像;
图像面板(缓冲图像图像)
{
这个图像=图像;
}
@凌驾
公共维度getPreferredSize()
{
if(super.isPreferredSizeSet())
{
返回super.getPreferredSize();
}
返回新维度(image.getWidth(),image.getHeight());
}
@凌驾
受保护组件(图形g)
{
超级油漆组件
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.MultipleGradientPaint.CycleMethod;
import java.awt.RadialGradientPaint;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

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


public class BorderBlurTest
{
    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);

        BufferedImage input = null;
        try
        {
            input = ImageIO.read(new File("KCR0B.jpg"));
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        BufferedImage output = blurBorder(input, 20);
        f.getContentPane().setLayout(new GridLayout());
        f.getContentPane().add(new ImagePanel(input));
        f.getContentPane().add(new ImagePanel(output));
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    private static BufferedImage blurBorder(BufferedImage input, double border)
    {
        int w = input.getWidth();
        int h = input.getHeight();
        BufferedImage output = new BufferedImage(
            w, h, BufferedImage.TYPE_INT_ARGB);

        Graphics2D g = output.createGraphics();
        g.drawImage(input, 0, 0, null);

        g.setComposite(AlphaComposite.DstOut);
        Color c0 = new Color(0,0,0,255);
        Color c1 = new Color(0,0,0,0);

        double cy = border;
        double cx = border;

        // Left
        g.setPaint(new GradientPaint(
            new Point2D.Double(0, cy), c0,
            new Point2D.Double(cx,cy), c1));
        g.fill(new Rectangle2D.Double(
            0, cy, cx, h-cy-cy));

        // Right
        g.setPaint(new GradientPaint(
            new Point2D.Double(w-cx, cy), c1,
            new Point2D.Double(w,cy), c0));
        g.fill(new Rectangle2D.Double(
            w-cx, cy, cx, h-cy-cy));

        // Top
        g.setPaint(new GradientPaint(
            new Point2D.Double(cx, 0), c0,
            new Point2D.Double(cx, cy), c1));
        g.fill(new Rectangle2D.Double(
            cx, 0, w-cx-cx, cy));

        // Bottom
        g.setPaint(new GradientPaint(
            new Point2D.Double(cx, h-cy), c1,
            new Point2D.Double(cx, h), c0));
        g.fill(new Rectangle2D.Double(
            cx, h-cy, w-cx-cx, cy));


        // Top Left
        g.setPaint(new RadialGradientPaint(
            new Rectangle2D.Double(0, 0, cx+cx, cy+cy),
            new float[]{0,1}, new Color[]{c1, c0}, CycleMethod.NO_CYCLE));
        g.fill(new Rectangle2D.Double(0, 0, cx, cy));

        // Top Right
        g.setPaint(new RadialGradientPaint(
            new Rectangle2D.Double(w-cx-cx, 0, cx+cx, cy+cy),
            new float[]{0,1}, new Color[]{c1, c0}, CycleMethod.NO_CYCLE));
        g.fill(new Rectangle2D.Double(w-cx, 0, cx, cy));

        // Bottom Left
        g.setPaint(new RadialGradientPaint(
            new Rectangle2D.Double(0, h-cy-cy, cx+cx, cy+cy),
            new float[]{0,1}, new Color[]{c1, c0}, CycleMethod.NO_CYCLE));
        g.fill(new Rectangle2D.Double(0, h-cy, cx, cy));

        // Bottom Right
        g.setPaint(new RadialGradientPaint(
            new Rectangle2D.Double(w-cx-cx, h-cy-cy, cx+cx, cy+cy),
            new float[]{0,1}, new Color[]{c1, c0}, CycleMethod.NO_CYCLE));
        g.fill(new Rectangle2D.Double(w-cx, h-cy, cx, cy));

        g.dispose();

        return output;
    }


    static class ImagePanel extends JPanel
    {
        private final BufferedImage image;

        ImagePanel(BufferedImage image)
        {
            this.image = image;
        }

        @Override
        public Dimension getPreferredSize()
        {
            if (super.isPreferredSizeSet())
            {
                return super.getPreferredSize();
            }
            return new Dimension(image.getWidth(), image.getHeight());
        }

        @Override
        protected void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            int s = 8;
            int w = getWidth();
            int h = getHeight();
            for (int x=0; x<w; x++)
            {
                for (int y=0; y<h; y++)
                {
                    if (((x+y) & 1) == 0)
                    {
                        g.setColor(Color.WHITE);
                    }
                    else
                    {
                        g.setColor(Color.LIGHT_GRAY);
                    }
                    g.fillRect(x*s,y*s,s,s);
                }
            }
            g.drawImage(image, 0, 0, null);
        }
    }

}