Java Code Examples for java.awt.geom.AffineTransform#getShearX()

The following examples show how to use java.awt.geom.AffineTransform#getShearX() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: TrakEM2   File: Patch.java    License: GNU General Public License v3.0 6 votes vote down vote up
public double getLocalScale( final double wx, final double wy )
{
	final AffineTransform affine = getLocalAffine( wx, wy );
	final double a = affine.getScaleX();
	final double b = affine.getShearX();
	final double c = affine.getShearY();
	final double d = affine.getScaleY();

	final double l1x = a + b;
	final double l1y = c + d;
	final double l2x = a - b;
	final double l2y = c - d;

	final double l1 = Math.sqrt( l1x * l1x + l1y * l1y ) / SQRT2;
	final double l2 = Math.sqrt( l2x * l2x + l2y * l2y ) / SQRT2;

	return ( l1 + l2 ) / 2.0;
}
 
Example 2
public static PathConsumer2D
    deltaTransformConsumer(PathConsumer2D out,
                           AffineTransform at)
{
    if (at == null) {
        return out;
    }
    float Mxx = (float) at.getScaleX();
    float Mxy = (float) at.getShearX();
    float Myx = (float) at.getShearY();
    float Myy = (float) at.getScaleY();
    if (Mxy == 0f && Myx == 0f) {
        if (Mxx == 1f && Myy == 1f) {
            return out;
        } else {
            return new DeltaScaleFilter(out, Mxx, Myy);
        }
    } else {
        return new DeltaTransformFilter(out, Mxx, Mxy, Myx, Myy);
    }
}
 
Example 3
public static PathConsumer2D
    inverseDeltaTransformConsumer(PathConsumer2D out,
                                  AffineTransform at)
{
    if (at == null) {
        return out;
    }
    float Mxx = (float) at.getScaleX();
    float Mxy = (float) at.getShearX();
    float Myx = (float) at.getShearY();
    float Myy = (float) at.getScaleY();
    if (Mxy == 0f && Myx == 0f) {
        if (Mxx == 1f && Myy == 1f) {
            return out;
        } else {
            return new DeltaScaleFilter(out, 1.0f/Mxx, 1.0f/Myy);
        }
    } else {
        float det = Mxx * Myy - Mxy * Myx;
        return new DeltaTransformFilter(out,
                                        Myy / det,
                                        -Mxy / det,
                                        -Myx / det,
                                        Mxx / det);
    }
}
 
Example 4
PathConsumer2D inverseDeltaTransformConsumer(PathConsumer2D out,
                                             AffineTransform at)
{
    if (at == null) {
        return out;
    }
    float mxx = (float) at.getScaleX();
    float mxy = (float) at.getShearX();
    float myx = (float) at.getShearY();
    float myy = (float) at.getScaleY();

    if (mxy == 0.0f && myx == 0.0f) {
        if (mxx == 1.0f && myy == 1.0f) {
            return out;
        } else {
            return iv_DeltaScaleFilter.init(out, 1.0f / mxx, 1.0f / myy);
        }
    } else {
        final float det = mxx * myy - mxy * myx;
        return iv_DeltaTransformFilter.init(out,
                                            myy / det,
                                           -mxy / det,
                                           -myx / det,
                                            mxx / det);
    }
}
 
Example 5
public static PathConsumer2D
    deltaTransformConsumer(PathConsumer2D out,
                           AffineTransform at)
{
    if (at == null) {
        return out;
    }
    float Mxx = (float) at.getScaleX();
    float Mxy = (float) at.getShearX();
    float Myx = (float) at.getShearY();
    float Myy = (float) at.getScaleY();
    if (Mxy == 0f && Myx == 0f) {
        if (Mxx == 1f && Myy == 1f) {
            return out;
        } else {
            return new DeltaScaleFilter(out, Mxx, Myy);
        }
    } else {
        return new DeltaTransformFilter(out, Mxx, Mxy, Myx, Myy);
    }
}
 
