Example usage for java.awt.geom AffineTransform scale

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

Introduction

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

Prototype

@SuppressWarnings("fallthrough")
public void scale(double sx, double sy) 

Source Link

Document

Concatenates this transform with a scaling transformation.

Usage

From source file:BasicShapes.java

public static void main(String[] args) {
    AffineTransform tx = new AffineTransform();
    tx.scale(1, 1);
    Rectangle shape = new Rectangle(1, 1, 1, 1);
    Shape newShape = tx.createTransformedShape(shape);

}

From source file:Main.java

public static void main(String[] args) {
    AffineTransform tx = new AffineTransform();
    tx.scale(1, 1);
    Rectangle shape = new Rectangle(1, 1, 1, 1);
    Shape newShape = tx.createTransformedShape(shape);

    System.out.println("done");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    AffineTransform tx = new AffineTransform();
    tx.scale(1, 2);

    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
    bufferedImage = op.filter(bufferedImage, null);
}

From source file:Main.java

private static AffineTransform createRandomTransform(double angleRad) {
    AffineTransform at = new AffineTransform();
    double scale = 1.0;
    at.translate(randomDouble(), randomDouble());
    scale = Math.abs(randomDouble());
    at.scale(scale, scale);
    at.rotate(angleRad);//w  w w  .j a  v  a  2s . co  m
    at.translate(randomDouble(), randomDouble());
    scale = Math.abs(randomDouble());
    at.scale(scale, scale);
    return at;
}

From source file:org.jfree.graphics2d.demo.ImageTest.java

private static void drawArcTest(Graphics2D g2) {
    g2.setPaint(Color.GREEN);//from   w w  w .  j ava2  s  .c om
    g2.drawRect(0, 20, 70, 50);
    g2.setPaint(Color.RED);
    Path2D path1 = new Path2D.Double();
    double[] pts = calculateReferenceArc(90);
    path1.moveTo(pts[0], pts[1]);
    path1.curveTo(pts[2], pts[3], pts[4], pts[5], pts[6], pts[7]);
    AffineTransform t = new AffineTransform();
    t.translate(35, 45);
    t.scale(35, 25);
    t.rotate(Math.PI / 4);
    path1.transform(t);
    g2.draw(path1);

    Path2D path2 = new Path2D.Double();
    path2.moveTo(pts[0], pts[1]);
    path2.curveTo(pts[2], pts[3], pts[4], pts[5], pts[6], pts[7]);
    AffineTransform t2 = new AffineTransform();
    t2.rotate(3 * Math.PI / 4);
    t2.scale(35, 25);
    t2.translate(35, 35);
    path2.transform(t2);
    //g2.draw(path2);
    Path2D arc = new Path2D.Double();
    arc.append(path1, false);
    arc.append(path2, false);
    //g2.draw(arc);
    //g2.draw(path1);
    //g2.transform(t);
    g2.setPaint(Color.BLUE);
    g2.drawArc(0, 20, 70, 50, 0, -270);
    //Arc2D arc2d = new Arc2D.Double(0d, 20d, 70d, 50d, 0d, 90, Arc2D.OPEN);
    //g2.draw(arc2d);
}

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./* w  w w  .j av  a 2s.  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:net.sf.ginp.util.GinpUtil.java

/**
 * Take a jpeg from an input stream and write it to an output.
 * stream with a scaled width and height
 * @param sos output stream for image//from   w w  w  .  ja  v  a2  s  .co  m
 * @param is input stream for image
 * @param width width
 * @param height height
 * @throws IOException if there is an error writing or reading
 */
public static void writeScaledImageToStream(final OutputStream sos, final InputStream is, final int width,
        final int height) throws IOException {
    JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(is);
    BufferedImage origImage = decoder.decodeAsBufferedImage();
    int origHeight = origImage.getHeight(null);
    int origWidth = origImage.getWidth(null);
    int scaledW = 0;
    int scaledH = 0;
    double scaleW = 1.0;
    double scaleH = 1.0;

    // close input stream
    is.close();

    // Calculate scale factors
    if (width == 0) {
        scaleW = (double) height / (double) origHeight;
        scaleH = (double) height / (double) origHeight;
    } else if (height == 0) {
        scaleW = (double) width / (double) origWidth;
        scaleH = (double) width / (double) origWidth;
    } else {
        scaleW = (double) width / (double) origWidth;
        scaleH = (double) height / (double) origHeight;
    }

    scaledW = (int) (scaleW * origWidth);
    scaledH = (int) (scaleH * origHeight);

    BufferedImage outImage = new BufferedImage(scaledW, scaledH, BufferedImage.TYPE_INT_RGB);
    AffineTransform tx = new AffineTransform();

    tx.scale(scaleW, scaleH);

    AffineTransformOp af = new AffineTransformOp(tx, null);

    af.filter(origImage, outImage);

    JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);

    encoder.encode(outImage);
}

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

