我想创建一个带有圆角的自定义边框。

代码-

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.border.AbstractBorder;

class JRoundedCornerBorder extends AbstractBorder
{
    private static final long serialVersionUID = 7644739936531926341L;
    private static final int THICKNESS = 5;

    JRoundedCornerBorder()
    {
        super();
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
    {
        Graphics2D g2 = (Graphics2D)g.create();

        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        if(c.hasFocus())
        {
            g2.setColor(Color.BLUE);
        }
        else
        {
            g2.setColor(Color.BLACK);
        }
        g2.setStroke(new BasicStroke(THICKNESS, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2.drawRect(x, y, width - 1, height - 1);

        g2.dispose();
    }

    @Override
    public Insets getBorderInsets(Component c)
    {
        return new Insets(THICKNESS, THICKNESS, THICKNESS, THICKNESS);
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets)
    {
        insets.left = insets.top = insets.right = insets.bottom = THICKNESS;
        return insets;
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                final JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new FlowLayout());

                // Add button with custom border
                final JButton button = new JButton("Hello");
                button.setBorder(new JRoundedCornerBorder());
                frame.add(button);

                // Add button without custom border
                frame.add(new JButton("Goodbye"));

                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

结果-



如您所见,Graphics.drawRect完全忽略BasicStroke.CAP_ROUNDBasicStroke.JOIN_ROUND属性。为什么?

最佳答案

Learning Java 2D, Part 1所述:

java.awt.BasicStroke.CAP_ROUND:这将使圆帽以端点为中心,并具有笔宽的直径。

关键字是“居中”。我相信,在使用粗笔画进行绘制时,Java2D总是会在假设像素的起点和终点坐标之间的无穷细线处将线的粗线居中。例如,当绘制一条7像素粗的垂直蓝线时,Java2D在要绘制的假设线段的每一侧绘制3像素。

在您的示例中,厚度为5像素。您需要偏移坐标才能在图形剪辑中完全绘制笔划。通过移动2个像素(或THICKNESS/2),圆角变为可见:

//...
        g2.setStroke(new BasicStroke(THICKNESS, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
        g2.drawRect(x + THICKNESS/2, y + THICKNESS/2, width - 2*(THICKNESS/2) - 1, height - 2*(THICKNESS/2) - 1);

        g2.dispose();
    }

    @Override
    public Insets getBorderInsets(Component c) {
        return new Insets(THICKNESS + THICKNESS/2, THICKNESS + THICKNESS/2, THICKNESS + THICKNESS/2, THICKNESS + THICKNESS/2);
    }
//...

08-17 19:09