Example usage for java.awt.geom AffineTransform getTranslateInstance

List of usage examples for java.awt.geom AffineTransform getTranslateInstance

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform getTranslateInstance.

Prototype

public static AffineTransform getTranslateInstance(double tx, double ty) 

Source Link

Document

Returns a transform representing a translation transformation.

Usage

From source file:nl.ctmm.trait.proteomics.qcviewer.utils.Utilities.java

/**
 * Scale the supplied image to the specified width and height. The scale type is either {@link Utilities#SCALE_FIT}
 * to make the scaled image fit within the width by height rectangle or {@link Utilities#SCALE_FILL} to make the
 * scaled image fill the entire rectangle (and possibly go outside it in one dimension).
 *
 * @param image     the image to be scaled.
 * @param scaleType {@link Utilities#SCALE_FIT} or {@link Utilities#SCALE_FILL}.
 * @param width     the preferred width.
 * @param height    the preferred height.
 * @return the scaled image.//from   ww  w.  j  a v a 2  s. c o  m
 */
public static BufferedImage scaleImage(final BufferedImage image, final int scaleType, final int width,
        final int height) {
    logger.fine("scaleImage: width: " + width + " height: " + height);

    /* TODO: can we do the scaling once and save the images of the right size? [Freek]
     * This is a good idea. [Pravin]  
     * 
     * TODO: are there classes in the standard Java libraries or third party libraries that do this scaling? [Freek]
     * return image.getScaledInstance(width, height, Image.SCALE_SMOOTH);
     * This article describes use of Greaphics2D.drawImage() [Pravin]
     * http://www.mkyong.com/java/how-to-resize-an-image-in-java/
     * imgscalr is Java image scaling library available under Apache 2 License. 
     * http://www.thebuzzmedia.com/software/imgscalr-java-image-scaling-library/
     */
    final BufferedImage scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    final Graphics2D graphics2D = scaledImage.createGraphics();
    graphics2D.setColor(Color.white);
    graphics2D.fillRect(0, 0, width, height);
    final double imageWidth = image.getWidth();
    final double imageHeight = image.getHeight();
    final double xScale = width / imageWidth;
    final double yScale = height / imageHeight;
    double scale = 1.0;
    switch (scaleType) {
    case SCALE_FIT:
        scale = Math.min(xScale, yScale);
        break;
    case SCALE_FILL:
        scale = Math.max(xScale, yScale);
        break;
    default:
        logger.warning(String.format("Unexpected scale type: %d.", scaleType));
        break;
    }
    final double x = (width - imageWidth * scale) / 2;
    final double y = (height - imageHeight * scale) / 2;
    final AffineTransform affineTransform = AffineTransform.getTranslateInstance(x, y);
    affineTransform.scale(scale, scale);
    graphics2D.drawRenderedImage(image, affineTransform);
    graphics2D.dispose();
    return scaledImage;
}

From source file:org.apache.fontbox.cff.Type1CharString.java

/**
 * Standard Encoding Accented Character/*w ww  .j a v  a2s .  com*/
 *
 * Makes an accented character from two other characters.
 * @param asb 
 */
private void seac(Number asb, Number adx, Number ady, Number bchar, Number achar) {
    // base character
    String baseName = StandardEncoding.INSTANCE.getName(bchar.intValue());
    if (baseName != null) {
        try {
            Type1CharString base = font.getType1CharString(baseName);
            path.append(base.getPath().getPathIterator(null), false);
        } catch (IOException e) {
            LOG.warn("invalid seac character in glyph " + glyphName + " of font " + fontName);
        }
    }
    // accent character
    String accentName = StandardEncoding.INSTANCE.getName(achar.intValue());
    if (accentName != null) {
        try {
            Type1CharString accent = font.getType1CharString(accentName);
            AffineTransform at = AffineTransform.getTranslateInstance(leftSideBearing.getX() + adx.floatValue(),
                    leftSideBearing.getY() + ady.floatValue());
            path.append(accent.getPath().getPathIterator(at), false);
        } catch (IOException e) {
            LOG.warn("invalid seac character in glyph " + glyphName + " of font " + fontName);
        }
    }
}

