下面列出了java.awt.geom.Area#exclusiveOr ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns inner shadow image based on provided shape.
*
* @param bounds shadow bounds
* @param width shadow width
* @param opacity shadow opacity
* @param color shadow color
* @param shape shadow shape
* @return inner shadow image based on provided shape
*/
@NotNull
public static BufferedImage createInnerShadowImage ( @NotNull final Rectangle bounds, final int width, final float opacity,
@NotNull final Color color, @NotNull final Shape shape )
{
final Rectangle b = new Rectangle ( bounds.x - width * 2, bounds.y - width * 2,
bounds.width + width * 4, bounds.height + width * 4 );
// Creating template image
final BufferedImage bi = ImageUtils.createCompatibleImage ( b.width, b.height, Transparency.TRANSLUCENT );
final Graphics2D ig = bi.createGraphics ();
GraphicsUtils.setupAntialias ( ig );
ig.translate ( -b.x, -b.y );
final Area area = new Area ( new Rectangle ( b.x, b.y, b.width, b.height ) );
area.exclusiveOr ( new Area ( shape ) );
ig.setPaint ( Color.BLACK );
ig.fill ( area );
ig.dispose ();
// Creating inner shadow image
final ShadowFilter sf = new ShadowFilter ( width, 0, 0, opacity );
sf.setShadowColor ( Color.BLACK.getRGB () );
final BufferedImage shadow = sf.filter ( bi, null );
// Clipping inner shadow image
final Graphics2D g2d = shadow.createGraphics ();
GraphicsUtils.setupAntialias ( g2d );
g2d.translate ( -b.x, -b.y );
g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
g2d.setPaint ( ColorUtils.transparent () );
g2d.fill ( area );
g2d.setPaint ( color );
g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
g2d.fillRect ( 0, 0, b.width, b.height );
g2d.dispose ();
return shadow.getSubimage ( width * 2, width * 2, b.width - width * 4, b.height - width * 4 );
}
/**
* Paints complex component area.
*
* @param g2d graphics context
* @param insets area insets
* @param outer outer area bounds
* @param inner inner area bounds
* @param color area color
*/
private void paintComplexArea ( @NotNull final Graphics2D g2d, @NotNull final Insets insets, @NotNull final Rectangle outer,
@NotNull final Rectangle inner, @NotNull final Color color )
{
if ( !insets.equals ( emptyInsets ) )
{
g2d.setPaint ( color );
final Area ma = new Area ( outer );
ma.exclusiveOr ( new Area ( inner ) );
g2d.fill ( ma );
}
}
/**
* Returns shadow image based on provided shape.
*
* @param width shadow image width
* @param shape shadow shape
* @param shadowWidth shadow width
* @param opacity shadow opacity
* @return shadow image based on provided shape
*/
@NotNull
public static BufferedImage createInnerShadowImage ( final int width, @NotNull final Shape shape, final int shadowWidth,
final float opacity )
{
// Creating template image
final BufferedImage bi = createCompatibleImage ( width, width, Transparency.TRANSLUCENT );
final Graphics2D ig = bi.createGraphics ();
GraphicsUtils.setupAntialias ( ig );
final Area area = new Area ( new Rectangle ( 0, 0, width, width ) );
area.exclusiveOr ( new Area ( shape ) );
ig.setPaint ( Color.BLACK );
ig.fill ( area );
ig.dispose ();
// Creating shadow image
final ShadowFilter sf = new ShadowFilter ( shadowWidth, 0, 0, opacity );
final BufferedImage shadow = sf.filter ( bi, null );
// Clipping shadow image
final Graphics2D g2d = shadow.createGraphics ();
GraphicsUtils.setupAntialias ( g2d );
g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
g2d.setPaint ( ColorUtils.transparent () );
g2d.fill ( area );
g2d.dispose ();
return shadow.getSubimage ( shadowWidth, shadowWidth, width - shadowWidth * 2, width - shadowWidth * 2 );
}
/** Converts all points in @param area to ints by casting. */
static public final Area areaInInts(final Area area) {
final Area a = new Area();
for (final Polygon pol : M.getPolygons(area)) {
a.exclusiveOr(new Area(pol));
}
return a;
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
public RectListImpl getExclusiveOr(RectListImpl rli) {
Area a2 = new Area(theArea);
a2.exclusiveOr(((AreaImpl) rli).theArea);
return new AreaImpl(a2);
}
@Override
protected void paintComponent ( final Graphics g )
{
super.paintComponent ( g );
final Graphics2D g2d = ( Graphics2D ) g;
final Object aa = GraphicsUtils.setupAntialias ( g2d );
// final int w = getWidth ();
final int h = getHeight ();
final int iw = h - margin.top - margin.bottom;
// Paint icon
if ( icon != null )
{
// Paint icon border
if ( isPaintBorder () )
{
final Area outer = new Area ( new Ellipse2D.Double ( 0, 0, h, h ) );
final Ellipse2D.Double inner = new Ellipse2D.Double ( 2, 2, h - 4, h - 4 );
outer.exclusiveOr ( new Area ( inner ) );
g2d.setPaint ( isEnabled () ? getBorderColor () : getDisabledBorderColor () );
g2d.fill ( outer );
g2d.setPaint ( isEnabled () ? getBorderBackground () : getDisabledBorderBackground () );
g2d.fill ( inner );
}
// Paint icon
g2d.drawImage ( icon.getImage (), margin.left + iw / 2 - icon.getIconWidth () / 2,
margin.top + iw / 2 - icon.getIconHeight () / 2, null );
}
// Paint text
if ( text != null )
{
g2d.setFont ( getFont () );
final FontMetrics fm = g2d.getFontMetrics ();
final int tw = fm.stringWidth ( text );
final int th = fm.getHeight ();
final int ts = LafUtils.getTextCenterShiftY ( fm );
// Calculating full text rectangle
final int tx = margin.left + iw + iconTextGap;
final int ty = margin.top + iw / 2 - th / 2 - textMargin.top;
final Rectangle tr = new Rectangle ( tx, ty, textMargin.left + tw + textMargin.right, textMargin.top + th + textMargin.bottom );
// Paint text border
g2d.setPaint ( rollover ? getRolloverBackground () : getBackground () );
g2d.fillRoundRect ( tr.x, tr.y, tr.width, tr.height, /*tr.height, tr.height*/ 6, 6 );
final int ath = tr.height - textMargin.top - textMargin.bottom;
g2d.setPaint ( getForeground () );
g2d.drawString ( text, tr.x + textMargin.left, tr.y + textMargin.top + ath / 2 + ts );
}
GraphicsUtils.restoreAntialias ( g2d, aa );
}
/**
* Computes the area covered by the first or the second but not both given
* areas.
*
* @param pa
* the first area to compute the xor for
* @param pb
* the second area to compute the xor for
* @return the exclusive-or of the areas
*/
public static Path exclusiveOr(Path pa, Path pb) {
Area a = new Area(Geometry2AWT.toAWTPath(pa));
Area b = new Area(Geometry2AWT.toAWTPath(pb));
a.exclusiveOr(b);
return AWT2Geometry.toPath(new Path2D.Double(a));
}