第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Rounded LineBorder - 并非所有角都是圓角

Rounded LineBorder - 并非所有角都是圓角

慕桂英3389331 2021-11-11 16:45:19
我正在使用JFreeChart并且我想ToolTip通過創(chuàng)建我自己的Class擴(kuò)展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;    }}問題出在Border。它不是在所有角落都是圓形的。為什么它不是在所有角落都圓潤的,我是怎么做到的?PS:我新建了一個簡單的項目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 的邊框也有同樣的問題。我正在使用Java 10
查看完整描述

1 回答

?
慕桂英4014372

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:

http://img1.sycdn.imooc.com//618cd84600019a1701180091.jpg

它不知道 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));

我得到

http://img1.sycdn.imooc.com//618cd8570001414401190091.jpg

查看完整回答
反對 回復(fù) 2021-11-11
  • 1 回答
  • 0 關(guān)注
  • 229 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號