Actually the reason I'm writing this up as its own entry is that it's a crucial part of three custom
Strokes included in this blog:
The concept is very simple: given a
Shape object, we often need a way to talk about the length of that shape. This includes being able to talk about that shape as fraction (from 0% to 100%) and in pixel lengths (iterate over 5 pixels at a time).
MeasuredShape is designed to address these needs.
There are a couple of possible pitfalls with this class:
1. In Java, a
Shape can have several subpaths. If we want to discuss a shape in terms of fractions: it makes sense to break up each subpath into its own object. So it is hazardous to use the constructors that simply take a
Shape argument, because they might throw
2. Also, especially when talking about fractions, it doesn't make sense to talk about unclosed shapes. So there is are two subtly different methods in the
getOriginalDistance(). If the path was closed when the
MeasuredShape was created: then these will be equal. If the path was unclosed, then the original distance represent the path that was passed to the constructor, and the current distance represents the length of the closed path.
Here is an applet demonstrating one fun method of this class:
This applet takes a circle and lets you adjust the percent drawn and the offset where the drawing begins. (This is handled in just 1 method in the
The jar (source code included) is available here.