Example usage for java.awt.geom AffineTransform AffineTransform

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

Introduction

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

Prototype

public AffineTransform() 

Source Link

Document

Constructs a new AffineTransform representing the Identity transformation.

Usage

From source file:com.rapidminer.gui.new_plotter.engine.jfreechart.PlotInstanceLegendCreator.java

private CustomLegendItem createValueSourceLegendItem(PlotConfiguration plotConfig, ValueSource valueSource) {

    Set<PlotDimension> dimensions = new HashSet<PlotDimension>();
    for (PlotDimension dimension : PlotDimension.values()) {
        switch (dimension) {
        case DOMAIN:
        case VALUE:
            break;
        default:/*w  ww  . ja  v a 2 s  .  co  m*/
            if (valueSource.useSeriesFormatForDimension(plotConfig, dimension)) {
                dimensions.add(dimension);
            }
        }
    }
    if (dimensions.isEmpty()) {
        return null;
    }

    SeriesFormat format = valueSource.getSeriesFormat();
    String description = "";
    String toolTipText = "";
    String urlText = "";
    boolean shapeVisible = true;
    Shape shape;
    boolean shapeFilled = true;
    Paint fillPaint = UNDEFINED_COLOR_PAINT;
    boolean shapeOutlineVisible = true;
    Paint outlinePaint = PlotConfiguration.DEFAULT_OUTLINE_COLOR;
    Stroke outlineStroke = DEFAULT_OUTLINE_STROKE;
    boolean lineVisible = format.getLineStyle() != LineStyle.NONE
            && format.getSeriesType() == SeriesFormat.VisualizationType.LINES_AND_SHAPES;

    // configure fill paint and line paint
    Paint linePaint;
    String label = valueSource.toString();
    if (label == null) {
        label = "";
    }
    if (dimensions.contains(PlotDimension.COLOR)) {
        Color color = format.getItemColor();
        fillPaint = format.getAreaFillPaint(color);
        linePaint = fillPaint;
    } else {
        if (format.getAreaFillStyle() == FillStyle.NONE) {
            fillPaint = new Color(0, 0, 0, 0);
            linePaint = fillPaint;
        } else if (format.getAreaFillStyle() == FillStyle.SOLID) {
            fillPaint = UNDEFINED_COLOR_PAINT;
            linePaint = UNDEFINED_LINE_COLOR;
        } else {
            fillPaint = format.getAreaFillPaint(UNDEFINED_COLOR);
            linePaint = fillPaint;
        }

    }

    VisualizationType seriesType = valueSource.getSeriesFormat().getSeriesType();
    if (seriesType == VisualizationType.LINES_AND_SHAPES) {
        if (dimensions.contains(PlotDimension.SHAPE)) {
            shape = format.getItemShape().getShape();
        } else if (dimensions.contains(PlotDimension.COLOR)) {
            shape = UNDEFINED_SHAPE;
        } else {
            shape = UNDEFINED_SHAPE_AND_COLOR;
        }

        if (dimensions.contains(PlotDimension.SIZE)) {
            AffineTransform transformation = new AffineTransform();
            double scalingFactor = format.getItemSize();
            transformation.scale(scalingFactor, scalingFactor);
            shape = transformation.createTransformedShape(shape);
        }
    } else if (seriesType == VisualizationType.BARS) {
        shape = BAR_SHAPE;
    } else if (seriesType == VisualizationType.AREA) {
        shape = AREA_SHAPE;
    } else {
        throw new RuntimeException("Unknown SeriesType. This should not happen.");
    }

    // configure line shape
    float lineLength = 0;
    if (lineVisible) {
        lineLength = format.getLineWidth();
        if (lineLength < 1) {
            lineLength = 1;
        }
        if (lineLength > 1) {
            lineLength = 1 + (float) Math.log(lineLength) / 2;
        }

        // line at least 30 pixels long, and show at least 2 iterations of stroke
        lineLength = Math.max(lineLength * 30, format.getStrokeLength() * 2);

        // line at least 2x longer than shape width
        if (shape != null) {
            lineLength = Math.max(lineLength, (float) shape.getBounds().getWidth() * 2f);
        }
    }

    // now create line shape and stroke
    Shape line = new Line2D.Float(0, 0, lineLength, 0);
    BasicStroke lineStroke = format.getStroke();
    if (lineStroke == null) {
        lineStroke = new BasicStroke();
    }

    // unset line ending decoration to prevent drawing errors in legend
    {
        BasicStroke s = lineStroke;
        lineStroke = new BasicStroke(s.getLineWidth(), BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND,
                s.getMiterLimit(), s.getDashArray(), s.getDashPhase());
    }

    return new CustomLegendItem(label, description, toolTipText, urlText, shapeVisible, shape, shapeFilled,
            fillPaint, shapeOutlineVisible, outlinePaint, outlineStroke, lineVisible, line, lineStroke,
            linePaint);
}

