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

The following examples show how to use java.awt.geom.AffineTransform#getTranslateY() . 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 File: HiDPIUtils.java    From FlatLaf with Apache License 2.0 6 votes vote down vote up
/**
 * Scales a rectangle in the same way as the JRE does in
 * sun.java2d.pipe.PixelToParallelogramConverter.fillRectangle(),
 * which is used by Graphics.fillRect().
 */
private static Rectangle2D.Double scale( AffineTransform transform, int x, int y, int width, int height ) {
	double dx1 = transform.getScaleX();
	double dy2 = transform.getScaleY();
	double px = x * dx1 + transform.getTranslateX();
	double py = y * dy2 + transform.getTranslateY();
	dx1 *= width;
	dy2 *= height;

	double newx = normalize( px );
	double newy = normalize( py );
	dx1 = normalize( px + dx1 ) - newx;
	dy2 = normalize( py + dy2 ) - newy;

	return new Rectangle2D.Double( newx, newy, dx1, dy2 );
}
 
Example 2
Source File: AttributeValues.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static AffineTransform extractRotation(Point2D.Double pt,
    AffineTransform tx, boolean andTranslation) {

    tx.deltaTransform(pt, pt);
    AffineTransform rtx = AffineTransform.getRotateInstance(pt.x, pt.y);

    try {
        AffineTransform rtxi = rtx.createInverse();
        double dx = tx.getTranslateX();
        double dy = tx.getTranslateY();
        tx.preConcatenate(rtxi);
        if (andTranslation) {
            if (dx != 0 || dy != 0) {
                tx.setTransform(tx.getScaleX(), tx.getShearY(),
                                tx.getShearX(), tx.getScaleY(), 0, 0);
                rtx.setTransform(rtx.getScaleX(), rtx.getShearY(),
                                 rtx.getShearX(), rtx.getScaleY(), dx, dy);
            }
        }
    }
    catch (NoninvertibleTransformException e) {
        return null;
    }
    return rtx;
}
 
Example 3
Source File: Font2D.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public FontStrike getStrike(Font font, FontRenderContext frc) {

        AffineTransform at = frc.getTransform();
        double ptSize = font.getSize2D();
        at.scale(ptSize, ptSize);
        if (font.isTransformed()) {
            at.concatenate(font.getTransform());
            if (at.getTranslateX() != 0 || at.getTranslateY() != 0) {
                at.setTransform(at.getScaleX(),
                                at.getShearY(),
                                at.getShearX(),
                                at.getScaleY(),
                                0.0, 0.0);
            }
        }
        int aa = FontStrikeDesc.getAAHintIntVal(this, font, frc);
        int fm = FontStrikeDesc.getFMHintIntVal(frc.getFractionalMetricsHint());
        FontStrikeDesc desc = new FontStrikeDesc(frc.getTransform(),
                                                 at, font.getStyle(),
                                                 aa, fm);
        return getStrike(desc, false);
    }
 
Example 4
Source File: Font2D.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public FontStrike getStrike(Font font, FontRenderContext frc) {

        AffineTransform at = frc.getTransform();
        double ptSize = font.getSize2D();
        at.scale(ptSize, ptSize);
        if (font.isTransformed()) {
            at.concatenate(font.getTransform());
            if (at.getTranslateX() != 0 || at.getTranslateY() != 0) {
                at.setTransform(at.getScaleX(),
                                at.getShearY(),
                                at.getShearX(),
                                at.getScaleY(),
                                0.0, 0.0);
            }
        }
        int aa = FontStrikeDesc.getAAHintIntVal(this, font, frc);
        int fm = FontStrikeDesc.getFMHintIntVal(frc.getFractionalMetricsHint());
        FontStrikeDesc desc = new FontStrikeDesc(frc.getTransform(),
                                                 at, font.getStyle(),
                                                 aa, fm);
        return getStrike(desc, false);
    }
 