Example 6
public static PathConsumer2D
    inverseDeltaTransformConsumer(PathConsumer2D out,
                                  AffineTransform at)
{
    if (at == null) {
        return out;
    }
    float Mxx = (float) at.getScaleX();
    float Mxy = (float) at.getShearX();
    float Myx = (float) at.getShearY();
    float Myy = (float) at.getScaleY();
    if (Mxy == 0f && Myx == 0f) {
        if (Mxx == 1f && Myy == 1f) {
            return out;
        } else {
            return new DeltaScaleFilter(out, 1.0f/Mxx, 1.0f/Myy);
        }
    } else {
        float det = Mxx * Myy - Mxy * Myx;
        return new DeltaTransformFilter(out,
                                        Myy / det,
                                        -Mxy / det,
                                        -Myx / det,
                                        Mxx / det);
    }
}
 
Example 7
PathConsumer2D inverseDeltaTransformConsumer(PathConsumer2D out,
                                             AffineTransform at)
{
    if (at == null) {
        return out;
    }
    float mxx = (float) at.getScaleX();
    float mxy = (float) at.getShearX();
    float myx = (float) at.getShearY();
    float myy = (float) at.getScaleY();

    if (mxy == 0.0f && myx == 0.0f) {
        if (mxx == 1.0f && myy == 1.0f) {
            return out;
        } else {
            return iv_DeltaScaleFilter.init(out, 1.0f / mxx, 1.0f / myy);
        }
    } else {
        final float det = mxx * myy - mxy * myx;
        return iv_DeltaTransformFilter.init(out,
                                            myy / det,
                                           -mxy / det,
                                           -myx / det,
                                            mxx / det);
    }
}
 
Example 8
public void fillRectangle(SunGraphics2D sg2d,
                          double rx, double ry,
                          double rw, double rh)
{
    double px, py;
    double dx1, dy1, dx2, dy2;
    AffineTransform txform = sg2d.transform;
    dx1 = txform.getScaleX();
    dy1 = txform.getShearY();
    dx2 = txform.getShearX();
    dy2 = txform.getScaleY();
    px = rx * dx1 + ry * dx2 + txform.getTranslateX();
    py = rx * dy1 + ry * dy2 + txform.getTranslateY();
    dx1 *= rw;
    dy1 *= rw;
    dx2 *= rh;
    dy2 *= rh;
    if (adjustfill &&
        sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM &&
        sg2d.strokeHint != SunHints.INTVAL_STROKE_PURE)
    {
        double newx = normalize(px);
        double newy = normalize(py);
        dx1 = normalize(px + dx1) - newx;
        dy1 = normalize(py + dy1) - newy;
        dx2 = normalize(px + dx2) - newx;
        dy2 = normalize(py + dy2) - newy;
        px = newx;
        py = newy;
    }
    outrenderer.fillParallelogram(sg2d, rx, ry, rx+rw, ry+rh,
                                  px, py, dx1, dy1, dx2, dy2);
}
 
Example 9
DPathConsumer2D deltaTransformConsumer(DPathConsumer2D out,
                                      AffineTransform at)
{
    if (at == null) {
        return out;
    }
    final double mxx = at.getScaleX();
    final double mxy = at.getShearX();
    final double myx = at.getShearY();
    final double myy = at.getScaleY();

    if (mxy == 0.0d && myx == 0.0d) {
        if (mxx == 1.0d && myy == 1.0d) {
            return out;
        } else {
            // Scale only
            if (rdrCtx.doClip) {
                // adjust clip rectangle (ymin, ymax, xmin, xmax):
                rdrCtx.clipInvScale = adjustClipScale(rdrCtx.clipRect,
                    mxx, myy);
            }
            return dt_DeltaScaleFilter.init(out, mxx, myy);
        }
    } else {
        if (rdrCtx.doClip) {
            // adjust clip rectangle (ymin, ymax, xmin, xmax):
            rdrCtx.clipInvScale = adjustClipInverseDelta(rdrCtx.clipRect,
                mxx, mxy, myx, myy);
        }
        return dt_DeltaTransformFilter.init(out, mxx, mxy, myx, myy);
    }
}
 