From source file:org.apache.pdfbox.rendering.PageDrawer.java

@Override
public void showTransparencyGroup(PDFormXObject form) throws IOException {
    TransparencyGroup group = new TransparencyGroup(form, false);

    graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
    setClip();/*from w  ww .  jav a2s .  co m*/

    // both the DPI xform and the CTM were already applied to the group, so all we do
    // here is draw it directly onto the Graphics2D device at the appropriate position
    PDRectangle bbox = group.getBBox();
    AffineTransform prev = graphics.getTransform();
    float x = bbox.getLowerLeftX();
    float y = pageSize.getHeight() - bbox.getLowerLeftY() - bbox.getHeight();
    graphics.setTransform(AffineTransform.getTranslateInstance(x * xform.getScaleX(), y * xform.getScaleY()));

    PDSoftMask softMask = getGraphicsState().getSoftMask();
    if (softMask != null) {
        BufferedImage image = group.getImage();
        Paint awtPaint = new TexturePaint(image,
                new Rectangle2D.Float(0, 0, image.getWidth(), image.getHeight()));
        awtPaint = applySoftMaskToPaint(awtPaint, softMask); // todo: PDFBOX-994 problem here?
        graphics.setPaint(awtPaint);
        graphics.fill(new Rectangle2D.Float(0, 0, bbox.getWidth() * (float) xform.getScaleX(),
                bbox.getHeight() * (float) xform.getScaleY()));
    } else {
        graphics.drawImage(group.getImage(), null, null);
    }

    graphics.setTransform(prev);
}

From source file:org.dwfa.ace.graph.AceGraphRenderer.java

/**
 * Draws the edge <code>e</code>, whose endpoints are at
 * <code>(x1,y1)</code> and <code>(x2,y2)</code>, on the graphics context
 * <code>g</code>./*w ww  .  ja v a 2  s .  c  o m*/
 * The <code>Shape</code> provided by the <code>EdgeShapeFunction</code>
 * instance
 * is scaled in the x-direction so that its width is equal to the distance
 * between <code>(x1,y1)</code> and <code>(x2,y2)</code>.
 */
