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:org.evors.rs.ui.sandpit.SandPitCamera.java

/**
 * Get the value of transform//from www.  jav  a2s . com
 *
 * @return the value of transform
 */
public AffineTransform getTransform() {
    AffineTransform returnTransform = new AffineTransform();
    Vector2D currentPosScreenCoord = new Vector2D(getScale(), getCurrentPosWorldCoord());
    Vector2D halfWindow = getHalfWindowSize();
    returnTransform.translate(currentPosScreenCoord.getX() + halfWindow.getX(),
            currentPosScreenCoord.getY() + halfWindow.getY());
    returnTransform.scale(getScale(), -getScale());

    return returnTransform;
}

From source file:Utils.java

public static Shape generatePolygon(int sides, int outsideRadius, int insideRadius) {

    if (sides < 3) {
        return new Ellipse2D.Float(0, 0, 10, 10);
    }//from   w  ww . j a v a  2  s  . c o  m

    AffineTransform trans = new AffineTransform();
    Polygon poly = new Polygon();
    for (int i = 0; i < sides; i++) {
        trans.rotate(Math.PI * 2 / (float) sides / 2);
        Point2D out = trans.transform(new Point2D.Float(0, outsideRadius), null);
        poly.addPoint((int) out.getX(), (int) out.getY());
        trans.rotate(Math.PI * 2 / (float) sides / 2);
        if (insideRadius > 0) {
            Point2D in = trans.transform(new Point2D.Float(0, insideRadius), null);
            poly.addPoint((int) in.getX(), (int) in.getY());
        }
    }

    return poly;
}

From source file:com.github.fritaly.dualcommander.Utils.java

public static int getTimestampRenderWidth() {
    final String text = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
    final Font font = Utils.getDefaultFont();
    final FontRenderContext context = new FontRenderContext(new AffineTransform(), true, true);

    return (int) font.getStringBounds(text, context).getWidth();
}

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);// w  w w.j a  v a2s.c om
    at.shear(-0.5, 0.0);
    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:RotateImage45Degrees.java

public RotateImage45Degrees(String imageFile) {
    addNotify();/*from  w  w  w  . ja  v a 2 s. co  m*/
    frameInsets = getInsets();
    inputImage = Toolkit.getDefaultToolkit().getImage(imageFile);

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(inputImage, 0);
    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
    }

    sourceBI = new BufferedImage(inputImage.getWidth(null), inputImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = (Graphics2D) sourceBI.getGraphics();
    g.drawImage(inputImage, 0, 0, null);

    AffineTransform at = new AffineTransform();

    // scale image
    at.scale(2.0, 2.0);

    // rotate 45 degrees around image center
    at.rotate(45.0 * Math.PI / 180.0, sourceBI.getWidth() / 2.0, sourceBI.getHeight() / 2.0);

    /*
     * translate to make sure the rotation doesn't cut off any image data
     */
    AffineTransform translationTransform;
    translationTransform = findTranslation(at, sourceBI);
    at.preConcatenate(translationTransform);

    // instantiate and apply affine transformation filter
    BufferedImageOp bio;
    bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

    destinationBI = bio.filter(sourceBI, null);

    int frameInsetsHorizontal = frameInsets.right + frameInsets.left;
    int frameInsetsVertical = frameInsets.top + frameInsets.bottom;
    setSize(destinationBI.getWidth() + frameInsetsHorizontal, destinationBI.getHeight() + frameInsetsVertical);
    show();
}

From source file:org.jcurl.model.PathSegmentTest.java

public void testTrafo() throws NoninvertibleTransformException {
    final Point2D x0 = new Point2D.Double(1.5, 2.5);
    // final Point2D x0 = new Point2D.Double(0, 0);
    final Point2D v0 = new Point2D.Double(2, 1);
    // build the trafo
    final AffineTransform rc2wc = new AffineTransform();
    {/*from w ww.ja  v a 2s .  c  om*/
        rc2wc.rotate(-Math.acos((v0.getX() * 0 + v0.getY() * 1) / v0.distance(0, 0)), x0.getX(), x0.getY());
        rc2wc.translate(x0.getX(), x0.getY());
    }
    // check some points.
    // wc(x0) -> rc(0,0)
    Point2D tmp = rc2wc.inverseTransform(x0, null);
    assertEquals("", 0, tmp.getX(), 1e-9);
    assertEquals("", 0, tmp.getY(), 1e-9);

    // rc(0,1) -> wc(x0)
    tmp = rc2wc.transform(new Point2D.Double(0, 1), null);
    assertEquals("", x0.getX() + 0.8944271909999, tmp.getX(), 1e-6);
    assertEquals("", x0.getY() + 0.4472135954999, tmp.getY(), 1e-6);
}