Example 5
Source File: AttributeValues.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static AffineTransform extractRotation(Point2D.Double pt,
    AffineTransform tx, boolean andTranslation) {

    tx.deltaTransform(pt, pt);
    AffineTransform rtx = AffineTransform.getRotateInstance(pt.x, pt.y);

    try {
        AffineTransform rtxi = rtx.createInverse();
        double dx = tx.getTranslateX();
        double dy = tx.getTranslateY();
        tx.preConcatenate(rtxi);
        if (andTranslation) {
            if (dx != 0 || dy != 0) {
                tx.setTransform(tx.getScaleX(), tx.getShearY(),
                                tx.getShearX(), tx.getScaleY(), 0, 0);
                rtx.setTransform(rtx.getScaleX(), rtx.getShearY(),
                                 rtx.getShearX(), rtx.getScaleY(), dx, dy);
            }
        }
    }
    catch (NoninvertibleTransformException e) {
        return null;
    }
    return rtx;
}
 
Example 6
Source File: AttributeValues.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static AffineTransform extractRotation(Point2D.Double pt,
    AffineTransform tx, boolean andTranslation) {

    tx.deltaTransform(pt, pt);
    AffineTransform rtx = AffineTransform.getRotateInstance(pt.x, pt.y);

    try {
        AffineTransform rtxi = rtx.createInverse();
        double dx = tx.getTranslateX();
        double dy = tx.getTranslateY();
        tx.preConcatenate(rtxi);
        if (andTranslation) {
            if (dx != 0 || dy != 0) {
                tx.setTransform(tx.getScaleX(), tx.getShearY(),
                                tx.getShearX(), tx.getScaleY(), 0, 0);
                rtx.setTransform(rtx.getScaleX(), rtx.getShearY(),
                                 rtx.getShearX(), rtx.getScaleY(), dx, dy);
            }
        }
    }
    catch (NoninvertibleTransformException e) {
        return null;
    }
    return rtx;
}
 
Example 7
Source File: TransformingPathConsumer2D.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
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 8
Source File: PixelToParallelogramConverter.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: TexturePaintContext.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 10
Source File: TransformingPathConsumer2D.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
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 11
Source File: TexturePaintContext.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 12
Source File: SunGraphics2D.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given AffineTransform is an integer
 * translation.
 */
private static boolean isIntegerTranslation(AffineTransform xform) {
    if (xform.isIdentity()) {
        return true;
    }
    if (xform.getType() == AffineTransform.TYPE_TRANSLATION) {
        double tx = xform.getTranslateX();
        double ty = xform.getTranslateY();
        return (tx == (int)tx && ty == (int)ty);
    }
    return false;
}
 
Example 13
Source File: SunGraphics2D.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Returns true if the given AffineTransform is an integer
 * translation.
 */
private static boolean isIntegerTranslation(AffineTransform xform) {
    if (xform.isIdentity()) {
        return true;
    }
    if (xform.getType() == AffineTransform.TYPE_TRANSLATION) {
        double tx = xform.getTranslateX();
        double ty = xform.getTranslateY();
        return (tx == (int)tx && ty == (int)ty);
    }
    return false;
}
 
Example 14
Source File: PaintersHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
int[] computeOffsets(Graphics gg, @Nonnull JComponent component) {
  if (myPainters.isEmpty()) return ArrayUtil.EMPTY_INT_ARRAY;
  int i = 0;
  int[] offsets = new int[2 + myPainters.size() * 2];
  // store current graphics transform
  Graphics2D g = (Graphics2D)gg;
  AffineTransform tx = g.getTransform();
  // graphics tx offsets include graphics scale
  offsets[i++] = (int)tx.getTranslateX();
  offsets[i++] = (int)tx.getTranslateY();
  // calculate relative offsets for painters
  Rectangle r = null;
  Component prev = null;
  for (Painter painter : myPainters) {
    if (!painter.needsRepaint()) continue;

    Component cur = myPainter2Component.get(painter);
    if (cur != prev || r == null) {
      Container curParent = cur.getParent();
      if (curParent == null) continue;
      r = SwingUtilities.convertRectangle(curParent, cur.getBounds(), component);
      prev = cur;
    }
    // component offsets don't include graphics scale, so compensate
    offsets[i++] = (int)(r.x * tx.getScaleX());
    offsets[i++] = (int)(r.y * tx.getScaleY());
  }
  return offsets;
}
 
