Example usage for java.awt.geom AffineTransform setToIdentity

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

Introduction

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

Prototype

public void setToIdentity() 

Source Link

Document

Resets this transform to the Identity transform.

Usage

From source file:BufferedImageThread.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Dimension d = getSize();//  w  w w  .j  ava2 s.c  o m

    bi = new BufferedImage(d.width, d.height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D big = bi.createGraphics();

    step(d.width, d.height);

    AffineTransform at = new AffineTransform();
    at.setToIdentity();
    at.translate(x, y);
    at.rotate(Math.toRadians(rotate));
    at.scale(scale, scale);
    big.drawImage(image, at, this);

    Graphics2D g2D = (Graphics2D) g;
    g2D.drawImage(bi, 0, 0, null);

    big.dispose();
}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java

public void createAppearanceDictionary(PDXObjectForm holderForml, PDSignatureField signatureField,
        float degrees) throws IOException {

    PDAppearanceDictionary appearance = new PDAppearanceDictionary();
    appearance.getCOSObject().setDirect(true);

    PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForml.getCOSStream());
    AffineTransform transform = new AffineTransform();
    transform.setToIdentity();
    transform.rotate(Math.toRadians(degrees));
    appearanceStream.setMatrix(transform);
    appearance.setNormalAppearance(appearanceStream);
    signatureField.getWidget().setAppearance(appearance);

    getStructure().setAppearanceDictionary(appearance);
    logger.debug("PDF appearance Dictionary has been created");

}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox2.PDFAsVisualSignatureBuilder.java

public void createAppearanceDictionary(PDFormXObject holderForml, PDSignatureField signatureField,
        float degrees) throws IOException {

    PDAppearanceDictionary appearance = new PDAppearanceDictionary();
    appearance.getCOSObject().setDirect(true);

    PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForml.getCOSStream());
    AffineTransform transform = new AffineTransform();
    transform.setToIdentity();
    transform.rotate(Math.toRadians(degrees));
    appearanceStream.setMatrix(transform);
    appearance.setNormalAppearance(appearanceStream);
    signatureField.getWidget().setAppearance(appearance);

    getStructure().setAppearanceDictionary(appearance);
    logger.debug("PDF appereance Dictionary has been created");

}

From source file:at.gv.egiz.pdfas.lib.impl.stamping.pdfbox.PDFAsVisualSignatureBuilder.java

public void createSignatureRectangle(PDSignatureField signatureField, PDFAsVisualSignatureDesigner properties,
        float degrees) throws IOException {

    PDRectangle rect = new PDRectangle();

    Point2D upSrc = new Point2D.Float();
    upSrc.setLocation(properties.getxAxis() + properties.getWidth(),
            properties.getPageHeight() - properties.getyAxis());

    Point2D llSrc = new Point2D.Float();
    llSrc.setLocation(properties.getxAxis(),
            properties.getPageHeight() - properties.getyAxis() - properties.getHeight());

    rect.setUpperRightX((float) upSrc.getX());
    rect.setUpperRightY((float) upSrc.getY());
    rect.setLowerLeftY((float) llSrc.getY());
    rect.setLowerLeftX((float) llSrc.getX());
    logger.debug("orig rectangle of signature has been created: {}", rect.toString());

    AffineTransform transform = new AffineTransform();
    transform.setToIdentity();
    if (degrees % 360 != 0) {
        transform.setToRotation(Math.toRadians(degrees), llSrc.getX(), llSrc.getY());
    }//w  ww .  j  a  va2s .c  om

    Point2D upDst = new Point2D.Float();
    transform.transform(upSrc, upDst);

    Point2D llDst = new Point2D.Float();
    transform.transform(llSrc, llDst);

    float xPos = properties.getxAxis();
    float yPos = properties.getPageHeight() - properties.getyAxis();
    logger.debug("POS {} x {}", xPos, yPos);
    logger.debug("SIZE {} x {}", properties.getWidth(), properties.getHeight());
    // translate according to page! rotation
    int pageRotation = properties.getPageRotation();
    AffineTransform translate = new AffineTransform();
    switch (pageRotation) {
    case 90:
        translate.setToTranslation(
                properties.getPageHeight() - (properties.getPageHeight() - properties.getyAxis())
                        - properties.getxAxis() + properties.getHeight(),
                properties.getxAxis() + properties.getHeight()
                        - (properties.getPageHeight() - properties.getyAxis()));
        break;
    case 180:
        // translate.setToTranslation(properties.getPageWidth() -
        // properties.getxAxis() - properties.getxAxis(),
        // properties.getPageHeight() - properties.getyAxis() +
        // properties.getHeight());
        translate.setToTranslation(properties.getPageWidth() - 2 * xPos,
                properties.getPageHeight() - 2 * (yPos - properties.getHeight()));
        break;
    case 270:
        translate.setToTranslation(-properties.getHeight() + yPos - xPos,
                properties.getPageWidth() - (yPos - properties.getHeight()) - xPos);
        break;
    }

    translate.transform(upDst, upDst);
    translate.transform(llDst, llDst);

    rect.setUpperRightX((float) upDst.getX());
    rect.setUpperRightY((float) upDst.getY());
    rect.setLowerLeftY((float) llDst.getY());
    rect.setLowerLeftX((float) llDst.getX());
    logger.debug("rectangle of signature has been created: {}", rect.toString());
    signatureField.getWidget().setRectangle(rect);
    getStructure().setSignatureRectangle(rect);
    logger.debug("rectangle of signature has been created");
}

