我正在创建自定义 Swing 组件,并希望提供一个UI
看起来像 Nimbus 的组件。
我知道如何访问UIDefaults
颜色,但是我可以重用更多代码Shape
吗? (无需重新发明轮子)
由于我在 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 );}
}