Example usage for java.awt.geom AffineTransform getScaleX

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

Introduction

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

Prototype

public double getScaleX() 

Source Link

Document

Returns the m00 element of the 3x3 affine transformation matrix.

Usage

From source file:Main.java

public void paint(Graphics g) {
    Shape shape = new Rectangle2D.Float(100, 50, 80, 80);

    Graphics2D g2 = (Graphics2D) g;

    AffineTransform at = new AffineTransform();
    at.setToQuadrantRotation(2, 0.5, 0.5);

    System.out.println(at.getScaleX());

    g2.setTransform(at);/*w w w. j a v a 2 s.com*/
    g2.draw(shape);

}

From source file:Matrix.java

/**
 * Set the values of the matrix from the AffineTransform.
 * /*from www  . j  av a 2 s.com*/
 * @param af The transform to get the values from.
 */
public void setFromAffineTransform(AffineTransform af) {
    single[0] = (float) af.getScaleX();
    single[1] = (float) af.getShearY();
    single[3] = (float) af.getShearX();
    single[4] = (float) af.getScaleY();
    single[6] = (float) af.getTranslateX();
    single[7] = (float) af.getTranslateY();
}

From source file:es.ucm.fdi.edd.ui.views.utils.SWTImageCanvas.java

/**
 * Synchronize the scrollbar with the image. If the transform is out of
 * range, it will correct it. This function considers only following factors
 * :<b> transform, image size, client area</b>.
 *//*from   ww  w. java2s. c om*/
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;

    ScrollBar horizontal = getHorizontalBar();
    horizontal.setIncrement((int) (getClientArea().width / 100));
    horizontal.setPageIncrement(getClientArea().width);
    Rectangle imageBound = sourceImage.getBounds();
    int cw = getClientArea().width, ch = getClientArea().height;
    if (imageBound.width * sx > cw) { /* image is wider than client area */
        horizontal.setMaximum((int) (imageBound.width * sx));
        horizontal.setEnabled(true);
        if (((int) -tx) > horizontal.getMaximum() - cw)
            tx = -horizontal.getMaximum() + cw;
    } else { /* image is narrower than client area */
        horizontal.setEnabled(false);
        tx = (cw - imageBound.width * sx) / 2; // center if too small.
    }
    horizontal.setSelection((int) (-tx));
    horizontal.setThumb((int) (getClientArea().width));

    ScrollBar vertical = getVerticalBar();
    vertical.setIncrement((int) (getClientArea().height / 100));
    vertical.setPageIncrement((int) (getClientArea().height));
    if (imageBound.height * sy > ch) { /* image is higher than client area */
        vertical.setMaximum((int) (imageBound.height * sy));
        vertical.setEnabled(true);
        if (((int) -ty) > vertical.getMaximum() - ch)
            ty = -vertical.getMaximum() + ch;
    } else { /* image is less higher than client area */
        vertical.setEnabled(false);
        ty = (ch - imageBound.height * sy) / 2; // center if too small.
    }
    vertical.setSelection((int) (-ty));
    vertical.setThumb((int) (getClientArea().height));

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

    redraw();
}

From source file:edu.umass.cs.iesl.pdf2meta.cli.extract.pdfbox.pagedrawer.BeginInlineImage.java

/**
 * process : BI : begin inline image.//from www . j av a  2  s  .c o m
 * @param operator The operator that is being executed.
 * @param arguments List
 * @throws java.io.IOException If there is an error displaying the inline image.
 */
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    GraphicsAwarePDFStreamEngine drawer = (GraphicsAwarePDFStreamEngine) context;

    PDPage page = drawer.getPage();
    //begin inline image object
    ImageParameters params = operator.getImageParameters();
    PDInlinedImage image = new PDInlinedImage();
    image.setImageParameters(params);
    image.setImageData(operator.getImageData());
    BufferedImage awtImage = image.createImage(context.getColorSpaces());

    if (awtImage == null) {
        log.warn("BeginInlineImage.process(): createImage returned NULL");
        return;
    }
    int imageWidth = awtImage.getWidth();
    int imageHeight = awtImage.getHeight();
    double pageHeight = drawer.getPageSize().getHeight();

    Matrix ctm = drawer.getGraphicsState().getCurrentTransformationMatrix();
    int pageRotation = page.findRotation();

    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix rotationMatrix = new Matrix();
    rotationMatrix.setFromAffineTransform(ctmAT);
    // calculate the inverse rotation angle
    // scaleX = m00 = cos
    // shearX = m01 = -sin
    // tan = sin/cos
    double angle = Math.atan(ctmAT.getShearX() / ctmAT.getScaleX());
    Matrix translationMatrix = null;
    if (pageRotation == 0 || pageRotation == 180) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getXScale()),
                (float) (pageHeight - 2 * ctm.getYPosition() - Math.cos(angle) * ctm.getYScale()));
    } else if (pageRotation == 90 || pageRotation == 270) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getYScale()),
                (float) (pageHeight - 2 * ctm.getYPosition()));
    }
    rotationMatrix = rotationMatrix.multiply(translationMatrix);
    rotationMatrix.setValue(0, 1, (-1) * rotationMatrix.getValue(0, 1));
    rotationMatrix.setValue(1, 0, (-1) * rotationMatrix.getValue(1, 0));
    AffineTransform at = new AffineTransform(rotationMatrix.getValue(0, 0), rotationMatrix.getValue(0, 1),
            rotationMatrix.getValue(1, 0), rotationMatrix.getValue(1, 1), rotationMatrix.getValue(2, 0),
            rotationMatrix.getValue(2, 1));
    drawer.drawImage(awtImage, at);
}