From source file:HelloUniverse.java

private void drawZPip(Graphics2D g2, float zAngle) {
    AffineTransform trans = new AffineTransform();
    Color origColor = g2.getColor();

    trans.translate(margin, margin);//from  w w w .j a v a2 s  .c  om
    trans.rotate(zAngle, diameter / 2, diameter / 2);

    g2.setXORMode(getBackground());
    g2.setTransform(trans);
    g2.setColor(Color.red);
    g2.fillPolygon(zPip);

    // Reset graphics context
    trans.setToIdentity();
    g2.setTransform(trans);
    g2.setColor(origColor);
    g2.setPaintMode();
}

From source file:HelloUniverse.java

private void drawXPip(Graphics2D g2, float angle) {
    AffineTransform trans = new AffineTransform();
    int y;//  w w  w  . j  a v a2  s . c  o m
    int xOrig = margin + diameter + space;
    int yOrig = margin;
    Color origColor = g2.getColor();

    if (angle <= Math.PI) {
        y = yOrig + diameter - (int) ((Math.abs(angle - Math.PI / 2) / (Math.PI / 2)) * diameter / 2);
    } else
        y = yOrig + (int) ((Math.abs((angle - Math.PI * 1.5)) / (Math.PI / 2)) * diameter / 2);

    if (angle < Math.PI / 2 || angle > Math.PI * 1.5)
        g2.setColor(Color.red); // Infront of wheel
    else {
        g2.setColor(Color.black); // Behind Wheel
        g2.setClip(xBackClip);
    }

    g2.setXORMode(getBackground());
    trans.setToTranslation(xOrig + pipOffset, y);
    g2.setTransform(trans);
    g2.fillPolygon(xPip);

    // Reset graphics context
    trans.setToIdentity();
    g2.setTransform(trans);
    g2.setColor(origColor);
    g2.setPaintMode();
}

From source file:HelloUniverse.java