Example 15
Source File: Font2D.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public FontStrike getStrike(Font font, AffineTransform devTx,
                            int aa, int fm) {

    /* Create the descriptor which is used to identify a strike
     * in the strike cache/map. A strike is fully described by
     * the attributes of this descriptor.
     */
    /* REMIND: generating garbage and doing computation here in order
     * to include pt size in the tx just for a lookup! Figure out a
     * better way.
     */
    double ptSize = font.getSize2D();
    AffineTransform glyphTx = (AffineTransform)devTx.clone();
    glyphTx.scale(ptSize, ptSize);
    if (font.isTransformed()) {
        glyphTx.concatenate(font.getTransform());
    }
    if (glyphTx.getTranslateX() != 0 || glyphTx.getTranslateY() != 0) {
        glyphTx.setTransform(glyphTx.getScaleX(),
                             glyphTx.getShearY(),
                             glyphTx.getShearX(),
                             glyphTx.getScaleY(),
                             0.0, 0.0);
    }
    FontStrikeDesc desc = new FontStrikeDesc(devTx, glyphTx,
                                             font.getStyle(), aa, fm);
    return getStrike(desc, false);
}
 
Example 16
Source File: TexturePaintContext.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
TexturePaintContext(ColorModel cm, AffineTransform xform,
                    int bWidth, int bHeight, int maxw) {
    this.colorModel = getInternedColorModel(cm);
    this.bWidth = bWidth;
    this.bHeight = bHeight;
    this.maxWidth = maxw;

    try {
        xform = xform.createInverse();
    } catch (NoninvertibleTransformException e) {
        xform.setToScale(0, 0);
    }
    this.incXAcross = mod(xform.getScaleX(), bWidth);
    this.incYAcross = mod(xform.getShearY(), bHeight);
    this.incXDown = mod(xform.getShearX(), bWidth);
    this.incYDown = mod(xform.getScaleY(), bHeight);
    this.xOrg = xform.getTranslateX();
    this.yOrg = xform.getTranslateY();
    this.colincx = (int) incXAcross;
    this.colincy = (int) incYAcross;
    this.colincxerr = fractAsInt(incXAcross);
    this.colincyerr = fractAsInt(incYAcross);
    this.rowincx = (int) incXDown;
    this.rowincy = (int) incYDown;
    this.rowincxerr = fractAsInt(incXDown);
    this.rowincyerr = fractAsInt(incYDown);

}
 
