Example usage for java.awt.geom AffineTransform setToTranslation

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

Introduction

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

Prototype

public void setToTranslation(double tx, double ty) 

Source Link

Document

Sets this transform to a translation transformation.

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.setToTranslation(Math.PI / 6, Math.PI / 6);

    g2.setTransform(at);/*w  w w . j  av  a2s . c  o m*/
    g2.draw(shape);

}

From source file:FontRenderContextRenderingHints.java

public void paint(Graphics g) {
    Graphics2D g2D = (Graphics2D) g;
    int w = getSize().width;
    int h = getSize().height;

    RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
    qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g2D.setRenderingHints(qualityHints);

    AffineTransform at = new AffineTransform();
    at.setToTranslation(-300, -400);
    at.shear(-0.5, 0.0);//from  w  w  w .j a v  a2 s .c  om
    FontRenderContext frc = new FontRenderContext(at, false, false);
    TextLayout tl = new TextLayout("World!", font, frc);
    Shape outline = tl.getOutline(null);
    g2D.setColor(Color.blue);

    BasicStroke wideStroke = new BasicStroke(2.0f);
    g2D.setStroke(wideStroke);
    g2D.draw(outline);

}

From source file:TransformTransRotation.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    // Use antialiasing.
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Move the origin to 75, 75.
    AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
    g2.transform(at);/*from w  w  w  .j  a v  a  2  s  . c  om*/

    // Draw the shapes in their original locations.
    g2.setPaint(Color.black);
    g2.draw(axes);
    g2.draw(shape);

    // Transform the Graphics2D.
    AffineTransform rat = new AffineTransform();
    rat.setToTranslation(100, 0);
    rat.rotate(Math.PI / 6);
    g2.transform(rat);

    // Draw the "new" shapes in dashed.
    Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 },
            0);
    g2.setStroke(stroke);
    g2.draw(axes);
    g2.draw(shape);
}

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();/*from   w ww . j a va  2 s  .c o  m*/
    if (degrees % 360 != 0) {
        transform.setToRotation(Math.toRadians(degrees), llSrc.getX(), llSrc.getY());
    }

    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 drawXPip(Graphics2D g2, float angle) {
    AffineTransform trans = new AffineTransform();
    int y;/*from w  w  w  .  j  av a  2 s  .c  om*/
    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  ww  . j a va2 s.  com
    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: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));
    }/*w w w.  j a va2 s .  c  o 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);
}

From source file:org.jcurl.demo.tactics.sg.BroomPromptScenario.java

private void syncSpeedM2V(final BoundedRangeModel r) {
    // log.info(r.getValue() + "/" + r.getMaximum());
    sli.setFillPaint(IceShapes.sliderColor(r));
    final AffineTransform t = slider.getAffine();
    t.setToTranslation(0, stickLength * IceShapes.value2ratio(r));
    slider.setAffine(t);/*from w w w . j  a v a  2  s.  c  om*/
}

From source file:se.ngm.ditaaeps.EpsRenderer.java