Example 10
private static boolean matchTX(double[] lhs, AffineTransform rhs) {
    return
        lhs[0] == rhs.getScaleX() &&
        lhs[1] == rhs.getShearY() &&
        lhs[2] == rhs.getShearX() &&
        lhs[3] == rhs.getScaleY();
}
 
Example 11
Source Project: gcs   File: Matrix.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Creates a matrix with the same elements as the given AffineTransform.
 * @param at
 */
public Matrix(AffineTransform at)
{
    single = new float[DEFAULT_SINGLE.length];
    System.arraycopy(DEFAULT_SINGLE, 0, single, 0, DEFAULT_SINGLE.length);
    single[0] = (float)at.getScaleX();
    single[1] = (float)at.getShearY();
    single[3] = (float)at.getShearX();
    single[4] = (float)at.getScaleY();
    single[6] = (float)at.getTranslateX();
    single[7] = (float)at.getTranslateY();
}
 
Example 12
public static PathConsumer2D
    transformConsumer(PathConsumer2D out,
                      AffineTransform at)
{
    if (at == null) {
        return out;
    }
    float Mxx = (float) at.getScaleX();
    float Mxy = (float) at.getShearX();
    float Mxt = (float) at.getTranslateX();
    float Myx = (float) at.getShearY();
    float Myy = (float) at.getScaleY();
    float Myt = (float) at.getTranslateY();
    if (Mxy == 0f && Myx == 0f) {
        if (Mxx == 1f && Myy == 1f) {
            if (Mxt == 0f && Myt == 0f) {
                return out;
            } else {
                return new TranslateFilter(out, Mxt, Myt);
            }
        } else {
            if (Mxt == 0f && Myt == 0f) {
                return new DeltaScaleFilter(out, Mxx, Myy);
            } else {
                return new ScaleFilter(out, Mxx, Myy, Mxt, Myt);
            }
        }
    } else if (Mxt == 0f && Myt == 0f) {
        return new DeltaTransformFilter(out, Mxx, Mxy, Myx, Myy);
    } else {
        return new TransformFilter(out, Mxx, Mxy, Mxt, Myx, Myy, Myt);
    }
}
 
Example 13
public void drawRectangle(SunGraphics2D sg2d,
                          double rx, double ry,
                          double rw, double rh,
                          double lw)
{
    double px, py;
    double dx1, dy1, dx2, dy2;
    double lw1, lw2;
    AffineTransform txform = sg2d.transform;
    dx1 = txform.getScaleX();
    dy1 = txform.getShearY();
    dx2 = txform.getShearX();
    dy2 = txform.getScaleY();
    px = rx * dx1 + ry * dx2 + txform.getTranslateX();
    py = rx * dy1 + ry * dy2 + txform.getTranslateY();
    // lw along dx1,dy1 scale by transformed length of dx2,dy2 vectors
    // and vice versa
    lw1 = len(dx1, dy1) * lw;
    lw2 = len(dx2, dy2) * lw;
    dx1 *= rw;
    dy1 *= rw;
    dx2 *= rh;
    dy2 *= rh;
    if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM &&
        sg2d.strokeHint != SunHints.INTVAL_STROKE_PURE)
    {
        double newx = normalize(px);
        double newy = normalize(py);
        dx1 = normalize(px + dx1) - newx;
        dy1 = normalize(py + dy1) - newy;
        dx2 = normalize(px + dx2) - newx;
        dy2 = normalize(py + dy2) - newy;
        px = newx;
        py = newy;
    }
    lw1 = Math.max(lw1, minPenSize);
    lw2 = Math.max(lw2, minPenSize);
    double len1 = len(dx1, dy1);
    double len2 = len(dx2, dy2);
    if (lw1 >= len1 || lw2 >= len2) {
        // The line widths are large enough to consume the
        // entire hole in the middle of the parallelogram
        // so we can just fill the outer parallelogram.
        fillOuterParallelogram(sg2d,
                               rx, ry, rx+rw, ry+rh,
                               px, py, dx1, dy1, dx2, dy2,
                               len1, len2, lw1, lw2);
    } else {
        outrenderer.drawParallelogram(sg2d,
                                      rx, ry, rx+rw, ry+rh,
                                      px, py, dx1, dy1, dx2, dy2,
                                      lw1 / len1, lw2 / len2);
    }
}
 