protected void drawSimpleEdge(Graphics2D g, Edge e, int x1, int y1, int x2, int y2) {
    Pair endpoints = e.getEndpoints();
    Vertex v1 = (Vertex) endpoints.getFirst();
    Vertex v2 = (Vertex) endpoints.getSecond();
    boolean isLoop = v1.equals(v2);
    Shape s2 = vertexShapeFunction.getShape(v2);
    Shape edgeShape = edgeShapeFunction.getShape(e);

    boolean edgeHit = true;
    boolean arrowHit = true;
    Rectangle deviceRectangle = null;
    if (screenDevice != null) {
        Dimension d = screenDevice.getSize();
        if (d.width <= 0 || d.height <= 0) {
            d = screenDevice.getPreferredSize();
        }
        deviceRectangle = new Rectangle(0, 0, d.width, d.height);
    }

    AffineTransform xform = AffineTransform.getTranslateInstance(x1, y1);

    if (isLoop) {
        // this is a self-loop. scale it is larger than the vertex
        // it decorates and translate it so that its nadir is
        // at the center of the vertex.
        Rectangle2D s2Bounds = s2.getBounds2D();
        xform.scale(s2Bounds.getWidth(), s2Bounds.getHeight());
        xform.translate(0, -edgeShape.getBounds2D().getWidth() / 2);
    } else {
        // this is a normal edge. Rotate it to the angle between
        // vertex endpoints, then scale it to the distance between
        // the vertices
        float dx = x2 - x1;
        float dy = y2 - y1;
        float thetaRadians = (float) Math.atan2(dy, dx);
        xform.rotate(thetaRadians);
        float dist = (float) Math.sqrt(dx * dx + dy * dy);
        xform.scale(dist, 1.0);
    }

    edgeShape = xform.createTransformedShape(edgeShape);

    if (deviceRectangle == null) {
        edgeHit = false;
    } else {
        edgeHit = viewTransformer.transform(edgeShape).intersects(deviceRectangle);
    }

    if (edgeHit == true) {

        Paint oldPaint = g.getPaint();

        // get Paints for filling and drawing
        // (filling is done first so that drawing and label use same Paint)
        Paint fill_paint = edgePaintFunction.getFillPaint(e);
        if (fill_paint != null) {
            g.setPaint(fill_paint);
            g.fill(edgeShape);
        }
        Paint draw_paint = edgePaintFunction.getDrawPaint(e);
        if (draw_paint != null) {
            g.setPaint(draw_paint);
            g.draw(edgeShape);
        }

        float scalex = (float) g.getTransform().getScaleX();
        float scaley = (float) g.getTransform().getScaleY();
        // see if arrows are too small to bother drawing
        if (scalex < .3 || scaley < .3)
            return;

        if (edgeArrowPredicate.evaluate(e)) {

            Shape destVertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getSecond());
            AffineTransform xf = AffineTransform.getTranslateInstance(x2, y2);
            destVertexShape = xf.createTransformedShape(destVertexShape);

            arrowHit = viewTransformer.transform(destVertexShape).intersects(deviceRectangle);
            if (arrowHit) {

                AffineTransform at;
                if (edgeShape instanceof GeneralPath)
                    at = getArrowTransform((GeneralPath) edgeShape, destVertexShape);
                else
                    at = getArrowTransform(new GeneralPath(edgeShape), destVertexShape);
                if (at == null)
                    return;
                Shape arrow = edgeArrowFunction.getArrow(e);
                arrow = at.createTransformedShape(arrow);
                // note that arrows implicitly use the edge's draw paint
                g.fill(arrow);
            }
            if (e instanceof UndirectedEdge) {
                Shape vertexShape = vertexShapeFunction.getShape((Vertex) e.getEndpoints().getFirst());
                xf = AffineTransform.getTranslateInstance(x1, y1);
                vertexShape = xf.createTransformedShape(vertexShape);

                arrowHit = viewTransformer.transform(vertexShape).intersects(deviceRectangle);

                if (arrowHit) {
                    AffineTransform at;
                    if (edgeShape instanceof GeneralPath)
                        at = getReverseArrowTransform((GeneralPath) edgeShape, vertexShape, !isLoop);
                    else
                        at = getReverseArrowTransform(new GeneralPath(edgeShape), vertexShape, !isLoop);
                    if (at == null)
                        return;
                    Shape arrow = edgeArrowFunction.getArrow(e);
                    arrow = at.createTransformedShape(arrow);
                    g.fill(arrow);
                }
            }
        }
        // use existing paint for text if no draw paint specified
        if (draw_paint == null)
            g.setPaint(oldPaint);
        String label = edgeStringer.getLabel(e);
        if (label != null) {
            labelEdge(g, e, label, x1, x2, y1, y2);
        }

        // restore old paint
        g.setPaint(oldPaint);
    }
}

From source file:org.dwfa.ace.graph.AceGraphRenderer.java

/**
 * Paints the vertex <code>v</code> at the location <code>(x,y)</code> on
 * the graphics context <code>g_gen</code>. The vertex is painted
 * using the shape returned by this instance's
 * <code>VertexShapeFunction</code>,
 * and the foreground and background (border) colors provided by this
 * instance's <code>VertexColorFunction</code>. Delegates drawing the
 * label (if any) for this vertex to <code>labelVertex</code>.
 *//*from  w w  w .j  a  va  2s . c  o  m*/
