public GradientPaintContext(ColorModel cm,
Point2D p1, Point2D p2, AffineTransform xform,
Color c1, Color c2, boolean cyclic) {
// First calculate the distance moved in user space when
// we move a single unit along the X & Y axes in device space.
Point2D xvec = new Point2D.Double(1, 0);
Point2D yvec = new Point2D.Double(0, 1);
try {
AffineTransform inverse = xform.createInverse();
inverse.deltaTransform(xvec, xvec);
inverse.deltaTransform(yvec, yvec);
} catch (NoninvertibleTransformException e) {
xvec.setLocation(0, 0);
yvec.setLocation(0, 0);
}
// Now calculate the (square of the) user space distance
// between the anchor points. This value equals:
// (UserVec . UserVec)
double udx = p2.getX() - p1.getX();
double udy = p2.getY() - p1.getY();
double ulenSq = udx * udx + udy * udy;
if (ulenSq <= Double.MIN_VALUE) {
dx = 0;
dy = 0;
} else {
// Now calculate the proportional distance moved along the
// vector from p1 to p2 when we move a unit along X & Y in
// device space.
//
// The length of the projection of the Device Axis Vector is
// its dot product with the Unit User Vector:
// (DevAxisVec . (UserVec / Len(UserVec))
//
// The "proportional" length is that length divided again
// by the length of the User Vector:
// (DevAxisVec . (UserVec / Len(UserVec))) / Len(UserVec)
// which simplifies to:
// ((DevAxisVec . UserVec) / Len(UserVec)) / Len(UserVec)
// which simplifies to:
// (DevAxisVec . UserVec) / LenSquared(UserVec)
dx = (xvec.getX() * udx + xvec.getY() * udy) / ulenSq;
dy = (yvec.getX() * udx + yvec.getY() * udy) / ulenSq;
if (cyclic) {
dx = dx % 1.0;
dy = dy % 1.0;
} else {
// We are acyclic
if (dx < 0) {
// If we are using the acyclic form below, we need
// dx to be non-negative for simplicity of scanning
// across the scan lines for the transition points.
// To ensure that constraint, we negate the dx/dy
// values and swap the points and colors.
Point2D p = p1; p1 = p2; p2 = p;
Color c = c1; c1 = c2; c2 = c;
dx = -dx;
dy = -dy;
}
}
}
Point2D dp1 = xform.transform(p1, null);
this.x1 = dp1.getX();
this.y1 = dp1.getY();
this.cyclic = cyclic;
int rgb1 = c1.getRGB();
int rgb2 = c2.getRGB();
int a1 = (rgb1 >> 24) & 0xff;