Example 14
/**
 * We use OpenGL's texture coordinate generator to automatically
 * map the TexturePaint image to the geometry being rendered.  The
 * generator uses two separate plane equations that take the (x,y)
 * location (in device space) of the fragment being rendered to
 * calculate (u,v) texture coordinates for that fragment:
 *     u = Ax + By + Cz + Dw
 *     v = Ex + Fy + Gz + Hw
 *
 * Since we use a 2D orthographic projection, we can assume that z=0
 * and w=1 for any fragment.  So we need to calculate appropriate
 * values for the plane equation constants (A,B,D) and (E,F,H) such
 * that {u,v}=0 for the top-left of the TexturePaint's anchor
 * rectangle and {u,v}=1 for the bottom-right of the anchor rectangle.
 * We can easily make the texture image repeat for {u,v} values
 * outside the range [0,1] by specifying the GL_REPEAT texture wrap
 * mode.
 *
 * Calculating the plane equation constants is surprisingly simple.
 * We can think of it as an inverse matrix operation that takes
 * device space coordinates and transforms them into user space
 * coordinates that correspond to a location relative to the anchor
 * rectangle.  First, we translate and scale the current user space
 * transform by applying the anchor rectangle bounds.  We then take
 * the inverse of this affine transform.  The rows of the resulting
 * inverse matrix correlate nicely to the plane equation constants
 * we were seeking.
 */
private static void setTexturePaint(RenderQueue rq,
                                    SunGraphics2D sg2d,
                                    TexturePaint paint,
                                    boolean useMask)
{
    BufferedImage bi = paint.getImage();
    SurfaceData dstData = sg2d.surfaceData;
    SurfaceData srcData =
        dstData.getSourceSurfaceData(bi, SunGraphics2D.TRANSFORM_ISIDENT,
                                     CompositeType.SrcOver, null);
    boolean filter =
        (sg2d.interpolationType !=
         AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

    // calculate plane equation constants
    AffineTransform at = (AffineTransform)sg2d.transform.clone();
    Rectangle2D anchor = paint.getAnchorRect();
    at.translate(anchor.getX(), anchor.getY());
    at.scale(anchor.getWidth(), anchor.getHeight());

    double xp0, xp1, xp3, yp0, yp1, yp3;
    try {
        at.invert();
        xp0 = at.getScaleX();
        xp1 = at.getShearX();
        xp3 = at.getTranslateX();
        yp0 = at.getShearY();
        yp1 = at.getScaleY();
        yp3 = at.getTranslateY();
    } catch (java.awt.geom.NoninvertibleTransformException e) {
        xp0 = xp1 = xp3 = yp0 = yp1 = yp3 = 0.0;
    }

    // assert rq.lock.isHeldByCurrentThread();
    rq.ensureCapacityAndAlignment(68, 12);
    RenderBuffer buf = rq.getBuffer();
    buf.putInt(SET_TEXTURE_PAINT);
    buf.putInt(useMask ? 1 : 0);
    buf.putInt(filter ? 1 : 0);
    buf.putLong(srcData.getNativeOps());
    buf.putDouble(xp0).putDouble(xp1).putDouble(xp3);
    buf.putDouble(yp0).putDouble(yp1).putDouble(yp3);
}
 
Example 15
private float userSpaceLineWidth(AffineTransform at, float lw) {

        float widthScale;

        if (at == null) {
            widthScale = 1.0f;
        } else if ((at.getType() & (AffineTransform.TYPE_GENERAL_TRANSFORM  |
                                    AffineTransform.TYPE_GENERAL_SCALE)) != 0) {
            widthScale = (float)Math.sqrt(at.getDeterminant());
        } else {
            // First calculate the "maximum scale" of this transform.
            double A = at.getScaleX();       // m00
            double C = at.getShearX();       // m01
            double B = at.getShearY();       // m10
            double D = at.getScaleY();       // m11

            /*
             * Given a 2 x 2 affine matrix [ A B ] such that
             *                             [ C D ]
             * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
             * find the maximum magnitude (norm) of the vector v'
             * with the constraint (x^2 + y^2 = 1).
             * The equation to maximize is
             *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
             * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
             * Since sqrt is monotonic we can maximize |v'|^2
             * instead and plug in the substitution y = sqrt(1 - x^2).
             * Trigonometric equalities can then be used to get
             * rid of most of the sqrt terms.
             */

            double EA = A*A + B*B;          // x^2 coefficient
            double EB = 2.0d * (A*C + B*D); // xy coefficient
            double EC = C*C + D*D;          // y^2 coefficient

            /*
             * There is a lot of calculus omitted here.
             *
             * Conceptually, in the interests of understanding the
             * terms that the calculus produced we can consider
             * that EA and EC end up providing the lengths along
             * the major axes and the hypot term ends up being an
             * adjustment for the additional length along the off-axis
             * angle of rotated or sheared ellipses as well as an
             * adjustment for the fact that the equation below
             * averages the two major axis lengths.  (Notice that
             * the hypot term contains a part which resolves to the
             * difference of these two axis lengths in the absence
             * of rotation.)
             *
             * In the calculus, the ratio of the EB and (EA-EC) terms
             * ends up being the tangent of 2*theta where theta is
             * the angle that the long axis of the ellipse makes
             * with the horizontal axis.  Thus, this equation is
             * calculating the length of the hypotenuse of a triangle
             * along that axis.
             */

            double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));
            // sqrt omitted, compare to squared limits below.
            double widthsquared = ((EA + EC + hypot) / 2.0d);

            widthScale = (float)Math.sqrt(widthsquared);
        }

        return (lw / widthScale);
    }
 
