CSS-like effects brought to Java shape painting.
Helps to decouple shapes and their styles allowing developer to concentrate on positioning rather than drawing.
Use org.ehony.awt.ShapePainter
to access fluent api for drawing shapes.
AWT Painter provides several classes which implement CSS-like effects.
Exemples below use fllowing objects:
ShapePainter shapePainter = new ShapePainter();
Shape shape = new Rectangle(20, 20, 60, 60);
This section describes bundled painters which provide same effects as CSS box-shadow
property.
org.ehony.awt.painter.InsetShadowPainter
is analogue box-shadow
property with inset
keyword specified.
Default shadow settings match ones defined in CSS specification. Inset shadow with all parameters set to zero paints nothing.
Example | Description |
---|---|
Blur Radius Positive blur radius indicates that the resulting shadow should be blurred. If the blur value is zero or negative, the edge of the shadow is sharp. Shadow is blurred with parallelized implemetation of Gaussian blur. decorator.background(0xfffcaf3e).insetShadow(0, 0, 15, 0, 0xcc000000); |
|
Offset Horizontal and vertical offsets can be specified separately. A positive value draws a shadow that is offset to the right (bottom) of the box, a negative length to the left (top). decorator.background(0xfffcaf3e).insetShadow(0, -5, 10, 0, 0xaa000000); |
|
Spread Positive values cause the shadow to expand in all directions by the specified value. Negative values cause the shadow to contract. decorator.background(0xfffcaf3e).insetShadow(0, 0, 15, -15, 0xcc000000); |
|
Paint Shadow may be painted with an arbitrary java.awt.Paint . If the paint was not specified then paint returned by Graphics2D.getPaint() is used.To create a gradient inset shadow create a custom GradientPaint instance:gp = new GradientPaint(0, 0, new Color(0xfcaf3e), 60, 60, new Color(0x4e9a06)); And use it to paint an inset shadow: decorator.background(Color.WHITE).insetShadow(0, 0, 15, 0, gp); |
org.ehony.awt.painter.DropShadowPainter
is analogue box-shadow
property without inset
keyword specified.
This painter inherits all the parameters from inner shadow and introduces:
Example | Description |
---|---|
Blur Radius Positive blur radius indicates that the resulting shadow should be blurred. If the blur value is zero or negative, the edge of the shadow is sharp. Shadow is blurred with parallelized implemetation of Gaussian blur. decorator.background(0xfffcaf3e).dropShadow(0, 0, 10, 0, 0xcc000000, false) |
|
Offset Horizontal and vertical offsets can be specified separately. A positive value draws a shadow that is offset to the right (bottom) of the box, a negative length to the left (top). decorator.background(0xfffcaf3e).dropShadow(5, 5, 10, 0, 0x99000000, false); |
|
Spread Positive values cause the shadow to expand in all directions by the specified value. Negative values cause the shadow to contract. decorator.background(0xfffcaf3e).dropShadow(6, 12, 6, -12, 0xcc000000, false); |
|
Paint Shadow may be painted with an arbitrary java.awt.Paint . If the paint was not specified then paint returned by Graphics2D.getPaint() is used.To create a gradient inset shadow create a custom GradientPaint instance:gp = new GradientPaint(0, 0, new Color(0xfcaf3e), 60, 60, new Color(0x4e9a06)); And use it to paint an inset shadow: decorator.background(Color.WHITE).dropShadow(-2, -2, 15, 4, gp, false); |
|
Exclude Original Shape If set to true omits painting shadow pixels which overlap with original shape.decorator.dropShadow(0, 0, 10, 0, 0xee000000, true) |
org.ehony.awt.painter.BackgroundPainter
is analogue of background
property.
org.ehony.awt.painter.OutlinePainter
is analogue of outline
property.
Default outline settings match ones defined in CSS specification. Outline painter does not have any required parameters but may be configured with:
Width The width of the outline. The width must be greater than or equal to zero. If width is set to zero, outline is not painted.
Cap If shape being drawn is an open path instance this property defined the decoration of the ends of an outline.
Line Join Defines the decoration applied where path segments meet.
Miter Limit The limit to trim the miter join, must be greater than or equal to 1.
Dash Pattern Pattern of empty and painted areas of an outline.
Initial Dash Phase The offset to start the dashing pattern.
Subsequent Dash If set to false
then getSubsequentShape(Shape)
method would return shape ignoring the dash pattern. This may be useful if multiple outline painters are used allowing to prohibit consequent outlines to bend around each other dashes.
org.ehony.awt.painter.CompositePainter
description.
The code is available under MIT licence.