From source file:org.apache.batchee.tools.maven.doc.DiagramGenerator.java

private void saveView(final Dimension currentSize, final Dimension desiredSize, final String name,
        final VisualizationViewer<Node, Edge> viewer) {
    BufferedImage bi = new BufferedImage(currentSize.width, currentSize.height, BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g = bi.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    final boolean db = viewer.isDoubleBuffered();
    viewer.setDoubleBuffered(false);/*from   ww w. j av a 2s.com*/
    viewer.paint(g);
    viewer.setDoubleBuffered(db);
    if (!currentSize.equals(desiredSize)) {
        final double xFactor = desiredSize.width * 1. / currentSize.width;
        final double yFactor = desiredSize.height * 1. / currentSize.height;
        final double factor = Math.min(xFactor, yFactor);
        info("optimal size is (" + currentSize.width + ", " + currentSize.height + ")");
        info("scaling with a factor of " + factor);

        final AffineTransform tx = new AffineTransform();
        tx.scale(factor, factor);
        final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage biNew = new BufferedImage((int) (bi.getWidth() * factor), (int) (bi.getHeight() * factor),
                bi.getType());
        bi = op.filter(bi, biNew);
    }
    g.dispose();

    OutputStream os = null;
    try {
        final File file = new File(output, (outputFileName != null ? outputFileName : name) + "." + format);
        os = new FileOutputStream(file);
        if (!ImageIO.write(bi, format, os)) {
            throw new IllegalStateException("can't save picture " + name + "." + format);
        }
        info("Saved " + file.getAbsolutePath());
    } catch (final IOException e) {
        throw new IllegalStateException("can't save the diagram", e);
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (final IOException e) {
                // no-op
            }
        }
    }
}

From source file:org.apache.batchee.tools.maven.DiagramMojo.java

private void saveView(final Dimension currentSize, final Dimension desiredSize, final String name,
        final VisualizationViewer<Node, Edge> viewer) throws MojoExecutionException {
    BufferedImage bi = new BufferedImage(currentSize.width, currentSize.height, BufferedImage.TYPE_INT_ARGB);

    final Graphics2D g = bi.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    final boolean db = viewer.isDoubleBuffered();
    viewer.setDoubleBuffered(false);/*from  ww w. j  av a 2s .c  om*/
    viewer.paint(g);
    viewer.setDoubleBuffered(db);
    if (!currentSize.equals(desiredSize)) {
        final double xFactor = desiredSize.width * 1. / currentSize.width;
        final double yFactor = desiredSize.height * 1. / currentSize.height;
        final double factor = Math.min(xFactor, yFactor);
        getLog().info("optimal size is (" + currentSize.width + ", " + currentSize.height + ")");
        getLog().info("scaling with a factor of " + factor);

        final AffineTransform tx = new AffineTransform();
        tx.scale(factor, factor);
        final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
        BufferedImage biNew = new BufferedImage((int) (bi.getWidth() * factor), (int) (bi.getHeight() * factor),
                bi.getType());
        bi = op.filter(bi, biNew);
    }
    g.dispose();

    OutputStream os = null;
    try {
        final File file = new File(output, (outputFileName != null ? outputFileName : name) + "." + format);
        os = new FileOutputStream(file);
        if (!ImageIO.write(bi, format, os)) {
            throw new MojoExecutionException("can't save picture " + name + "." + format);
        }
        getLog().info("Saved " + file.getAbsolutePath());
    } catch (final IOException e) {
        throw new MojoExecutionException("can't save the diagram", e);
    } finally {
        if (os != null) {
            try {
                os.flush();
                os.close();
            } catch (final IOException e) {
                // no-op
            }
        }
    }
}