private void drawYPip(Graphics2D g2, float angle) {
    AffineTransform trans = new AffineTransform();
    int x;//from  w  w  w  . jav a 2  s .co m
    int xOrig = margin;
    int yOrig = margin + diameter + space;
    Color origColor = g2.getColor();

    if (angle <= Math.PI) {
        x = xOrig + diameter - (int) ((Math.abs(angle - Math.PI / 2) / (Math.PI / 2)) * diameter / 2);
    } else
        x = xOrig + (int) ((Math.abs((angle - Math.PI * 1.5)) / (Math.PI / 2)) * diameter / 2);

    if (angle < Math.PI / 2 || angle > Math.PI * 1.5)
        g2.setColor(Color.red); // Infront on wheel
    else {
        g2.setColor(Color.black); // Behind Wheel
        g2.setClip(yBackClip);
    }

    g2.setXORMode(getBackground());
    trans.setToTranslation(x, yOrig + pipOffset);
    g2.setTransform(trans);
    g2.fillPolygon(yPip);

    // Reset graphics context
    trans.setToIdentity();
    g2.setTransform(trans);
    g2.setColor(origColor);
    g2.setPaintMode();
}

From source file:lucee.runtime.img.Image.java

public void translate(int xtrans, int ytrans, Object interpolation) throws ExpressionException {

    RenderingHints hints = new RenderingHints(RenderingHints.KEY_INTERPOLATION, interpolation);
    if (interpolation != RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR) {
        hints.add(new RenderingHints(JAI.KEY_BORDER_EXTENDER, BorderExtender.createInstance(1)));
    }//w w  w .  j a  v a2  s. c o  m

    ParameterBlock pb = new ParameterBlock();
    pb.addSource(image());
    BufferedImage img = JAI.create("translate", pb).getAsBufferedImage();
    Graphics2D graphics = img.createGraphics();
    graphics.clearRect(0, 0, img.getWidth(), img.getHeight());
    AffineTransform at = new AffineTransform();
    at.setToIdentity();
    graphics.drawImage(image(), new AffineTransformOp(at, hints), xtrans, ytrans);
    graphics.dispose();
    image(img);
}

From source file:Thumbnail.java