private static void syncHandleM2V(final boolean outTurn, final Affine handle) {
    final AffineTransform t = handle.getAffine();
    t.setToIdentity();/*w  ww.  j  a v a2  s . c o  m*/
    if (outTurn)
        t.scale(-1, 1);
    handle.setAffine(t);
}

From source file:com.mikenimer.familydam.services.photos.ThumbnailService.java

/**
 * get the right transformation based on the orientation setting in the exif metadata. In case the physical image is actually
 * stored in a rotated state./*  ww w  .  j a v  a  2s  . c  o m*/
 *
 * @param orientation
 * @param width
 * @param height
 * @return
 */
public static AffineTransform getExifTransformation(int orientation, double width, double height, double scaleW,
        double scaleH) {

    AffineTransform t = new AffineTransform();

    switch (orientation) {
    case 1:
        t.scale(scaleW, scaleH);
        break;
    case 2: // Flip X //todo: test & fix
        t.scale(-scaleW, scaleH);
        t.translate(-width, 0);
        break;
    case 3: // PI rotation
        t.translate(width, height);
        t.rotate(Math.PI);
        t.scale(scaleW, scaleH);
        break;
    case 4: // Flip Y //todo: test & fix
        t.scale(scaleW, -scaleH);
        t.translate(0, -height);
        break;
    case 5: // - PI/2 and Flip X
        t.rotate(-Math.PI / 2);
        t.scale(-scaleW, scaleH);
        break;
    case 6: // -PI/2 and -width
        t.translate(height, 0);
        t.rotate(Math.PI / 2);
        t.scale(scaleW, scaleH);
        break;
    case 7: // PI/2 and Flip //todo:test & fix
        t.scale(-scaleW, scaleH);
        t.translate(-height, 0);
        t.translate(0, width);
        t.rotate(3 * Math.PI / 2);
        break;
    case 8: // PI / 2
        t.translate(0, width);
        t.rotate(3 * Math.PI / 2);
        t.scale(scaleW, scaleH);
        break;
    }

    return t;
}

From source file:de.bund.bfr.jung.JungUtils.java

static <V, E> Shape getTransformedEdgeShape(RenderContext<V, E> rc, Layout<V, E> layout, E e) {
    Graph<V, E> graph = layout.getGraph();
    edu.uci.ics.jung.graph.util.Pair<V> endpoints = graph.getEndpoints(e);
    V v1 = endpoints.getFirst();//w  w  w.  jav a 2  s . com
    V v2 = endpoints.getSecond();

    if (!rc.getEdgeIncludePredicate().evaluate(Context.<Graph<V, E>, E>getInstance(graph, e))
            || !rc.getVertexIncludePredicate().evaluate(Context.<Graph<V, E>, V>getInstance(graph, v1))
            || !rc.getVertexIncludePredicate().evaluate(Context.<Graph<V, E>, V>getInstance(graph, v2))) {
        return null;
    }

    Point2D p1 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v1));
    Point2D p2 = rc.getMultiLayerTransformer().transform(Layer.LAYOUT, layout.transform(v2));
    float x1 = (float) p1.getX();
    float y1 = (float) p1.getY();
    float x2 = (float) p2.getX();
    float y2 = (float) p2.getY();
    Shape edgeShape = rc.getEdgeShapeTransformer().transform(Context.getInstance(graph, e));
    AffineTransform edgeShapeTransform = AffineTransform.getTranslateInstance(x1, y1);

    if (v1.equals(v2)) {
        Rectangle2D bounds = rc.getVertexShapeTransformer().transform(v1).getBounds2D();

        edgeShapeTransform.scale(bounds.getWidth(), bounds.getHeight());
        edgeShapeTransform.translate(0, -edgeShape.getBounds2D().getWidth() / 2);
    } else {
        float dx = x2 - x1;
        float dy = y2 - y1;

        edgeShapeTransform.rotate(Math.atan2(dy, dx));
        edgeShapeTransform.scale(Math.sqrt(dx * dx + dy * dy), 1.0);
    }

    return edgeShapeTransform.createTransformedShape(edgeShape);
}