Example usage for java.awt Graphics2D rotate

List of usage examples for java.awt Graphics2D rotate

Introduction

In this page you can find the example usage for java.awt Graphics2D rotate.

Prototype

public abstract void rotate(double theta, double x, double y);

Source Link

Document

Concatenates the current Graphics2D Transform with a translated rotation transform.

Usage

From source file:Main.java

public static BufferedImage create(BufferedImage image, double angle, GraphicsConfiguration gc) {
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    int transparency = image.getColorModel().getTransparency();
    BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);
    g.drawRenderedImage(image, null);/*  ww w  . j  a v a  2s  .c  o  m*/
    return result;
}

From source file:com.ables.pix.utility.PictureOps.java

public static BufferedImage tilt(BufferedImage image, double rotation) {
    double sin = Math.abs(Math.sin(getAngle()));
    double cos = Math.abs(Math.cos(getAngle()));
    int w = image.getWidth(), h = image.getHeight();

    int neww = (int) Math.floor(w * cos + sin * h), newh = (int) Math.floor(h * cos + sin * w);
    GraphicsConfiguration gc = getDefaultConfiguration();
    BufferedImage rotated = gc.createCompatibleImage(neww, newh);
    Graphics2D g = rotated.createGraphics();
    g.translate((neww - w) / 2, (newh - h / 2));
    g.rotate(getAngle(), w / 2, h / 2);
    g.drawRenderedImage(image, null);//from w ww  .ja v a2 s .c o m
    g.dispose();
    return rotated;
}

From source file:D20140128.ApacheXMLGraphicsTest.EPSExample1.java

/**
 * Creates an EPS file. The contents are painted using a Graphics2D
 * implementation that generates an EPS file.
 *
 * @param outputFile the target file//  w  ww.  ja  v a  2s .com
 * @throws IOException In case of an I/O error
 */
public static void generateEPSusingJava2D(File outputFile) throws IOException {
    OutputStream out = new java.io.FileOutputStream(outputFile);
    out = new java.io.BufferedOutputStream(out);
    try {
        //Instantiate the EPSDocumentGraphics2D instance
        EPSDocumentGraphics2D g2d = new EPSDocumentGraphics2D(false);
        g2d.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());

        //Set up the document size
        g2d.setupDocument(out, 400, 200); //400pt x 200pt

        //Paint a bounding box
        g2d.drawRect(0, 0, 400, 200);

        //A few rectangles rotated and with different color
        Graphics2D copy = (Graphics2D) g2d.create();
        int c = 12;
        for (int i = 0; i < c; i++) {
            float f = ((i + 1) / (float) c);
            Color col = new Color(0.0f, 1 - f, 0.0f);
            copy.setColor(col);
            copy.fillRect(70, 90, 50, 50);
            copy.rotate(-2 * Math.PI / (double) c, 70, 90);
        }
        copy.dispose();

        //Some text
        g2d.rotate(-0.25);
        g2d.setColor(Color.RED);
        g2d.setFont(new Font("sans-serif", Font.PLAIN, 36));
        g2d.drawString("Hello world!", 140, 140);
        g2d.setColor(Color.RED.darker());
        g2d.setFont(new Font("serif", Font.PLAIN, 36));
        g2d.drawString("Hello world!", 140, 180);

        //Cleanup
        g2d.finish();
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:net.sf.mcf2pdf.mcfelements.util.ImageUtil.java

public static BufferedImage readImage(File imageFile) throws IOException {
    int rotation = getImageRotation(imageFile);
    BufferedImage img = ImageIO.read(imageFile);

    if (rotation == 0) {
        return img;
    }/*from  w w  w .ja  v  a2s  .  c om*/

    boolean swapXY = rotation != 180;

    BufferedImage rotated = new BufferedImage(swapXY ? img.getHeight() : img.getWidth(),
            swapXY ? img.getWidth() : img.getHeight(), BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2d = rotated.createGraphics();
    g2d.translate((rotated.getWidth() - img.getWidth()) / 2, (rotated.getHeight() - img.getHeight()) / 2);
    g2d.rotate(Math.toRadians(rotation), img.getWidth() / 2, img.getHeight() / 2);

    g2d.drawImage(img, 0, 0, null);
    g2d.dispose();

    return rotated;
}

From source file:com.sketchy.image.ImageProcessingThread.java

public static BufferedImage rotateImage(BufferedImage image, RotateOption rotateOption) {

    if (rotateOption == RotateOption.ROTATE_NONE)
        return image;

    int degrees = 0;
    int imageWidth = image.getWidth();
    int imageHeight = image.getHeight();

    BufferedImage rotatedImage = null;
    if (rotateOption == RotateOption.ROTATE_90) {
        degrees = 90;/*from  w  ww  . j  a  va2 s .  c  o  m*/
        rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB);
    } else if (rotateOption == RotateOption.ROTATE_180) {
        degrees = 180;
        rotatedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB);
    } else if (rotateOption == RotateOption.ROTATE_270) {
        degrees = 270;
        rotatedImage = new BufferedImage(imageHeight, imageWidth, BufferedImage.TYPE_INT_ARGB);
    }

    Graphics2D g = rotatedImage.createGraphics();
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g.rotate(Math.toRadians(degrees), 0, 0);

    if (degrees == 90) {
        g.drawImage(image, null, 0, -imageHeight);
    } else if (degrees == 180) {
        g.drawImage(image, null, -imageWidth, -imageHeight);
    } else if (degrees == 270) {
        g.drawImage(image, null, -imageWidth, 0);
    }
    g.dispose();
    return rotatedImage;
}

From source file:Main.java

