1 回答

TA貢獻(xiàn)1871條經(jīng)驗(yàn) 獲得超13個贊
圓角的效果取決于這些圓角的大小。在的情況下LineBorder,由thickness屬性決定。這是相關(guān)實(shí)現(xiàn)代碼的樣子:
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);
所以它區(qū)分了內(nèi)角和外角,這對于線大小為1的意義不大。但更糟糕的是,外角大小只是offs,與thickness(在您的情況下為一個)相同,而內(nèi)圓角的大小由 決定arc,即.2f * offs。對于您中thickness的一個,生成的內(nèi)角大小為0.2。因此,我們在左上角看到效果似乎純屬巧合(這兩個不同角的圓角問題),因?yàn)榧词挂粋€較大的外角尺寸也不足以創(chuàng)建可見的圓角效果。
下面是 a thicknessof 的樣子20,這會導(dǎo)致外角大小為 ,20而內(nèi)角大小為4:
它不知道 Swing 開發(fā)人員在該類中添加圓角支持時考慮的是哪個實(shí)際用例。我無法想象這種策略有用的任何場景。
實(shí)現(xiàn)一個有意義Border的并不難。一種可能的實(shí)現(xiàn)如下所示:
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);
}
}
}
現(xiàn)在,當(dāng)我改變線路時
b.setBorder(BorderFactory.createLineBorder(Color.BLACK, 1, true));
在你的例子中
b.setBorder(new RoundedLineBorder(Color.BLACK, 1, 10, true));
我得到
添加回答
舉報