From source file:chiliad.parser.pdf.extractor.vectorgraphics.operator.BeginInlineImage.java

/**
 * process : BI : begin inline image./* ww w. ja  v a  2s  .c  om*/
 *
 * @param operator The operator that is being executed.
 * @param arguments List
 * @throws IOException If there is an error displaying the inline image.
 */
@Override
public void process(PDFOperator operator, List<COSBase> arguments) throws IOException {
    VectorGraphicsExtractor extractor = (VectorGraphicsExtractor) context;
    PDPage page = extractor.getPage();
    //begin inline image object
    ImageParameters params = operator.getImageParameters();
    PDInlinedImage image = new PDInlinedImage();
    image.setImageParameters(params);
    image.setImageData(operator.getImageData());
    if (params.isStencil()) {
        //TODO implement inline image stencil masks 
        LOG.warn("Stencil masks are not implemented, background may be incorrect");
    }
    BufferedImage awtImage = image.createImage(context.getColorSpaces());

    if (awtImage == null) {
        LOG.warn("BeginInlineImage.process(): createImage returned NULL");
        return;
    }
    int imageWidth = awtImage.getWidth();
    int imageHeight = awtImage.getHeight();
    double pageHeight = extractor.getPageSize().getHeight();

    Matrix ctm = extractor.getGraphicsState().getCurrentTransformationMatrix();
    int pageRotation = page.findRotation();

    AffineTransform ctmAT = ctm.createAffineTransform();
    ctmAT.scale(1f / imageWidth, 1f / imageHeight);
    Matrix rotationMatrix = new Matrix();
    rotationMatrix.setFromAffineTransform(ctmAT);
    // calculate the inverse rotation angle
    // scaleX = m00 = cos
    // shearX = m01 = -sin
    // tan = sin/cos
    double angle = Math.atan(ctmAT.getShearX() / ctmAT.getScaleX());
    Matrix translationMatrix = null;
    if (pageRotation == 0 || pageRotation == 180) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getXScale()),
                (float) (pageHeight - 2 * ctm.getYPosition() - Math.cos(angle) * ctm.getYScale()));
    } else if (pageRotation == 90 || pageRotation == 270) {
        translationMatrix = Matrix.getTranslatingInstance((float) (Math.sin(angle) * ctm.getYScale()),
                (float) (pageHeight - 2 * ctm.getYPosition()));
    }
    rotationMatrix = rotationMatrix.multiply(translationMatrix);
    rotationMatrix.setValue(0, 1, (-1) * rotationMatrix.getValue(0, 1));
    rotationMatrix.setValue(1, 0, (-1) * rotationMatrix.getValue(1, 0));
    AffineTransform at = new AffineTransform(rotationMatrix.getValue(0, 0), rotationMatrix.getValue(0, 1),
            rotationMatrix.getValue(1, 0), rotationMatrix.getValue(1, 1), rotationMatrix.getValue(2, 0),
            rotationMatrix.getValue(2, 1));
    extractor.drawImage(awtImage, at);
}

From source file:fr.ign.cogit.geoxygene.appli.layer.LayerViewAwtPanel.java

