Skip to content
This repository has been archived by the owner on Jun 14, 2021. It is now read-only.

smikhalevski/awt-painter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWT Painter

CSS-like effects brought to Java shape painting.

Helps to decouple shapes and their styles allowing developer to concentrate on positioning rather than drawing.

Contents

  1. Quick Start
  2. Shadows
    1. Inner Shadow
    2. Drop Shadow
  3. Background
  4. Outlines
  5. Painter Ordering
  6. License

Quick Start

Use org.ehony.awt.ShapePainter to access fluent api for drawing shapes.

Bundled Painters

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);

Shadows

This section describes bundled painters which provide same effects as CSS box-shadow property.

Inset Shadow

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);

Drop Shadow

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)

Background

org.ehony.awt.painter.BackgroundPainter is analogue of background property.

Outlines

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.

Painter Ordering

org.ehony.awt.painter.CompositePainter description.

License

The code is available under MIT licence.