From source file:org.jcurl.core.base.CurveTransformedTest.java

public void testAffineTransformMemoryLayout() {
    Rock r = new RockDouble(1, 2, 3);
    AffineTransform at = new AffineTransform();
    final double[] d = new double[6];
    at.getMatrix(d);/*from w  w w  . java  2 s  .  c  om*/
    assertEquals("", 1.0, d[0], 1e-9);
    assertEquals("", 0.0, d[1], 1e-9);
    assertEquals("", 0.0, d[2], 1e-9);
    assertEquals("", 1.0, d[3], 1e-9);
    assertEquals("", 0.0, d[4], 1e-9);
    assertEquals("", 0.0, d[5], 1e-9);
    at.transform(r = new RockDouble(2, 3, 4), r);
    assertEquals(2, 3, 4, r, 1e-9);

    at = AffineTransform.getScaleInstance(0.5, 0.75);
    at.getMatrix(d);
    assertEquals("", 0.5, d[0], 1e-9);
    assertEquals("", 0.0, d[1], 1e-9);
    assertEquals("", 0.0, d[2], 1e-9);
    assertEquals("", 0.75, d[3], 1e-9);
    assertEquals("", 0.0, d[4], 1e-9);
    assertEquals("", 0.0, d[5], 1e-9);
    at.transform(r = new RockDouble(2, 3, 4), r);
    assertEquals(1, 2.25, 4, r, 1e-9);

    at = AffineTransform.getTranslateInstance(0.5, 0.75);
    at.getMatrix(d);
    assertEquals("", 1.0, d[0], 1e-9);
    assertEquals("", 0.0, d[1], 1e-9);
    assertEquals("", 0.0, d[2], 1e-9);
    assertEquals("", 1.0, d[3], 1e-9);
    assertEquals("", 0.5, d[4], 1e-9);
    assertEquals("", 0.75, d[5], 1e-9);
    at.transform(r = new RockDouble(2, 3, 4), r);
    assertEquals(2.5, 3.75, 4, r, 1e-9);
}

From source file:de.mfo.jsurf.grid.RotationGrid.java

public static BufferedImage renderAnimGrid(int xAngleMin, int xAngleMax, int xSteps, int yAngleMin,
        int yAngleMax, int ySteps) {
    BufferedImage grid = new BufferedImage(ySteps * size, xSteps * size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) grid.getGraphics();
    for (int x = 0; x < xSteps; ++x) {
        double xAngle = xAngleMin + (xAngleMax - xAngleMin) * (xSteps == 1 ? 0.5 : (x / (double) (xSteps - 1)));
        Matrix4d matRotX = new Matrix4d();
        matRotX.setIdentity();//from  w w w. jav  a  2s  .  c om
        matRotX.rotX(Math.toRadians(xAngle));
        for (int y = 0; y < ySteps; ++y) {
            double yAngle = yAngleMin
                    + (yAngleMax - yAngleMin) * (ySteps == 1 ? 0.5 : (y / (double) (ySteps - 1)));
            Matrix4d matRotY = new Matrix4d();
            matRotY.setIdentity();
            matRotY.rotY(Math.toRadians(yAngle));
            additional_rotation.mul(matRotY, matRotX);
            BufferedImage bi = createBufferedImageFromRGB(draw(size, size, aam, aap));
            g2.drawImage(bi,
                    new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
                    (ySteps - 1 - y) * size, x * size);
        }
    }
    return grid;
}

From source file:FontPaint.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    setBackground(Color.white);/*from w  w  w.j  a va 2  s  . c  om*/
    int width = getSize().width;
    int height = getSize().height;
    Graphics2D g2 = (Graphics2D) g;

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    FontRenderContext frc = g2.getFontRenderContext();
    Font f = new Font("Helvetica", 1, 60);
    String s = new String("Java Source and Support.");
    TextLayout textTl = new TextLayout(s, f, frc);
    AffineTransform transform = new AffineTransform();
    Shape outline = textTl.getOutline(null);
    Rectangle outlineBounds = outline.getBounds();
    transform = g2.getTransform();
    transform.translate(width / 2 - (outlineBounds.width / 2), height / 2 + (outlineBounds.height / 2));
    g2.transform(transform);
    g2.setColor(Color.blue);
    g2.draw(outline);
    g2.setClip(outline);
}

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

/**
 * Resizes and overwrites the image. Will keep the original pictures dimensions.
 * //from w w w  . j  av  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);

}