Example 16
/**
 * This method uses techniques that are nearly identical to those
 * employed in setGradientPaint() above.  The primary difference
 * is that at the native level we use a fragment shader to manually
 * apply the plane equation constants to the current fragment position
 * to calculate the gradient position in the range [0,1] (the native
 * code for GradientPaint does the same, except that it uses OpenGL's
 * automatic texture coordinate generation facilities).
 *
 * One other minor difference worth mentioning is that
 * setGradientPaint() calculates the plane equation constants
 * such that the gradient end points are positioned at 0.25 and 0.75
 * (for reasons discussed in the comments for that method).  In
 * contrast, for LinearGradientPaint we setup the equation constants
 * such that the gradient end points fall at 0.0 and 1.0.  The
 * reason for this difference is that in the fragment shader we
 * have more control over how the gradient values are interpreted
 * (depending on the paint's CycleMethod).
 */
private static void setLinearGradientPaint(RenderQueue rq,
                                           SunGraphics2D sg2d,
                                           LinearGradientPaint paint,
                                           boolean useMask)
{
    boolean linear =
        (paint.getColorSpace() == ColorSpaceType.LINEAR_RGB);
    Color[] colors = paint.getColors();
    int numStops = colors.length;
    Point2D pt1 = paint.getStartPoint();
    Point2D pt2 = paint.getEndPoint();
    AffineTransform at = paint.getTransform();
    at.preConcatenate(sg2d.transform);

    if (!linear && numStops == 2 &&
        paint.getCycleMethod() != CycleMethod.REPEAT)
    {
        // delegate to the optimized two-color gradient codepath
        boolean isCyclic =
            (paint.getCycleMethod() != CycleMethod.NO_CYCLE);
        setGradientPaint(rq, at,
                         colors[0], colors[1],
                         pt1, pt2,
                         isCyclic, useMask);
        return;
    }

    int cycleMethod = paint.getCycleMethod().ordinal();
    float[] fractions = paint.getFractions();
    int[] pixels = convertToIntArgbPrePixels(colors, linear);

    // calculate plane equation constants
    double x = pt1.getX();
    double y = pt1.getY();
    at.translate(x, y);
    // now gradient point 1 is at the origin
    x = pt2.getX() - x;
    y = pt2.getY() - y;
    double len = Math.sqrt(x * x + y * y);
    at.rotate(x, y);
    // now gradient point 2 is on the positive x-axis
    at.scale(len, 1);
    // now gradient point 1 is at (0.0, 0), point 2 is at (1.0, 0)

    float p0, p1, p3;
    try {
        at.invert();
        p0 = (float)at.getScaleX();
        p1 = (float)at.getShearX();
        p3 = (float)at.getTranslateX();
    } catch (java.awt.geom.NoninvertibleTransformException e) {
        p0 = p1 = p3 = 0.0f;
    }

    // assert rq.lock.isHeldByCurrentThread();
    rq.ensureCapacity(20 + 12 + (numStops*4*2));
    RenderBuffer buf = rq.getBuffer();
    buf.putInt(SET_LINEAR_GRADIENT_PAINT);
    buf.putInt(useMask ? 1 : 0);
    buf.putInt(linear  ? 1 : 0);
    buf.putInt(cycleMethod);
    buf.putInt(numStops);
    buf.putFloat(p0);
    buf.putFloat(p1);
    buf.putFloat(p3);
    buf.put(fractions);
    buf.put(pixels);
}
 