public void saveAsImage(String fileName, int width, int height, boolean doSaveWorldFile, boolean drawOverlay) {
    Color bg = this.getBackground();
    BufferedImage outImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graphics = outImage.createGraphics();
    // TEMP/* ww  w . j  a va  2 s . c o  m*/
    graphics.setColor(bg);
    graphics.fillRect(0, 0, width, height);
    int tmpw = this.getWidth();
    int tmph = this.getHeight();
    // We save the old extent to force the Viewport to keep the old
    // window in world coordinates.
    IEnvelope env = this.getViewport().getEnvelopeInModelCoordinates();
    // Artificially resize the canvas to the image dimensions.
    this.setSize(width, height);
    try {
        // We zoom to the old extent in the resized canvas.
        this.getViewport().zoom(env);
    } catch (NoninvertibleTransformException e2) {
        logger.error("In Image Export : failed to zoom in the correct extent.");
        e2.printStackTrace();
    }
    this.renderingManager.renderAll();
    long time = System.currentTimeMillis();
    long twaited = 0;
    while (this.renderingManager.isRendering() && twaited < 15000) {
        // Wait for the rendering to end for a maximum of 15s. If the
        // rendering is not finished after this delay,
        // we give up.
        twaited = System.currentTimeMillis() - time;
    }
    if (this.renderingManager.isRendering()) {
        logger.error("Export to image : waited 15s but the rendering is still not finished. Abort.");
        return;
    }

    // We have to impose a bbox !!!
    this.getRenderingManager().copyTo(graphics);

    if (drawOverlay)
        this.paintOverlays(graphics);
    graphics.dispose();
    try {
        ImgUtil.saveImage(outImage, fileName);
    } catch (IOException e1) {
        e1.printStackTrace();
    }

    if (doSaveWorldFile) {
        String wld = FilenameUtils.removeExtension(fileName) + ".wld";
        try {
            AffineTransform t = this.getViewport().getModelToViewTransform();
            fr.ign.cogit.geoxygene.util.conversion.WorldFileWriter.write(new File(wld), t.getScaleX(),
                    t.getScaleY(), this.getViewport().getViewOrigin().getX(),
                    this.getViewport().getViewOrigin().getY(), this.getHeight());
        } catch (NoninvertibleTransformException e) {
            logger.error("Failed to save the world file associated with the image file " + fileName);
            e.printStackTrace();
        }
    }

    // Finally, rollback the canvas to its original size.
    this.setSize(tmpw, tmph);
    try {
        // Zoom back to the "normal" extent
        this.getViewport().zoom(env);
    } catch (NoninvertibleTransformException e2) {
        logger.error("In Image Export : failed to zoom back to the original LayerViewPanel extent.");
        e2.printStackTrace();
        return;
    }
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/**
 * Handle the Batik drawing event/*from ww w . j  ava 2  s .  c om*/
 *
 * @param shape
 *            the shape to draw
 * @param fill
 *            true if the shape is to be drawn filled
 */
private void doDrawing(Shape shape, boolean fill) {
    if (!fill) {
        graphicsObj.newSegment();
    }

    graphicsObj.setColor(gc.getColor());

    applyPaint(gc.getPaint(), fill);

    if (fill) {
        graphicsObj.beginArea();
    } else {
        applyStroke(gc.getStroke());
    }

    AffineTransform trans = gc.getTransform();
    PathIterator iter = shape.getPathIterator(trans);
    if (shape instanceof Line2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.currentSegment(dstPts);
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        graphicsObj.addLine(coords);
    } else if (shape instanceof Rectangle2D) {
        double[] dstPts = new double[6];
        iter.currentSegment(dstPts);
        int[] coords = new int[4];
        coords[X2] = (int) Math.round(dstPts[X]);
        coords[Y2] = (int) Math.round(dstPts[Y]);
        iter.next();
        iter.next();
        iter.currentSegment(dstPts);
        coords[X1] = (int) Math.round(dstPts[X]);
        coords[Y1] = (int) Math.round(dstPts[Y]);
        graphicsObj.addBox(coords);
    } else if (shape instanceof Ellipse2D) {
        double[] dstPts = new double[6];
        Ellipse2D elip = (Ellipse2D) shape;
        double scale = trans.getScaleX();
        double radiusWidth = elip.getWidth() / 2;
        double radiusHeight = elip.getHeight() / 2;
        graphicsObj.setArcParams((int) Math.round(radiusWidth * scale), (int) Math.round(radiusHeight * scale),
                0, 0);
        double[] srcPts = new double[] { elip.getCenterX(), elip.getCenterY() };
        trans.transform(srcPts, 0, dstPts, 0, 1);
        final int mh = 1;
        final int mhr = 0;
        graphicsObj.addFullArc((int) Math.round(dstPts[X]), (int) Math.round(dstPts[Y]), mh, mhr);
    } else {
        processPathIterator(iter);
    }

    if (fill) {
        graphicsObj.endArea();
    }
}

From source file:org.apache.fop.afp.AFPGraphics2D.java

/** {@inheritDoc} */
@Override//from   w ww.j  a v a 2  s  .c  o  m
public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
    int imgWidth = img.getWidth();
    int imgHeight = img.getHeight();

    AffineTransform gat = gc.getTransform();
    int graphicsObjectHeight = graphicsObj.getObjectEnvironmentGroup().getObjectAreaDescriptor().getHeight();

    double toMillipointFactor = UnitConv.IN2PT * 1000 / (double) paintingState.getResolution();
    double x = gat.getTranslateX();
    double y = -(gat.getTranslateY() - graphicsObjectHeight);
    x = toMillipointFactor * x;
    y = toMillipointFactor * y;
    double w = toMillipointFactor * imgWidth * gat.getScaleX();
    double h = toMillipointFactor * imgHeight * -gat.getScaleY();

    AFPImageHandlerRenderedImage handler = new AFPImageHandlerRenderedImage();
    ImageInfo imageInfo = new ImageInfo(null, null);
    imageInfo.setSize(new ImageSize(img.getWidth(), img.getHeight(), paintingState.getResolution()));
    imageInfo.getSize().calcSizeFromPixels();
    ImageRendered red = new ImageRendered(imageInfo, img, null);
    Rectangle targetPos = new Rectangle((int) Math.round(x), (int) Math.round(y), (int) Math.round(w),
            (int) Math.round(h));
    AFPRenderingContext context = new AFPRenderingContext(null, resourceManager, paintingState, fontInfo, null);
    try {
        handler.handleImage(context, red, targetPos);
    } catch (IOException ioe) {
        handleIOException(ioe);
    }
}