public static void renderToEps(Diagram diagram, PrintWriter out, RenderingOptions options) {
    //RenderedImage renderedImage = image;
    EpsGraphics2D g2 = new EpsGraphics2D(out,
            new Rectangle2D.Double(0, -diagram.getHeight(), diagram.getWidth(), diagram.getHeight()));

    g2.scale(1, -1); // g2 origo is top-left, eps is bottom-left

    Object antialiasSetting = antialiasSetting = RenderingHints.VALUE_ANTIALIAS_OFF;
    if (options.performAntialias())
        antialiasSetting = RenderingHints.VALUE_ANTIALIAS_ON;

    //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting);

    g2.setColor(Color.white);// w w w .jav  a2s  .co m
    //TODO: find out why the next line does not work
    //g2.fillRect(0, 0, image.getWidth()+10, image.getHeight()+10);
    /*for(int y = 0; y < diagram.getHeight(); y ++)
      g2.drawLine(0, y, diagram.getWidth(), y);*/

    g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE, BasicStroke.JOIN_ROUND));

    ArrayList shapes = diagram.getAllDiagramShapes();

    if (DEBUG)
        System.out.println("Rendering " + shapes.size() + " shapes (groups flattened)");

    Iterator shapesIt;
    if (options.dropShadows()) {
        //render shadows
        shapesIt = shapes.iterator();
        while (shapesIt.hasNext()) {
            DiagramShape shape = (DiagramShape) shapesIt.next();

            if (shape.getPoints().isEmpty())
                continue;

            //GeneralPath path = shape.makeIntoPath();
            GeneralPath path;
            path = shape.makeIntoRenderPath(diagram);

            float offset = diagram.getMinimumOfCellDimension() / 3.333f;

            if (path != null && shape.dropsShadow()) {
                GeneralPath shadow = new GeneralPath(path);
                AffineTransform translate = new AffineTransform();
                translate.setToTranslation(offset, offset);
                shadow.transform(translate);
                g2.setColor(new Color(150, 150, 150));
                g2.fill(shadow);

            }
        }

        //blur shadows

        //            if(true) {
        //                int blurRadius = 6;
        //                int blurRadius2 = blurRadius * blurRadius;
        //                float blurRadius2F = blurRadius2;
        //                float weight = 1.0f / blurRadius2F;
        //                float[] elements = new float[blurRadius2];
        //                for (int k = 0; k < blurRadius2; k++)
        //                    elements[k] = weight;
        //                Kernel myKernel = new Kernel(blurRadius, blurRadius, elements);
        //
        //                //if EDGE_NO_OP is not selected, EDGE_ZERO_FILL is the default which creates a black border
        //                ConvolveOp simpleBlur =
        //                        new ConvolveOp(myKernel, ConvolveOp.EDGE_NO_OP, null);
        //                //BufferedImage destination = new BufferedImage(image.getWidth()+blurRadius, image.getHeight()+blurRadius, image.getType());
        //                BufferedImage destination =
        //                        new BufferedImage(
        //                        image.getWidth(),
        //                        image.getHeight(),
        //                        image.getType());
        //                simpleBlur.filter(image, destination);
        //                //destination = destination.getSubimage(blurRadius/2, blurRadius/2, image.getWidth(), image.getHeight());
        //                g2 = destination.createGraphics();
        //                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasSetting);
        //                renderedImage = destination;
        //            }
    }

    //fill and stroke

    float dashInterval = Math.min(diagram.getCellWidth(), diagram.getCellHeight()) / 2;
    //Stroke normalStroke = g2.getStroke();

    float strokeWeight = diagram.getMinimumOfCellDimension() / 10;

    Stroke normalStroke = new BasicStroke(strokeWeight,
            //10,
            BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);

    Stroke dashStroke = new BasicStroke(strokeWeight, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND, 0,
            new float[] { dashInterval }, 0);

    //find storage shapes
    ArrayList storageShapes = new ArrayList();
    shapesIt = shapes.iterator();
    while (shapesIt.hasNext()) {
        DiagramShape shape = (DiagramShape) shapesIt.next();
        if (shape.getType() == DiagramShape.TYPE_STORAGE) {
            storageShapes.add(shape);
            continue;
        }
    }

    //render storage shapes
    //special case since they are '3d' and should be
    //rendered bottom to top
    //TODO: known bug: if a storage object is within a bigger normal box, it will be overwritten in the main drawing loop
    //(BUT this is not possible since tags are applied to all shapes overlaping shapes)

    Collections.sort(storageShapes, new Shape3DOrderingComparator());

    g2.setStroke(normalStroke);
    shapesIt = storageShapes.iterator();
    while (shapesIt.hasNext()) {
        DiagramShape shape = (DiagramShape) shapesIt.next();

        GeneralPath path;
        path = shape.makeIntoRenderPath(diagram);

        if (!shape.isStrokeDashed()) {
            if (shape.getFillColor() != null)
                g2.setColor(shape.getFillColor());
            else
                g2.setColor(Color.white);
            g2.fill(path);
        }

        if (shape.isStrokeDashed())
            g2.setStroke(dashStroke);
        else
            g2.setStroke(normalStroke);
        g2.setColor(shape.getStrokeColor());
        g2.draw(path);
    }

    //render the rest of the shapes
    ArrayList pointMarkers = new ArrayList();
    shapesIt = shapes.iterator();
    while (shapesIt.hasNext()) {
        DiagramShape shape = (DiagramShape) shapesIt.next();
        if (shape.getType() == DiagramShape.TYPE_POINT_MARKER) {
            pointMarkers.add(shape);
            continue;
        }
        if (shape.getType() == DiagramShape.TYPE_STORAGE) {
            continue;
        }

        if (shape.getPoints().isEmpty())
            continue;

        int size = shape.getPoints().size();

        GeneralPath path;
        path = shape.makeIntoRenderPath(diagram);

        if (path != null && shape.isClosed() && !shape.isStrokeDashed()) {
            if (shape.getFillColor() != null)
                g2.setColor(shape.getFillColor());
            else
                g2.setColor(Color.white);
            g2.fill(path);
        }
        if (shape.getType() != DiagramShape.TYPE_ARROWHEAD) {
            g2.setColor(shape.getStrokeColor());
            if (shape.isStrokeDashed())
                g2.setStroke(dashStroke);
            else
                g2.setStroke(normalStroke);
            g2.draw(path);
        }
    }

    //render point markers

    g2.setStroke(normalStroke);
    shapesIt = pointMarkers.iterator();
    while (shapesIt.hasNext()) {
        DiagramShape shape = (DiagramShape) shapesIt.next();
        //if(shape.getType() != DiagramShape.TYPE_POINT_MARKER) continue;

        GeneralPath path;
        path = shape.makeIntoRenderPath(diagram);

        g2.setColor(Color.white);
        g2.fill(path);
        g2.setColor(shape.getStrokeColor());
        g2.draw(path);
    }

    //handle text
    //g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

    Iterator textIt = diagram.getTextObjects().iterator();
    while (textIt.hasNext()) {
        DiagramText text = (DiagramText) textIt.next();
        g2.setColor(text.getColor());
        g2.setFont(text.getFont());
        g2.drawString(text.getText(), text.getXPos(), text.getYPos());
    }

    if (options.renderDebugLines() || DEBUG) {
        Stroke debugStroke = new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        g2.setStroke(debugStroke);
        g2.setColor(new Color(170, 170, 170));
        g2.setXORMode(Color.white);
        for (int x = 0; x < diagram.getWidth(); x += diagram.getCellWidth())
            g2.drawLine(x, 0, x, diagram.getHeight());
        for (int y = 0; y < diagram.getHeight(); y += diagram.getCellHeight())
            g2.drawLine(0, y, diagram.getWidth(), y);
    }

    g2.dispose();
}