public void paint(Graphics g) {
    g.fillRect(0, 0, 20, 20);/*from w  ww . j  a v a  2s .c o  m*/

    Graphics2D g2 = (Graphics2D) g;

    g2.translate(50, 50);
    g2.rotate(30.0 * Math.PI / 180.0, 13, 12);

    g2.scale(2.0, 2.0);

    g.setColor(Color.red);

    g.fillRect(0, 0, 20, 20);
}

From source file:de.romankreisel.faktotum.beans.BundesbruderBean.java

public void rotateProfilePictureClockwise(BundesbruderEntity bundesbruder, double angle) throws IOException {
    BufferedImage image = this.getImageFromByteArray(bundesbruder.getPictureOriginal());
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    GraphicsConfiguration gc = image.createGraphics().getDeviceConfiguration();
    BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);
    g.drawRenderedImage(image, null);/*  w  w w  . jav a  2 s  .  co  m*/
    g.dispose();
    this.setProfilePicture(bundesbruder, this.storeImageToByteArray(image));
}

From source file:de.uni_siegen.wineme.come_in.thumbnailer.thumbnailers.PDFBoxThumbnailer.java

private BufferedImage convertToImage(final PDPage page, final int imageType, final int thumbWidth,
        final int thumbHeight)/*     */ throws IOException
/*     */ {//from  ww w .  jav  a  2s  .c o  m
    /* 707 */ final PDRectangle mBox = page.findMediaBox();
    /* 708 */ final float widthPt = mBox.getWidth();
    /* 709 */ final float heightPt = mBox.getHeight();
    /* 711 */ final int widthPx = thumbWidth;
    // Math.round(widthPt * scaling);
    /* 712 */ final int heightPx = thumbHeight;
    // Math.round(heightPt * scaling);
    /* 710 */ final double scaling = Math.min((double) thumbWidth / widthPt, (double) thumbHeight / heightPt);
    // resolution / 72.0F;
    /*     */
    /* 714 */ final Dimension pageDimension = new Dimension((int) widthPt, (int) heightPt);
    /*     */
    /* 716 */ final BufferedImage retval = new BufferedImage(widthPx, heightPx, imageType);
    /* 717 */ final Graphics2D graphics = (Graphics2D) retval.getGraphics();
    /* 718 */ graphics.setBackground(PDFBoxThumbnailer.TRANSPARENT_WHITE);
    /* 719 */ graphics.clearRect(0, 0, retval.getWidth(), retval.getHeight());
    /* 720 */ graphics.scale(scaling, scaling);
    /* 721 */ final PageDrawer drawer = new PageDrawer();
    /* 722 */ drawer.drawPage(graphics, page, pageDimension);
    /*     */ try
    /*     */ {
        /* 728 */ final int rotation = page.findRotation();
        /* 729 */ if (rotation == 90 || rotation == 270)
        /*     */ {
            /* 731 */ final int w = retval.getWidth();
            /* 732 */ final int h = retval.getHeight();
            /* 733 */ final BufferedImage rotatedImg = new BufferedImage(w, h, retval.getType());
            /* 734 */ final Graphics2D g = rotatedImg.createGraphics();
            /* 735 */ g.rotate(Math.toRadians(rotation), w / 2, h / 2);
            /* 736 */ g.drawImage(retval, null, 0, 0);
            /*     */ }
        /*     */ }
    /*     */ catch (final ImagingOpException e)
    /*     */ {
        /* 741 */ //log.warn("Unable to rotate page image", e);
        /*     */ }
    /*     */
    /* 744 */ return retval;
    /*     */ }

From source file:image.writer.ImageWriterExample1.java

/**
 * Paints a few things on a Graphics2D instance.
 * @param g2d the Graphics2D instance/*w  w w  .j  a va  2 s  .c o m*/
 * @param pageNum a page number
 */
protected void paintSome(Graphics2D g2d, int pageNum) {
    //Paint a bounding box
    g2d.drawRect(0, 0, 400, 200);

    //A few rectangles rotated and with different color
    Graphics2D copy = (Graphics2D) g2d.create();
    int c = 12;
    for (int i = 0; i < c; i++) {
        float f = ((i + 1) / (float) c);
        Color col = new Color(0.0f, 1 - f, 0.0f);
        copy.setColor(col);
        copy.fillRect(70, 90, 50, 50);
        copy.rotate(-2 * Math.PI / (double) c, 70, 90);
    }
    copy.dispose();

    //Some text
    copy = (Graphics2D) g2d.create();
    copy.rotate(-0.25);
    copy.setColor(Color.RED);
    copy.setFont(new Font("sans-serif", Font.PLAIN, 36));
    copy.drawString("Hello world!", 140, 140);
    copy.setColor(Color.RED.darker());
    copy.setFont(new Font("serif", Font.PLAIN, 36));
    copy.drawString("Hello world!", 140, 180);
    copy.dispose();

    //Try attributed text
    AttributedString aString = new AttributedString("This is attributed text.");
    aString.addAttribute(TextAttribute.FAMILY, "SansSerif");
    aString.addAttribute(TextAttribute.FAMILY, "Serif", 8, 18);
    aString.addAttribute(TextAttribute.FOREGROUND, Color.orange, 8, 18);
    g2d.drawString(aString.getIterator(), 250, 170);

    g2d.drawString("Page: " + pageNum, 250, 190);
}

From source file:Bouncer.java

protected void setTransform(Graphics2D g2) {
    if (mTransform == false)
        return;/*  w w w .j  ava2 s.  co m*/
    Dimension d = getSize();
    g2.rotate(mTheta, d.width / 2, d.height / 2);
}