From source file:PrintCanvas3D.java

public int print(Graphics g, PageFormat pf, int pi) throws PrinterException {

    if (pi >= 1) {
        return Printable.NO_SUCH_PAGE;
    }// w ww .  jav  a 2s  .c  om

    Graphics2D g2d = (Graphics2D) g;
    //g2d.translate(pf.getImageableX(), pf.getImageableY());
    AffineTransform t2d = new AffineTransform();
    t2d.translate(pf.getImageableX(), pf.getImageableY());
    double xscale = pf.getImageableWidth() / (double) bImage.getWidth();
    double yscale = pf.getImageableHeight() / (double) bImage.getHeight();
    double scale = Math.min(xscale, yscale);
    t2d.scale(scale, scale);
    try {
        g2d.drawImage(bImage, t2d, this);
    } catch (Exception ex) {
        ex.printStackTrace();
        return Printable.NO_SUCH_PAGE;
    }
    return Printable.PAGE_EXISTS;
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

@Test
public void testLink() throws Exception {
    PDFDocument pdfdoc = new PDFDocument("");
    PDFPage pdfpage = new PDFPage(new PDFResources(pdfdoc), 0, r, r, r, r);
    pdfpage.setDocument(pdfdoc);//from   ww w  .  j  a v a2 s .com
    pdfpage.setObjectNumber(1);
    Map<Integer, PDFArray> pageNumbers = new HashMap<Integer, PDFArray>();
    PDFBoxAdapter adapter = new PDFBoxAdapter(pdfpage, new HashMap(), pageNumbers);
    PDDocument doc = getResource(LINK);
    PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
    AffineTransform at = new AffineTransform();
    Rectangle r = new Rectangle(0, 1650, 842000, 595000);
    String stream = adapter.createStreamFromPDFBoxPage(doc, page, "key", at, null, r);
    Assert.assertTrue(stream.contains("/Link <</MCID 5 >>BDC"));
    Assert.assertEquals(pageNumbers.size(), 4);
    PDFAnnotList annots = (PDFAnnotList) pdfpage.get("Annots");
    Assert.assertEquals(annots.toPDFString(), "[\n1 0 R\n2 0 R\n]");
    doc.close();
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage resizeImage(BufferedImage img, double xScale, double yScale, int type) {
    if (img == null) {
        return null;
    }// ww  w.j a  va2  s .  c o m

    if (xScale <= 0 || yScale <= 0) {
        return null;
    }

    int w = img.getWidth();
    int h = img.getHeight();

    int neww = (int) (((double) w) * xScale);
    int newh = (int) (((double) h) * yScale);

    BufferedImage out = new BufferedImage(neww, newh, img.getType());

    AffineTransform tr = new AffineTransform();
    tr.scale(xScale, yScale);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    op.filter(img, out);
    return out;
}

From source file:org.apache.pdfbox.pdmodel.interactive.form.AppearanceGeneratorHelper.java

private AffineTransform calculateMatrix(PDRectangle bbox, int rotation) {
    if (rotation == 0) {
        return new AffineTransform();
    }//from  w  ww.j  a  va  2 s  .c o  m
    float tx = 0, ty = 0;
    switch (rotation) {
    case 90:
        tx = bbox.getUpperRightY();
        break;
    case 180:
        tx = bbox.getUpperRightY();
        ty = bbox.getUpperRightX();
        break;
    case 270:
        ty = bbox.getUpperRightX();
        break;
    default:
        break;
    }
    Matrix matrix = Matrix.getRotateInstance(Math.toRadians(rotation), tx, ty);
    return matrix.createAffineTransform();

}

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  ww  w .jav a2 s .co  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:com.flexive.shared.media.impl.FxMediaNativeEngine.java

/**
 * Rotate an image using the requested angle
 *
 * @param bufferedImage imeg to rotate//from  www  . j a  va 2 s  . c om
 * @param angle         angle to rotate
 * @return BufferedImage containing the rotation
 */
@SuppressWarnings({ "UnusedAssignment" })
private static BufferedImage rotate(BufferedImage bufferedImage, int angle) {
    angle = angle % 360;
    if (angle == 0)
        return bufferedImage;
    if (angle < 0)
        angle += 360;
    switch (angle) {
    case 90:
        BufferedImageOp rot90 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI / 2.0,
                bufferedImage.getHeight() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img90 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot90.filter(bufferedImage, img90);
    case 180:
        BufferedImageOp rot180 = new AffineTransformOp(AffineTransform.getRotateInstance(Math.PI,
                bufferedImage.getWidth() / 2.0, bufferedImage.getHeight() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img180 = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(),
                bufferedImage.getType());
        return rot180.filter(bufferedImage, img180);
    case 270:
        BufferedImageOp rot270 = new AffineTransformOp(AffineTransform.getRotateInstance(-Math.PI / 2.0,
                bufferedImage.getWidth() / 2.0, bufferedImage.getWidth() / 2.0),
                AffineTransformOp.TYPE_BILINEAR);
        BufferedImage img270 = new BufferedImage(bufferedImage.getHeight(), bufferedImage.getWidth(),
                bufferedImage.getType());
        return rot270.filter(bufferedImage, img270);
    default:
        //rotate using a non-standard angle (have to draw a box around the image that can fit it)
        int box = (int) Math.sqrt(bufferedImage.getHeight() * bufferedImage.getHeight()
                + bufferedImage.getWidth() * bufferedImage.getWidth());
        BufferedImage imgFree = new BufferedImage(box, box, bufferedImage.getType());
        BufferedImage imgRet = new BufferedImage(box, box, bufferedImage.getType());
        Graphics2D g = imgFree.createGraphics();
        if (bufferedImage.getTransparency() == Transparency.OPAQUE) {
            //draw a white background on opaque images since they dont support transparency
            g.setBackground(Color.WHITE);
            g.clearRect(0, 0, box, box);
            Graphics2D gr = imgRet.createGraphics();
            gr.setBackground(Color.WHITE);
            gr.clearRect(0, 0, box, box);
            gr = null;
        }
        g.drawImage(bufferedImage, box / 2 - bufferedImage.getWidth() / 2,
                box / 2 - bufferedImage.getHeight() / 2, bufferedImage.getWidth(), bufferedImage.getHeight(),
                null);
        g = null;
        AffineTransform at = new AffineTransform();
        at.rotate(angle * Math.PI / 180.0, box / 2.0, box / 2.0);
        BufferedImageOp bio;
        bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        return bio.filter(imgFree, imgRet);
    }
}

From source file:org.apache.fop.render.pdf.PDFBoxAdapterTestCase.java

@Test
public void testXform() throws Exception {
    PDFDocument pdfdoc = new PDFDocument("");
    pdfdoc.getFilterMap().put(PDFFilterList.DEFAULT_FILTER, Arrays.asList("null"));
    pdfdoc.setMergeFontsEnabled(true);//from  w  w w.  j  a v  a 2  s .c o m
    PDFPage pdfpage = new PDFPage(new PDFResources(pdfdoc), 0, r, r, r, r);
    pdfpage.setDocument(pdfdoc);
    pdfpage.setObjectNumber(1);
    Map<Integer, PDFArray> pageNumbers = new HashMap<Integer, PDFArray>();
    PDFBoxAdapter adapter = new PDFBoxAdapter(pdfpage, new HashMap(), pageNumbers);
    PDDocument doc = getResource(XFORM);
    PDPage page = (PDPage) doc.getDocumentCatalog().getPages().get(0);
    AffineTransform at = new AffineTransform();
    Rectangle r = new Rectangle(0, 1650, 842000, 595000);
    adapter.createStreamFromPDFBoxPage(doc, page, "key", at, new FontInfo(), r);
    doc.close();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    pdfdoc.output(bos);
    Assert.assertFalse(bos.toString("UTF-8").contains("/W 5 /H 5 /BPC 8 /CS /RGB ID "));
}