gpt4 book ai didi

java - Rounded LineBorder - 不是所有的角都是圆的

转载 作者:行者123 更新时间:2023-11-30 07:44:10 26 4
gpt4 key购买 nike

我正在使用 JFreeChart,我想通过创建我自己的 Class 来自定义 ToolTip,它扩展了 ChartPanel 并覆盖 createToolTip()

static private class PrivateChartPanel extends ChartPanel{

//constructors

@Override
public JToolTip createToolTip() {

JToolTip jtt = super.createToolTip();

jtt.setBackground(Color.WHITE);

jtt.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));

return jtt;

}

}

问题出在边界。它并非在所有角落都是圆形的。

tooltip-border-problem

为什么它不是在所有角落都是圆的,我是怎么做到的?

P.S.: 我创建了一个新的简单项目

import java.awt.Color;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class HelloWorld {

public static void main(String[] args) {

JFrame a = new JFrame();

a.setBounds(100, 100, 100, 100);
a.setLayout(null);

JPanel b = new JPanel();

b.setBounds(5, 5, 50, 50);
b.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));

a.add(b);

a.setVisible(true);

}

}

JPanel 的 Border 也有同样的问题。我正在使用 Java 10

最佳答案

圆角的效果取决于这些圆角的大小。在 LineBorder 的情况下,它由 thickness 属性确定。相关的实现代码是这样的:

int offs = this.thickness;
int size = offs + offs;
if (this.roundedCorners) {
float arc = .2f * offs;
outer = new RoundRectangle2D.Float(x, y, width, height, offs, offs);
inner = new RoundRectangle2D.Float(x + offs, y + offs, width - size, height - size, arc, arc);
}
else {
outer = new Rectangle2D.Float(x, y, width, height);
inner = new Rectangle2D.Float(x + offs, y + offs, width - size, height - size);
}
Path2D path = new Path2D.Float(Path2D.WIND_EVEN_ODD);
path.append(outer, false);
path.append(inner, false);
g2d.fill(path);

所以它区分了内角和外角,这对于1的线宽意义不大。但更糟糕的是,外角尺寸只是 offs,与 thickness 相同(在您的情况下为一个),内圆角的尺寸由 确定>arc,即 .2f * offs。对于您的 thickness 之一,生成的内角尺寸为 0.2。因此,我们在左上角看到效果似乎纯属巧合(这两个不同角的圆角问题),因为即使是更大的外角尺寸也不足以产生可见的圆角效果。

这是 thickness20 的样子,这导致外角大小为 20 和一个巨大的内角4 的大小:

Screenshot with border thickness of 20

不知道 Swing 开发人员在此类中添加圆角支持时考虑的实际用例。我无法想象这种策略在任何情况下都有用。

实现一个有意义的 Border 并不难。一种可能的实现方式如下:

public class RoundedLineBorder extends AbstractBorder {
int lineSize, cornerSize;
Paint fill;
Stroke stroke;
private Object aaHint;

public RoundedLineBorder(Paint fill, int lineSize, int cornerSize) {
this.fill = fill;
this.lineSize = lineSize;
this.cornerSize = cornerSize;
stroke = new BasicStroke(lineSize);
}
public RoundedLineBorder(Paint fill, int lineSize, int cornerSize, boolean antiAlias) {
this.fill = fill;
this.lineSize = lineSize;
this.cornerSize = cornerSize;
stroke = new BasicStroke(lineSize);
aaHint = antiAlias? RenderingHints.VALUE_ANTIALIAS_ON: RenderingHints.VALUE_ANTIALIAS_OFF;
}

@Override
public Insets getBorderInsets(Component c, Insets insets) {
int size = Math.max(lineSize, cornerSize);
if(insets == null) insets = new Insets(size, size, size, size);
else insets.left = insets.top = insets.right = insets.bottom = size;
return insets;
}

@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
Graphics2D g2d = (Graphics2D)g;
Paint oldPaint = g2d.getPaint();
Stroke oldStroke = g2d.getStroke();
Object oldAA = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
try {
g2d.setPaint(fill!=null? fill: c.getForeground());
g2d.setStroke(stroke);
if(aaHint != null) g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, aaHint);
int off = lineSize >> 1;
g2d.drawRoundRect(x+off, y+off, width-lineSize, height-lineSize, cornerSize, cornerSize);
}
finally {
g2d.setPaint(oldPaint);
g2d.setStroke(oldStroke);
if(aaHint != null) g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, oldAA);
}
}
}

现在,当我换行的时候

b.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));

在你的例子中

b.setBorder(new RoundedLineBorder(Color.BLACK, 1, 10, true));

我明白了

Screenshot with custom border

关于java - Rounded LineBorder - 不是所有的角都是圆的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52759203/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com