Example 17
public void drawRectangle(SunGraphics2D sg2d,
                          double rx, double ry,
                          double rw, double rh,
                          double lw)
{
    double px, py;
    double dx1, dy1, dx2, dy2;
    double lw1, lw2;
    AffineTransform txform = sg2d.transform;
    dx1 = txform.getScaleX();
    dy1 = txform.getShearY();
    dx2 = txform.getShearX();
    dy2 = txform.getScaleY();
    px = rx * dx1 + ry * dx2 + txform.getTranslateX();
    py = rx * dy1 + ry * dy2 + txform.getTranslateY();
    // lw along dx1,dy1 scale by transformed length of dx2,dy2 vectors
    // and vice versa
    lw1 = len(dx1, dy1) * lw;
    lw2 = len(dx2, dy2) * lw;
    dx1 *= rw;
    dy1 *= rw;
    dx2 *= rh;
    dy2 *= rh;
    if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM &&
        sg2d.strokeHint != SunHints.INTVAL_STROKE_PURE)
    {
        double newx = normalize(px);
        double newy = normalize(py);
        dx1 = normalize(px + dx1) - newx;
        dy1 = normalize(py + dy1) - newy;
        dx2 = normalize(px + dx2) - newx;
        dy2 = normalize(py + dy2) - newy;
        px = newx;
        py = newy;
    }
    lw1 = Math.max(lw1, minPenSize);
    lw2 = Math.max(lw2, minPenSize);
    double len1 = len(dx1, dy1);
    double len2 = len(dx2, dy2);
    if (lw1 >= len1 || lw2 >= len2) {
        // The line widths are large enough to consume the
        // entire hole in the middle of the parallelogram
        // so we can just fill the outer parallelogram.
        fillOuterParallelogram(sg2d,
                               rx, ry, rx+rw, ry+rh,
                               px, py, dx1, dy1, dx2, dy2,
                               len1, len2, lw1, lw2);
    } else {
        outrenderer.drawParallelogram(sg2d,
                                      rx, ry, rx+rw, ry+rh,
                                      px, py, dx1, dy1, dx2, dy2,
                                      lw1 / len1, lw2 / len2);
    }
}
 