From source file:org.apache.fop.render.pcl.PCLRenderingUtil.java

/**
 * Determines the print direction based on the given transformation matrix. This method
 * only detects right angles (0, 90, 180, 270). If any other angle is determined, 0 is
 * returned./*w  ww  .  j a va  2  s .c om*/
 * @param transform the transformation matrix
 * @return the angle in degrees of the print direction.
 */
public static int determinePrintDirection(AffineTransform transform) {
    int newDir;
    if (transform.getScaleX() == 0 && transform.getScaleY() == 0 && transform.getShearX() == 1
            && transform.getShearY() == -1) {
        newDir = 90;
    } else if (transform.getScaleX() == -1 && transform.getScaleY() == -1 && transform.getShearX() == 0
            && transform.getShearY() == 0) {
        newDir = 180;
    } else if (transform.getScaleX() == 0 && transform.getScaleY() == 0 && transform.getShearX() == -1
            && transform.getShearY() == 1) {
        newDir = 270;
    } else {
        newDir = 0;
    }
    return newDir;
}

From source file:org.apache.fop.render.pdf.pdfbox.PDFBoxImageHandler.java

public void handleImage(RenderingContext context, Image image, Rectangle pos) throws IOException {
    assert context instanceof PDFRenderingContext;
    PDFRenderingContext pdfContext = (PDFRenderingContext) context;
    PDFContentGenerator generator = pdfContext.getGenerator();
    assert image instanceof ImagePDF;
    ImagePDF pdfImage = (ImagePDF) image;

    float x = (float) pos.getX() / 1000f;
    float y = (float) pos.getY() / 1000f;
    //        float w = (float)pos.getWidth() / 1000f;
    float h = (float) pos.getHeight() / 1000f;

    AffineTransform pageAdjust = new AffineTransform();
    AffineTransform at = generator.getAffineTransform();
    if (at != null) {
        pageAdjust.setToTranslation((float) (generator.getState().getTransform().getTranslateX()),
                (float) (generator.getState().getTransform().getTranslateY() - h - y));
    }/*from   w  w w  . j  ava  2  s .co m*/
    FontInfo fontinfo = (FontInfo) context.getHint("fontinfo");
    String stream = createStreamForPDF(pdfImage, pdfContext.getPage(), pdfContext.getUserAgent(), pageAdjust,
            fontinfo, pos, pdfContext.getPageNumbers(), pdfContext.getPdfLogicalStructureHandler(),
            pdfContext.getCurrentSessionStructElem());

    if (stream == null) {
        return;
    }
    if (pageAdjust.getScaleX() != 0) {
        pageAdjust.translate(x * (1 / pageAdjust.getScaleX()), -y * (1 / -pageAdjust.getScaleY()));
    }
    generator.placeImage(pageAdjust, stream);
}