Example 17
Source File: BufferedPaints.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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 18
Source File: RepaintManager.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private void paintDoubleBufferedFPScales(JComponent c, Image image,
                                         Graphics g, int clipX, int clipY,
                                         int clipW, int clipH) {
    Graphics osg = image.getGraphics();
    Graphics2D g2d = (Graphics2D) g;
    Graphics2D osg2d = (Graphics2D) osg;

    AffineTransform identity = new AffineTransform();
    int bw = Math.min(clipW, image.getWidth(null));
    int bh = Math.min(clipH, image.getHeight(null));
    int x, y, maxx, maxy;

    AffineTransform tx = g2d.getTransform();
    double scaleX = tx.getScaleX();
    double scaleY = tx.getScaleY();
    double trX = tx.getTranslateX();
    double trY = tx.getTranslateY();

    boolean translucent = volatileBufferType != Transparency.OPAQUE;
    Composite oldComposite = g2d.getComposite();

    try {
        for (x = clipX, maxx = clipX + clipW; x < maxx; x += bw) {
            for (y = clipY, maxy = clipY + clipH; y < maxy; y += bh) {

                // draw x, y, bw, bh
                int pixelx1 = Region.clipRound(x * scaleX + trX);
                int pixely1 = Region.clipRound(y * scaleY + trY);
                int pixelx2 = Region.clipRound((x + bw) * scaleX + trX);
                int pixely2 = Region.clipRound((y + bh) * scaleY + trY);
                int pixelw = pixelx2 - pixelx1;
                int pixelh = pixely2 - pixely1;

                osg2d.setTransform(identity);
                if (translucent) {
                    final Color oldBg = g2d.getBackground();
                    g2d.setBackground(c.getBackground());
                    g2d.clearRect(pixelx1, pixely1, pixelw, pixelh);
                    g2d.setBackground(oldBg);
                }

                osg2d.setClip(0, 0, pixelw, pixelh);
                osg2d.translate(trX - pixelx1, trY - pixely1);
                osg2d.scale(scaleX, scaleY);
                c.paintToOffscreen(osg, x, y, bw, bh, maxx, maxy);

                g2d.setTransform(identity);
                g2d.setClip(pixelx1, pixely1, pixelw, pixelh);
                AffineTransform stx = new AffineTransform();
                stx.translate(pixelx1, pixely1);
                stx.scale(scaleX, scaleY);
                g2d.setTransform(stx);

                if (translucent) {
                    g2d.setComposite(AlphaComposite.Src);
                }

                g2d.drawImage(image, 0, 0, c);

                if (translucent) {
                    g2d.setComposite(oldComposite);
                }
                g2d.setTransform(tx);
            }
        }
    } finally {
        osg.dispose();
    }
}
 
Example 19
Source File: ImageCanvas.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * SYNC the scroll-bars with the image.
 */
public void syncScrollBars( )
{
	if ( sourceImage == null )
	{
		redraw( );
		return;
	}

	AffineTransform af = transform;
	double sx = af.getScaleX( ), sy = af.getScaleY( );
	double tx = af.getTranslateX( ), ty = af.getTranslateY( );
	if ( tx > 0 )
		tx = 0;
	if ( ty > 0 )
		ty = 0;

	Rectangle imageBound = sourceImage.getBounds( );
	int cw = getClientArea( ).width, ch = getClientArea( ).height;

	ScrollBar horizontal = getHorizontalBar( );

	if ( horizontal != null )
	{
		horizontal.setIncrement( (int) ( getClientArea( ).width / 100 ) );
		horizontal.setPageIncrement( getClientArea( ).width );

		if ( imageBound.width * sx > cw )
		{
			horizontal.setMaximum( (int) ( imageBound.width * sx ) );
			horizontal.setEnabled( true );
			if ( ( (int) -tx ) > horizontal.getMaximum( ) - cw )
				tx = -horizontal.getMaximum( ) + cw;
		}
		else
		{
			horizontal.setEnabled( false );
			tx = ( cw - imageBound.width * sx ) / 2;
		}
		horizontal.setSelection( (int) ( -tx ) );
		horizontal.setThumb( (int) ( getClientArea( ).width ) );
	}
	ScrollBar vertical = getVerticalBar( );
	if ( vertical != null )
	{
		vertical.setIncrement( (int) ( getClientArea( ).height / 100 ) );
		vertical.setPageIncrement( (int) ( getClientArea( ).height ) );
		if ( imageBound.height * sy > ch )
		{
			vertical.setMaximum( (int) ( imageBound.height * sy ) );
			vertical.setEnabled( true );
			if ( ( (int) -ty ) > vertical.getMaximum( ) - ch )
				ty = -vertical.getMaximum( ) + ch;
		}
		else
		{
			vertical.setEnabled( false );
			ty = ( ch - imageBound.height * sy ) / 2;
		}
		vertical.setSelection( (int) ( -ty ) );
		vertical.setThumb( (int) ( getClientArea( ).height ) );
	}

	af = AffineTransform.getScaleInstance( sx, sy );
	af.preConcatenate( AffineTransform.getTranslateInstance( tx, ty ) );
	transform = af;

	redraw( );
}
 
Example 20
Source File: BufferedPaints.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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);
}