public void paintVertex(Graphics g, Vertex v, int x, int y) {
    if (!vertexIncludePredicate.evaluate(v))
        return;

    boolean vertexHit = true;
    Rectangle deviceRectangle = null;
    Graphics2D g2d = (Graphics2D) g;
    if (screenDevice != null) {
        Dimension d = screenDevice.getSize();
        if (d.width <= 0 || d.height <= 0) {
            d = screenDevice.getPreferredSize();
        }
        deviceRectangle = new Rectangle(0, 0, d.width, d.height);
    }

    Stroke old_stroke = g2d.getStroke();
    Stroke new_stroke = vertexStrokeFunction.getStroke(v);
    if (new_stroke != null) {
        g2d.setStroke(new_stroke);
    }
    // get the shape to be rendered
    Shape s = vertexShapeFunction.getShape(v);

    // create a transform that translates to the location of
    // the vertex to be rendered
    AffineTransform xform = AffineTransform.getTranslateInstance(x, y);
    // transform the vertex shape with xtransform
    s = xform.createTransformedShape(s);

    if (deviceRectangle == null) {
        vertexHit = false;
    } else {
        vertexHit = viewTransformer.transform(s).intersects(deviceRectangle);
    }

    if (vertexHit) {

        if (vertexShapeRendered) {
            if (vertexIconFunction != null) {
                paintIconForVertex(g2d, v, x, y);
            } else {
                paintShapeForVertex(g2d, v, s);
            }
        }

        if (new_stroke != null) {
            g2d.setStroke(old_stroke);
        }
        String label = vertexStringer.getLabel(v);
        if (label != null) {
            labelVertex(g, v, label, x, y);
        }
    }
}

From source file:org.esa.snap.graphbuilder.gpf.ui.worldmap.NestWorldMapPane.java

public static GeneralPath areaToPath(final Area negativeArea, final double deltaX,
        final GeneralPath pixelPath) {

    final float[] floats = new float[6];
    // move to correct rectangle
    final AffineTransform transform = AffineTransform.getTranslateInstance(deltaX, 0.0);
    final PathIterator iterator = negativeArea.getPathIterator(transform);

    while (!iterator.isDone()) {
        final int segmentType = iterator.currentSegment(floats);
        if (segmentType == PathIterator.SEG_LINETO) {
            pixelPath.lineTo(floats[0], floats[1]);
        } else if (segmentType == PathIterator.SEG_MOVETO) {
            pixelPath.moveTo(floats[0], floats[1]);
        } else if (segmentType == PathIterator.SEG_CLOSE) {
            pixelPath.closePath();/*from w  w w. ja  va2  s  .c o  m*/
        }
        iterator.next();
    }
    return pixelPath;
}

From source file:org.geotools.coverage.io.util.Utilities.java

/**
 * Retrieves the original grid to world transformation for this {@link AbstractGridCoverage2DReader}.
 * /*from w w  w . j a va 2  s.com*/
 * @param pixInCell specifies the datum of the transformation we want.
 * @return the original grid to world transformation
 */
public static MathTransform getOriginalGridToWorld(MathTransform raster2Model, final PixelInCell pixInCell) {
    // we do not have to change the pixel datum
    if (pixInCell == PixelInCell.CELL_CENTER)
        return raster2Model;

    // we do have to change the pixel datum
    if (raster2Model instanceof AffineTransform) {
        final AffineTransform tr = new AffineTransform((AffineTransform) raster2Model);
        tr.concatenate(AffineTransform.getTranslateInstance(-0.5, -0.5));
        return ProjectiveTransform.create(tr);
    }
    if (raster2Model instanceof IdentityTransform) {
        final AffineTransform tr = new AffineTransform(1, 0, 0, 1, 0, 0);
        tr.concatenate(AffineTransform.getTranslateInstance(-0.5, -0.5));
        return ProjectiveTransform.create(tr);
    }
    throw new IllegalStateException("This grid to world transform is invalud!");
}

From source file:org.jcurl.core.base.CurveTransformedTest.java