/**
* Reads an image in a file and creates a thumbnail in another file.
* largestDimension is the largest dimension of the thumbnail, the other dimension is scaled accordingly.
* Utilises weighted stepping method to gradually reduce the image size for better results,
* i.e. larger steps to start with then smaller steps to finish with.
* Note: always writes a JPEG because GIF is protected or something - so always make your outFilename end in 'jpg'.
* PNG's with transparency are given white backgrounds
*/// w ww  .j  av a  2s.  co  m
public String createThumbnail(String inFilename, String outFilename, int largestDimension) {
    try {
        double scale;
        int sizeDifference, originalImageLargestDim;
        if (!inFilename.endsWith(".jpg") && !inFilename.endsWith(".jpeg") && !inFilename.endsWith(".gif")
                && !inFilename.endsWith(".png")) {
            return "Error: Unsupported image type, please only either JPG, GIF or PNG";
        } else {
            Image inImage = Toolkit.getDefaultToolkit().getImage(inFilename);
            if (inImage.getWidth(null) == -1 || inImage.getHeight(null) == -1) {
                return "Error loading file: \"" + inFilename + "\"";
            } else {
                //find biggest dimension       
                if (inImage.getWidth(null) > inImage.getHeight(null)) {
                    scale = (double) largestDimension / (double) inImage.getWidth(null);
                    sizeDifference = inImage.getWidth(null) - largestDimension;
                    originalImageLargestDim = inImage.getWidth(null);
                } else {
                    scale = (double) largestDimension / (double) inImage.getHeight(null);
                    sizeDifference = inImage.getHeight(null) - largestDimension;
                    originalImageLargestDim = inImage.getHeight(null);
                }
                //create an image buffer to draw to
                BufferedImage outImage = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); //arbitrary init so code compiles
                Graphics2D g2d;
                AffineTransform tx;
                if (scale < 1.0d) //only scale if desired size is smaller than original
                {
                    int numSteps = sizeDifference / 100;
                    int stepSize = sizeDifference / numSteps;
                    int stepWeight = stepSize / 2;
                    int heavierStepSize = stepSize + stepWeight;
                    int lighterStepSize = stepSize - stepWeight;
                    int currentStepSize, centerStep;
                    double scaledW = inImage.getWidth(null);
                    double scaledH = inImage.getHeight(null);
                    if (numSteps % 2 == 1) //if there's an odd number of steps
                        centerStep = (int) Math.ceil((double) numSteps / 2d); //find the center step
                    else
                        centerStep = -1; //set it to -1 so it's ignored later
                    Integer intermediateSize = originalImageLargestDim,
                            previousIntermediateSize = originalImageLargestDim;
                    Integer calculatedDim;
                    for (Integer i = 0; i < numSteps; i++) {
                        if (i + 1 != centerStep) //if this isn't the center step
                        {
                            if (i == numSteps - 1) //if this is the last step
                            {
                                //fix the stepsize to account for decimal place errors previously
                                currentStepSize = previousIntermediateSize - largestDimension;
                            } else {
                                if (numSteps - i > numSteps / 2) //if we're in the first half of the reductions
                                    currentStepSize = heavierStepSize;
                                else
                                    currentStepSize = lighterStepSize;
                            }
                        } else //center step, use natural step size
                        {
                            currentStepSize = stepSize;
                        }
                        intermediateSize = previousIntermediateSize - currentStepSize;
                        scale = (double) intermediateSize / (double) previousIntermediateSize;
                        scaledW = (int) scaledW * scale;
                        scaledH = (int) scaledH * scale;
                        outImage = new BufferedImage((int) scaledW, (int) scaledH, BufferedImage.TYPE_INT_RGB);
                        g2d = outImage.createGraphics();
                        g2d.setBackground(Color.WHITE);
                        g2d.clearRect(0, 0, outImage.getWidth(), outImage.getHeight());
                        g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                        tx = new AffineTransform();
                        tx.scale(scale, scale);
                        g2d.drawImage(inImage, tx, null);
                        g2d.dispose();
                        inImage = new ImageIcon(outImage).getImage();
                        previousIntermediateSize = intermediateSize;
                    }
                } else {
                    //just copy the original
                    outImage = new BufferedImage(inImage.getWidth(null), inImage.getHeight(null),
                            BufferedImage.TYPE_INT_RGB);
                    g2d = outImage.createGraphics();
                    g2d.setBackground(Color.WHITE);
                    g2d.clearRect(0, 0, outImage.getWidth(), outImage.getHeight());
                    tx = new AffineTransform();
                    tx.setToIdentity(); //use identity matrix so image is copied exactly
                    g2d.drawImage(inImage, tx, null);
                    g2d.dispose();
                }
                //JPEG-encode the image and write to file.
                OutputStream os = new FileOutputStream(outFilename);
                JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(os);
                encoder.encode(outImage);
                os.close();
            }
        }
    } catch (Exception ex) {
        String errorMsg = "";
        errorMsg += "<br>Exception: " + ex.toString();
        errorMsg += "<br>Cause = " + ex.getCause();
        errorMsg += "<br>Stack Trace = ";
        StackTraceElement stackTrace[] = ex.getStackTrace();
        for (int traceLine = 0; traceLine < stackTrace.length; traceLine++) {
            errorMsg += "<br>" + stackTrace[traceLine];
        }
        return errorMsg;
    }
    return ""; //success
}

From source file:org.encuestame.business.images.ImageThumbnailGeneratorImpl.java

/**
 * Create a thumbnail image and save it to disk.
 *
 * This algorithm is based on:/*from www  .j ava2s. c  om*/
 *      http://www.philreeve.com/java_high_quality_thumbnails.php
 *
 * @param imageIn           The image you want to scale.
 * @param fileOut           The output file.
 * @param largestDimension  The largest dimension, so that neither the width nor height
 *                          will exceed this value.
 *
 * @return the image that was created, null if imageIn or fileOut is null.
 * @throws java.io.IOException if something goes wrong when saving as jpeg
 */
