可重复使用的 Nimbus 绘画组件(聚焦环)

发布于 2025-01-05 23:49:11 字数 222 浏览 0 评论 0原文

我正在创建自定义 Swing 组件,并希望提供一个看起来像 Nimbus 的 UI

我知道如何访问 UIDefaults 颜色,但是我可以重用更多代码吗?特别是,有没有办法获得绘制聚焦环(矩形或椭圆形,或者甚至更好,沿着任何轮廓 Shape),就像其他 Nimbus 组件一样? (无需重新发明轮子)

I am creating custom Swing components and want to provide a UI that looks just like Nimbus.

I know how to access the UIDefaults colors, but can I reuse more code, particularly, is there a way to get an object that paints the focus rings (rectangular or oval, or even better, along any outline Shape) exactly like the other Nimbus components do? (without reinventing the wheel)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单挑你×的.吻 2025-01-12 23:49:11

由于我在 Nimbus 中没有看到任何允许为新组件重用画家的设计决策,而且我还被困在 OS X 上,Java 6 和 Nimbus 是一个移动(命名空间)目标,这是我的解决方案,它基本上重新创建Synth 风格使用什么:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class NimbusFocusBorder implements javax.swing.border.Border {
    private static final RoundRectangle2D rect = new RoundRectangle2D.Float();
    private static final Area area = new Area();

    private final float arcIn;
    private final float arcOut;

    public NimbusFocusBorder() {
        arcIn = arcOut = 0f;
    }

    public NimbusFocusBorder( float rounded ) {
        arcIn  = rounded * 2;
        arcOut = arcIn + 2.8f;
    }

    public void paintBorder( Component c, Graphics _g, int x, int y, int w, int h ) {
        if( !c.hasFocus() ) return;
        final Graphics2D g = (Graphics2D) _g;
        g.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON );
        g.setColor( /* NimbusHelper. */ getFocusColor() );
        rect.setRoundRect( x + 0.6, y + 0.6, w - 1.2, h - 1.2, arcOut, arcOut );
        area.reset();
        area.add( new Area( rect ));
        rect.setRoundRect( x + 2, y + 2, w - 4, h - 4, arcIn, arcIn );
        area.subtract( new Area( rect ));
        g.fill( area );

    }

    public Insets getBorderInsets( Component c ) {
        return new Insets( 2, 2, 2, 2 );
    }

    public boolean isBorderOpaque() { return false; }

    // this actually looks up the color in UIDefaults
    // in the real implementation
    private Color getFocusColor() { return new Color( 115, 164, 209, 255 );}
}

As I didn't see any design decision in Nimbus to allow the reuse of painters for new components, and also I'm stuck on OS X with Java 6 and Nimbus being a moving (namespace) target, here is my solution which basically recreates what the Synth style is using:

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;

public class NimbusFocusBorder implements javax.swing.border.Border {
    private static final RoundRectangle2D rect = new RoundRectangle2D.Float();
    private static final Area area = new Area();

    private final float arcIn;
    private final float arcOut;

    public NimbusFocusBorder() {
        arcIn = arcOut = 0f;
    }

    public NimbusFocusBorder( float rounded ) {
        arcIn  = rounded * 2;
        arcOut = arcIn + 2.8f;
    }

    public void paintBorder( Component c, Graphics _g, int x, int y, int w, int h ) {
        if( !c.hasFocus() ) return;
        final Graphics2D g = (Graphics2D) _g;
        g.setRenderingHint( RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON );
        g.setColor( /* NimbusHelper. */ getFocusColor() );
        rect.setRoundRect( x + 0.6, y + 0.6, w - 1.2, h - 1.2, arcOut, arcOut );
        area.reset();
        area.add( new Area( rect ));
        rect.setRoundRect( x + 2, y + 2, w - 4, h - 4, arcIn, arcIn );
        area.subtract( new Area( rect ));
        g.fill( area );

    }

    public Insets getBorderInsets( Component c ) {
        return new Insets( 2, 2, 2, 2 );
    }

    public boolean isBorderOpaque() { return false; }

    // this actually looks up the color in UIDefaults
    // in the real implementation
    private Color getFocusColor() { return new Color( 115, 164, 209, 255 );}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文