public void testAffineTransformMemoryLayout() {
    Rock r = new RockDouble(1, 2, 3);
    AffineTransform at = new AffineTransform();
    final double[] d = new double[6];
    at.getMatrix(d);/*from  ww w .ja va2  s. c  o m*/
    assertEquals("", 1.0, d[0], 1e-9);
    assertEquals("", 0.0, d[1], 1e-9);
    assertEquals("", 0.0, d[2], 1e-9);
    assertEquals("", 1.0, d[3], 1e-9);
    assertEquals("", 0.0, d[4], 1e-9);
    assertEquals("", 0.0, d[5], 1e-9);
    at.transform(r = new RockDouble(2, 3, 4), r);
    assertEquals(2, 3, 4, r, 1e-9);

    at = AffineTransform.getScaleInstance(0.5, 0.75);
    at.getMatrix(d);
    assertEquals("", 0.5, d[0], 1e-9);
    assertEquals("", 0.0, d[1], 1e-9);
    assertEquals("", 0.0, d[2], 1e-9);
    assertEquals("", 0.75, d[3], 1e-9);
    assertEquals("", 0.0, d[4], 1e-9);
    assertEquals("", 0.0, d[5], 1e-9);
    at.transform(r = new RockDouble(2, 3, 4), r);
    assertEquals(1, 2.25, 4, r, 1e-9);

    at = AffineTransform.getTranslateInstance(0.5, 0.75);
    at.getMatrix(d);
    assertEquals("", 1.0, d[0], 1e-9);
    assertEquals("", 0.0, d[1], 1e-9);
    assertEquals("", 0.0, d[2], 1e-9);
    assertEquals("", 1.0, d[3], 1e-9);
    assertEquals("", 0.5, d[4], 1e-9);
    assertEquals("", 0.75, d[5], 1e-9);
    at.transform(r = new RockDouble(2, 3, 4), r);
    assertEquals(2.5, 3.75, 4, r, 1e-9);
}

From source file:org.jcurl.core.base.CurveTransformedTest.java

/**
 * Test the transformation from a Rock Coordinates (rc) System at wc(3,3.5)
 * with positive y axis along wc(2,4.2) into World Coordinates (wc). Uses a
 * Point rc(5,1.3) = wc(8,2.5)./*from w w  w.j  ava  2  s . co m*/
 */
public void testAffineTransformRotateShift() {
    final Point2D p0_wc = new Point2D.Double(3, 3.5);
    final Rock v0_wc = new RockDouble(2, 4.2, 0.3);
    final double v = v0_wc.distance(0, 0);
    final double[] d = { v0_wc.getY(), -v0_wc.getX(), v0_wc.getX(), v0_wc.getY(), 0, 0 };
    final AffineTransform at = new AffineTransform(d);
    at.scale(1 / v, 1 / v);
    assertEquals(AffineTransform.TYPE_GENERAL_ROTATION + AffineTransform.TYPE_UNIFORM_SCALE, at.getType());
    assertEquals(1.0, at.getDeterminant());
    assertEquals(0.9028605188239303, at.getScaleX());
    assertEquals(at.getScaleX(), at.getScaleY());
    assertEquals(0.42993358039234775, at.getShearX());
    assertEquals(-at.getShearX(), at.getShearY());
    assertEquals(0, at.getTranslateX());
    assertEquals(0, at.getTranslateY());
    Point2D p = null;
    p = at.transform(new Point2D.Double(5, 1.3), null);
    assertEquals("Point2D.Double[5.073216248629703, -0.9759492274906292]", p.toString());

    at.preConcatenate(AffineTransform.getTranslateInstance(p0_wc.getX(), p0_wc.getY()));
    assertEquals(AffineTransform.TYPE_GENERAL_ROTATION + AffineTransform.TYPE_TRANSLATION
            + AffineTransform.TYPE_UNIFORM_SCALE, at.getType());
    assertEquals(1.0, at.getDeterminant());
    assertEquals(0.9028605188239303, at.getScaleX());
    assertEquals(at.getScaleX(), at.getScaleY());
    assertEquals(0.42993358039234775, at.getShearX());
    assertEquals(-at.getShearX(), at.getShearY());
    assertEquals(p0_wc.getX(), at.getTranslateX());
    assertEquals(p0_wc.getY(), at.getTranslateY());

    p = at.transform(new Point2D.Double(5, 1.3), null);
    assertEquals("Point2D.Double[8.073216248629702, 2.524050772509371]", p.toString());
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @see Graphics#drawImage(Image, int, int, int, int, Color, ImageObserver)
 *//*from   ww  w  . j  a v a  2s. c o m*/
@Override
public boolean drawImage(final Image img, final int x, final int y, final int width, final int height,
        final Color bgcolor, final ImageObserver observer) {
    waitForImage(img);
    final double scalex = width / (double) img.getWidth(observer);
    final double scaley = height / (double) img.getHeight(observer);
    final AffineTransform tx = AffineTransform.getTranslateInstance(x, y);
    tx.scale(scalex, scaley);
    return drawImage(img, null, tx, bgcolor, observer);
}