public BufferedImage createThumbnailImage(Image imageIn, File fileOut, int largestDimension)
        throws IOException {
    if ((imageIn == null) || (fileOut == null)) {
        return null;
    }
    //it seems to not return the right size until the methods get called for the first time
    imageIn.getWidth(null);
    imageIn.getHeight(null);

    // Find biggest dimension
    int nImageWidth = imageIn.getWidth(null);
    int nImageHeight = imageIn.getHeight(null);
    int nImageLargestDim = Math.max(nImageWidth, nImageHeight);
    double scale = (double) largestDimension / (double) nImageLargestDim;
    int sizeDifference = nImageLargestDim - largestDimension;

    //create an image buffer to draw to
    BufferedImage imageOut = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); // 8-bit RGB
    Graphics2D g2d;
    AffineTransform tx;

    // Use a few steps if the sizes are drastically different, and only scale
    // if the desired size is smaller than the original.
    int numSteps = 0;
    if (scale < 1.0d) {
        // Make sure we have at least 1 step
        numSteps = Math.max(1, (sizeDifference / 100));
    }

    if (numSteps > 0) {
        int stepSize = sizeDifference / numSteps;
        int stepWeight = stepSize / 2;
        int heavierStepSize = stepSize + stepWeight;
        int lighterStepSize = stepSize - stepWeight;
        int currentStepSize, centerStep;
        double scaledW = imageIn.getWidth(null);
        double scaledH = imageIn.getHeight(null);

        if ((numSteps % 2) == 1) //if there's an odd number of steps
            centerStep = (int) Math.ceil((double) numSteps / 2d); //find the center step
        else
            centerStep = -1; //set it to -1 so it's ignored later

        Integer intermediateSize;
        Integer previousIntermediateSize = nImageLargestDim;

        for (Integer i = 0; i < numSteps; i++) {
            if (i + 1 != centerStep) {
                //if this isn't the center step

                if (i == numSteps - 1) {
                    //if this is the last step
                    //fix the stepsize to account for decimal place errors previously
                    currentStepSize = previousIntermediateSize - largestDimension;
                } else {
                    if (numSteps - i > numSteps / 2) //if we're in the first half of the reductions
                        currentStepSize = heavierStepSize;
                    else
                        currentStepSize = lighterStepSize;
                }
            } else {
                //center step, use natural step size
                currentStepSize = stepSize;
            }

            intermediateSize = previousIntermediateSize - currentStepSize;
            scale = intermediateSize / (double) previousIntermediateSize;
            scaledW = Math.max((int) (scaledW * scale), 1);
            scaledH = Math.max((int) (scaledH * scale), 1);

            log.info("step " + i + ": scaling to " + scaledW + " x " + scaledH);
            imageOut = new BufferedImage((int) scaledW, (int) scaledH, BufferedImage.TYPE_INT_RGB); // 8 bit RGB
            g2d = imageOut.createGraphics();
            g2d.setBackground(Color.WHITE);
            g2d.clearRect(0, 0, imageOut.getWidth(), imageOut.getHeight());
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            tx = new AffineTransform();
            tx.scale(scale, scale);
            g2d.drawImage(imageIn, tx, null);
            g2d.dispose();
            imageIn = new ImageIcon(imageOut).getImage();
            previousIntermediateSize = intermediateSize;
        }
    } else {
        // This enforces a rule that we always have an 8-bit image with white background for the thumbnail.  Plus, for large
        // images, this makes subsequent downscaling really fast because we are working on a large 8-bit image
        // instead of a large 12 or 24 bit image, so the downstream effect is very noticable.
        imageOut = new BufferedImage(imageIn.getWidth(null), imageIn.getHeight(null),
                BufferedImage.TYPE_INT_RGB);
        g2d = imageOut.createGraphics();
        g2d.setBackground(Color.WHITE);
        g2d.clearRect(0, 0, imageOut.getWidth(), imageOut.getHeight());
        tx = new AffineTransform();
        tx.setToIdentity(); //use identity matrix so image is copied exactly
        g2d.drawImage(imageIn, tx, null);
        g2d.dispose();
    }
    //saveImageAsJPEG(imageOut, fileOut);
    ImageIO.write(imageOut, "jpg", fileOut);
    return imageOut;
}