From source file:se.vgregion.webbisar.svc.impl.ImageUtil.java

/**
 * Resizes and overwrites the image. Will keep the original pictures dimensions.
 * //  www . j  a v  a 2s .c  o m
 * @param sourceFile
 *            the file to rezise
 * @param newLongSideSize
 *            the new size, in pixels.
 * @param quality
 *            a number between 0 and 100 where 100 gives the best quality
 * @throws IOException
 */
public synchronized static void scaleImage(File sourceFile, ImageSize imageSize, float quality)
        throws IOException {
    // System.gc();
    BufferedImage sourceImage = ImageIO.read(sourceFile);
    int srcWidth = sourceImage.getWidth();
    int srcHeight = sourceImage.getHeight();

    double longSideForSource = Math.max(srcWidth, srcHeight);
    double longSideForDest = srcWidth > srcHeight ? imageSize.getWidth() : imageSize.getHeight();
    double multiplier = longSideForDest / longSideForSource;

    int destWidth = (int) (srcWidth * multiplier);
    int destHeight = (int) (srcHeight * multiplier);

    BufferedImage destImage = new BufferedImage((int) imageSize.getWidth(), (int) imageSize.getHeight(),
            BufferedImage.TYPE_INT_RGB);

    Graphics2D graphics = destImage.createGraphics();
    graphics.setPaint(Color.WHITE);
    graphics.fillRect(0, 0, destImage.getWidth(), destImage.getHeight());

    AffineTransform affineTransform = AffineTransform.getScaleInstance(multiplier, multiplier);
    AffineTransform trans = new AffineTransform();
    trans.setToTranslation((imageSize.getWidth() - destWidth) / 2, (imageSize.getHeight() - destHeight) / 2);
    graphics.transform(trans);

    graphics.drawRenderedImage(sourceImage, affineTransform);
    saveImageAsJPEG(sourceFile.getAbsolutePath(), destImage, quality);

}