Example 18
public void drawRectangle(SunGraphics2D sg2d,
                          double rx, double ry,
                          double rw, double rh,
                          double lw)
{
    double px, py;
    double dx1, dy1, dx2, dy2;
    double lw1, lw2;
    AffineTransform txform = sg2d.transform;
    dx1 = txform.getScaleX();
    dy1 = txform.getShearY();
    dx2 = txform.getShearX();
    dy2 = txform.getScaleY();
    px = rx * dx1 + ry * dx2 + txform.getTranslateX();
    py = rx * dy1 + ry * dy2 + txform.getTranslateY();
    // lw along dx1,dy1 scale by transformed length of dx2,dy2 vectors
    // and vice versa
    lw1 = len(dx1, dy1) * lw;
    lw2 = len(dx2, dy2) * lw;
    dx1 *= rw;
    dy1 *= rw;
    dx2 *= rh;
    dy2 *= rh;
    if (sg2d.strokeState < SunGraphics2D.STROKE_CUSTOM &&
        sg2d.strokeHint != SunHints.INTVAL_STROKE_PURE)
    {
        double newx = normalize(px);
        double newy = normalize(py);
        dx1 = normalize(px + dx1) - newx;
        dy1 = normalize(py + dy1) - newy;
        dx2 = normalize(px + dx2) - newx;
        dy2 = normalize(py + dy2) - newy;
        px = newx;
        py = newy;
    }
    lw1 = Math.max(lw1, minPenSize);
    lw2 = Math.max(lw2, minPenSize);
    double len1 = len(dx1, dy1);
    double len2 = len(dx2, dy2);
    if (lw1 >= len1 || lw2 >= len2) {
        // The line widths are large enough to consume the
        // entire hole in the middle of the parallelogram
        // so we can just fill the outer parallelogram.
        fillOuterParallelogram(sg2d,
                               rx, ry, rx+rw, ry+rh,
                               px, py, dx1, dy1, dx2, dy2,
                               len1, len2, lw1, lw2);
    } else {
        outrenderer.drawParallelogram(sg2d,
                                      rx, ry, rx+rw, ry+rh,
                                      px, py, dx1, dy1, dx2, dy2,
                                      lw1 / len1, lw2 / len2);
    }
}
 
Example 19
public static int getAAHintIntVal(Font2D font2D, Font font,
                                  FontRenderContext frc) {
    Object aa = frc.getAntiAliasingHint();
    if (aa == VALUE_TEXT_ANTIALIAS_OFF ||
        aa == VALUE_TEXT_ANTIALIAS_DEFAULT) {
        return INTVAL_TEXT_ANTIALIAS_OFF;
    } else if (aa == VALUE_TEXT_ANTIALIAS_ON) {
        return INTVAL_TEXT_ANTIALIAS_ON;
    } else if (aa == VALUE_TEXT_ANTIALIAS_GASP) {
        /* FRC.isIdentity() would have been useful */
        int ptSize;
        AffineTransform tx = frc.getTransform();
        if (tx.isIdentity() && !font.isTransformed()) {
            ptSize = font.getSize();
        } else {
            /* one or both transforms is not identity */
            float size = font.getSize2D();
            if (tx.isIdentity()) {
                tx = font.getTransform();
                tx.scale(size, size);
            } else {
                tx.scale(size, size);
                if (font.isTransformed()) {
                    tx.concatenate(font.getTransform());
                }
            }
            double shearx = tx.getShearX();
            double scaley = tx.getScaleY();
            if (shearx != 0) {
                scaley = Math.sqrt(shearx * shearx + scaley * scaley);
            }
            ptSize = (int)(Math.abs(scaley)+0.5);
        }
        if (font2D.useAAForPtSize(ptSize)) {
            return INTVAL_TEXT_ANTIALIAS_ON;
        } else {
            return INTVAL_TEXT_ANTIALIAS_OFF;
        }
    } else if (aa == VALUE_TEXT_ANTIALIAS_LCD_HRGB ||
               aa == VALUE_TEXT_ANTIALIAS_LCD_HBGR) {
        return INTVAL_TEXT_ANTIALIAS_LCD_HRGB;
    } else if (aa == VALUE_TEXT_ANTIALIAS_LCD_VRGB ||
               aa == VALUE_TEXT_ANTIALIAS_LCD_VBGR) {
        return INTVAL_TEXT_ANTIALIAS_LCD_VRGB;
    } else {
        return INTVAL_TEXT_ANTIALIAS_OFF;
    }
}
 
Example 20
/**
 * Note: This code is factored out into a separate static method
 * so that it can be shared by both the Gradient and LinearGradient
 * implementations.  LinearGradient uses this code (for the
 * two-color sRGB case only) because it can be much faster than the
 * equivalent implementation that uses fragment shaders.
 *
 * We use OpenGL's texture coordinate generator to automatically
 * apply a smooth gradient (either cyclic or acyclic) to the geometry
 * being rendered.  This technique is almost identical to the one
 * described in the comments for BufferedPaints.setTexturePaint(),
 * except the calculations take place in one dimension instead of two.
 * Instead of an anchor rectangle in the TexturePaint case, we use
 * the vector between the two GradientPaint end points in our
 * calculations.  The generator uses a single plane equation that
 * takes the (x,y) location (in device space) of the fragment being
 * rendered to calculate a (u) texture coordinate for that fragment:
 *     u = Ax + By + Cz + Dw
 *
 * The gradient renderer uses a two-pixel 1D texture where the first
 * pixel contains the first GradientPaint color, and the second pixel
 * contains the second GradientPaint color.  (Note that we use the
 * GL_CLAMP_TO_EDGE wrapping mode for acyclic gradients so that we
 * clamp the colors properly at the extremes.)  The following diagram
 * attempts to show the layout of the texture containing the two
 * GradientPaint colors (C1 and C2):
 *
 *                        +-----------------+
 *                        |   C1   |   C2   |
 *                        |        |        |
 *                        +-----------------+
 *                      u=0  .25  .5   .75  1
 *
 * We calculate our plane equation constants (A,B,D) such that u=0.25
 * corresponds to the first GradientPaint end point in user space and
 * u=0.75 corresponds to the second end point.  This is somewhat
 * non-obvious, but since the gradient colors are generated by
 * interpolating between C1 and C2, we want the pure color at the
 * end points, and we will get the pure color only when u correlates
 * to the center of a texel.  The following chart shows the expected
 * color for some sample values of u (where C' is the color halfway
 * between C1 and C2):
 *
 *       u value      acyclic (GL_CLAMP)      cyclic (GL_REPEAT)
 *       -------      ------------------      ------------------
 *        -0.25              C1                       C2
 *         0.0               C1                       C'
 *         0.25              C1                       C1
 *         0.5               C'                       C'
 *         0.75              C2                       C2
 *         1.0               C2                       C'
 *         1.25              C2                       C1
 *
 * Original inspiration for this technique came from UMD's Agile2D
 * project (GradientManager.java).
 */
private static void setGradientPaint(RenderQueue rq, AffineTransform at,
                                     Color c1, Color c2,
                                     Point2D pt1, Point2D pt2,
                                     boolean isCyclic, boolean useMask)
{
    // convert gradient colors to IntArgbPre format
    PixelConverter pc = PixelConverter.ArgbPre.instance;
    int pixel1 = pc.rgbToPixel(c1.getRGB(), null);
    int pixel2 = pc.rgbToPixel(c2.getRGB(), null);

    // calculate plane equation constants
    double x = pt1.getX();
    double y = pt1.getY();
    at.translate(x, y);
    // now gradient point 1 is at the origin
    x = pt2.getX() - x;
    y = pt2.getY() - y;
    double len = Math.sqrt(x * x + y * y);
    at.rotate(x, y);
    // now gradient point 2 is on the positive x-axis
    at.scale(2*len, 1);
    // now gradient point 2 is at (0.5, 0)
    at.translate(-0.25, 0);
    // now gradient point 1 is at (0.25, 0), point 2 is at (0.75, 0)

    double p0, p1, p3;
    try {
        at.invert();
        p0 = at.getScaleX();
        p1 = at.getShearX();
        p3 = at.getTranslateX();
    } catch (java.awt.geom.NoninvertibleTransformException e) {
        p0 = p1 = p3 = 0.0;
    }

    // assert rq.lock.isHeldByCurrentThread();
    rq.ensureCapacityAndAlignment(44, 12);
    RenderBuffer buf = rq.getBuffer();
    buf.putInt(SET_GRADIENT_PAINT);
    buf.putInt(useMask ? 1 : 0);
    buf.putInt(isCyclic ? 1 : 0);
    buf.putDouble(p0).putDouble(p1).putDouble(p3);
    buf.putInt(pixel1).putInt(pixel2);
}