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);

Source Link

Document

Concatenates the current Graphics2D Transform with a rotation transform.

Usage

From source file:com.funambol.foundation.util.MediaUtils.java

/**
 * Rotates given buffered image by given amount of degree.
 * The valid degree values are 0, 90, 180, 270.
 * If the image is a jpg, the rotation is lossless, exif data are preserved
 * and image size is almost the same.//from w  w w  .  j  a  v  a2  s  .  c  o  m
 *
 * @param bufImage the buffered image
 * @param degree amount of degree to apply
 * @return a buffered image containing rotated image data
 * @throws PicturesException if amount of degree is invalid or if an
 *         IOException occurs
 */
private static BufferedImage rotateImage(BufferedImage bufImage, int degree)
        throws FileDataObjecyUtilsException {

    degree = degree % 360;
    int h;
    int w;

    switch (degree) {
    case 0:
    case 180:
        h = bufImage.getHeight();
        w = bufImage.getWidth();
        break;
    case 90:
    case 270:
        h = bufImage.getWidth();
        w = bufImage.getHeight();
        break;
    default:
        throw new FileDataObjecyUtilsException(
                "Error rotating image since the '" + degree + "' degree value is unsupported");
    }

    BufferedImage out = null;

    int bufImageType = bufImage.getType();
    if (BufferedImage.TYPE_BYTE_INDEXED == bufImageType || BufferedImage.TYPE_BYTE_BINARY == bufImageType) {

        IndexColorModel model = (IndexColorModel) bufImage.getColorModel();
        out = new BufferedImage(w, h, bufImage.getType(), model);

    } else if (BufferedImage.TYPE_CUSTOM == bufImageType) {

        // we don't know what type of image it can be

        // there's a bug in some VM that cause some PNG images to have 
        // type custom: this should take care of this issue

        //check if we need to have alpha channel
        boolean alpha = bufImage.getTransparency() != BufferedImage.OPAQUE;

        if (alpha) {
            // TYPE_INT_ARGB_PRE gives you smaller output images
            // than TYPE_INT_ARGB
            out = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB_PRE);
        } else {
            out = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        }

    } else {

        out = new BufferedImage(w, h, bufImage.getType());
    }

    Graphics2D g2d = out.createGraphics();

    Map renderingHints = new HashMap();

    renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    renderingHints.put(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    g2d.setRenderingHints(renderingHints);
    g2d.rotate(Math.toRadians(degree));

    switch (degree) {
    case 90:
        g2d.translate(0, -w);
        break;
    case 180:
        g2d.translate(-w, -h);
        break;
    case 270:
        g2d.translate(-h, 0);
        break;
    }

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

    return out;
}

From source file:SwingTimerBasedAnimationScaleRotate.java

public void paint(Graphics g) {
    int h = getHeight();
    int w = getWidth();

    Graphics2D g2d = (Graphics2D) g;

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

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

    g2d.translate(w / 2, h / 2);//from  w w w . jav  a2s  .  co m
    g2d.rotate(angle);
    g2d.scale(scale, scale);

    g2d.fill(r);
}

From source file:Main.java

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

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

    int cx = getSize().width / 2;
    int cy = getSize().height / 2;

    g2.translate(cx, cy);//from  ww w .  j  av a2s  . c  om
    g2.rotate(theta * Math.PI / 180);

    Shape oldClip = g2.getClip();
    Shape e = new Ellipse2D.Float(-cx, -cy, cx * 2, cy * 2);
    g2.clip(e);

    Shape c = new Ellipse2D.Float(-cx, -cy, cx * 3 / 4, cy * 2);
    g2.setPaint(new GradientPaint(40, 40, Color.blue, 60, 50, Color.white, true));
    g2.fill(c);

    g2.setPaint(Color.yellow);
    g2.fillOval(cx / 4, 0, cx, cy);

    g2.setClip(oldClip);

    g2.setFont(new Font("Times New Roman", Font.PLAIN, 64));
    g2.setPaint(new GradientPaint(-cx, 0, Color.red, cx, 0, Color.black, false));
    g2.drawString("Hello, 2D!", -cx * 3 / 4, cy / 4);

    AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) .75);
    g2.setComposite(ac);

    Shape r = new RoundRectangle2D.Float(0, -cy * 3 / 4, cx * 3 / 4, cy * 3 / 4, 20, 20);
    g2.setStroke(new BasicStroke(4));
    g2.setPaint(Color.magenta);
    g2.fill(r);
    g2.setPaint(Color.green);
    g2.draw(r);

    g2.drawImage(image, -cx / 2, -cy / 2, this);
}

From source file:juicebox.track.EigenvectorTrack.java

private void drawRotatedString(Graphics2D g2, String string, float x, float y) {
    AffineTransform orig = g2.getTransform();
    g2.rotate(0);
    g2.setColor(Color.BLUE);//from   www  . j a v  a 2 s. c o m
    g2.translate(x, 0);
    g2.scale(-1, 1);
    g2.translate(-x, 0);
    g2.drawString(string, x, y);
    g2.setTransform(orig);
}

From source file:org.shredzone.commons.captcha.impl.DefaultCaptchaGenerator.java

/**
 * Draws a single character./*from   w w w  .j a  v  a2  s .c om*/
 *
 * @param g2d
 *            {@link Graphics2D} context
 * @param ch
 *            character to draw
 * @param x
 *            left x position of the character
 * @param boxWidth
 *            width of the box
 */
private void drawCharacter(Graphics2D g2d, char ch, int x, int boxWidth) {
    double degree = (rnd.nextDouble() * rotationAmplitude * 2) - rotationAmplitude;
    double scale = 1 - (rnd.nextDouble() * scaleAmplitude / 100);

    Graphics2D cg2d = (Graphics2D) g2d.create();
    cg2d.setFont(font.deriveFont(fontSize));

    cg2d.translate(x + (boxWidth / 2), height / 2);
    cg2d.rotate(degree * PI / 90);
    cg2d.scale(scale, scale);

    FontMetrics fm = cg2d.getFontMetrics();
    int charWidth = fm.charWidth(ch);
    int charHeight = fm.getAscent() + fm.getDescent();

    cg2d.drawString(String.valueOf(ch), -(charWidth / 2), fm.getAscent() - (charHeight / 2));

    cg2d.dispose();
}

From source file:RotationAboutCenter.java

protected void paintComponent(Graphics g) {
    Graphics2D g2d;
    g2d = (Graphics2D) g.create();

    // Erase background to white
    g2d.setColor(Color.WHITE);/*ww  w .  j a  v  a2 s  .c om*/
    g2d.fillRect(0, 0, getWidth(), getHeight());

    // base rectangle
    g2d.setColor(Color.GRAY.brighter());
    g2d.fillRect(50, 50, 50, 50);

    // rotated 45 degrees around origin
    g2d.rotate(Math.toRadians(45));
    g2d.setColor(Color.GRAY.darker());
    g2d.fillRect(50, 50, 50, 50);

    // rotated 45 degrees about center of rect
    g2d = (Graphics2D) g.create();
    g2d.rotate(Math.toRadians(45), 75, 75);
    g2d.setColor(Color.BLACK);
    g2d.fillRect(50, 50, 50, 50);

    // done with g2d, dispose it
    g2d.dispose();
}

From source file:TextBouncer.java

protected void setTransform(Graphics2D g2) {
    Dimension d = getSize();//from   www .ja v a 2 s . com
    int cx = d.width / 2;
    int cy = d.height / 2;
    g2.translate(cx, cy);
    if (mShear)
        g2.shear(mShearX, mShearY);
    if (mRotate)
        g2.rotate(mTheta);
    g2.translate(-cx, -cy);
}

From source file:no.met.jtimeseries.chart.XYWindArrowRenderer.java

/**
 * Draws the visual representation of a single wind arrow.
 *//*  w  w w  .  java2 s  .  c  o  m*/
@Override
public void drawItem(Graphics2D g2d, XYItemRendererState state, Rectangle2D plotArea, PlotRenderingInfo info,
        XYPlot plot, ValueAxis domainAxis, ValueAxis rangeAxis, XYDataset dataset, int series, int item,
        CrosshairState crosshairState, int pass) {
    resizeArrowByPlotHeight((int) plotArea.getHeight());
    // Needs a new graphics object to use translate() and rotate()
    Graphics2D g2 = (Graphics2D) g2d.create();
    g2.setRenderingHints(renderHints);
    RectangleEdge domainAxisLocation = plot.getDomainAxisEdge();
    double middleY = plotArea.getCenterY();

    WindDataset windData = (WindDataset) dataset;

    Number x = windData.getX(series, item);
    Number windDir = windData.getWindDirection(series, item);
    Number wforce = windData.getWindForce(series, item);

    double middleX = domainAxis.valueToJava2D(x.doubleValue(), plotArea, domainAxisLocation);

    g2.translate((int) middleX, (int) middleY);
    g2.setColor(Color.BLACK);

    if (wforce.doubleValue() <= zeroWindLimit) {
        drawCircle(g2);
    } else {
        g2.rotate(Math.toRadians(windDir.doubleValue() - 180));
        drawArrow(g2, wforce.doubleValue());

        if (useArrowHead) {
            g2.fill(getPolygonHead(arrowHeadSize, arrowHeight));
        } else {
            g2.draw(getCircleHead(arrowHeadSize, arrowHeight));
        }
    }
}

From source file:convcao.com.caoAgent.convcaoNeptusInteraction.java

@Override
public void paint(Graphics2D g, StateRenderer2D renderer) {
    Point2D center = renderer.getScreenPosition(coords.squareCenter);
    double width = renderer.getZoom() * coords.cellWidth * coords.numCols;
    double height = renderer.getZoom() * coords.cellWidth * coords.numRows;
    g.setColor(new Color(0, 0, 255, 64));
    g.translate(center.getX(), center.getY());
    g.rotate(-renderer.getRotation());
    g.fill(new Rectangle2D.Double(-width / 2, -height / 2, width, height));
    g.rotate(renderer.getRotation());/*  www  .jav a2  s  .  com*/
    g.translate(-center.getX(), -center.getY());

    if (!active)
        return;

    g.setColor(Color.orange);
    int pos = 50;
    for (String v : nameTable.values()) {
        g.drawString(v + ": " + depths.get(v) + "m", 15, pos);
        pos += 20;
    }

    for (String vehicle : nameTable.values()) {
        LocationType src = positions.get(vehicle);
        LocationType dst = destinations.get(vehicle);

        if (!arrived.get(vehicle))
            g.setColor(Color.red.darker());
        else
            g.setColor(Color.green.darker());
        float dash[] = { 4.0f };
        g.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, dash, 0.0f));
        g.draw(new Line2D.Double(renderer.getScreenPosition(src), renderer.getScreenPosition(dst)));

        Point2D dstPt = renderer.getScreenPosition(dst);

        if (!arrived.get(vehicle))
            g.setColor(Color.red.darker());
        else
            g.setColor(Color.green.darker());

        g.fill(new Ellipse2D.Double(dstPt.getX() - 4, dstPt.getY() - 4, 8, 8));
    }
}

From source file:convcao.com.agent.ConvcaoNeptusInteraction.java

@Override
public void paint(Graphics2D g2, StateRenderer2D renderer) {
    Graphics2D g = (Graphics2D) g2.create();

    Point2D center = renderer.getScreenPosition(coords.squareCenter);
    double width = renderer.getZoom() * coords.cellWidth * coords.numCols;
    double height = renderer.getZoom() * coords.cellWidth * coords.numRows;
    g.setColor(new Color(0, 0, 255, 64));
    g.translate(center.getX(), center.getY());
    g.rotate(-renderer.getRotation());
    g.fill(new Rectangle2D.Double(-width / 2, -height / 2, width, height));
    g.rotate(renderer.getRotation());/*  w ww .j  a v a2s .  co m*/
    g.translate(-center.getX(), -center.getY());

    if (!active) {
        g.dispose();
        return;
    }

    g.setColor(Color.orange);
    int pos = 50;
    for (String v : nameTable.values()) {
        g.drawString(v + ": " + depths.get(v) + "m", 15, pos);
        pos += 20;
    }

    for (String vehicle : nameTable.values()) {
        LocationType src = positions.get(vehicle);
        LocationType dst = destinations.get(vehicle);

        if (!arrived.get(vehicle))
            g.setColor(Color.red.darker());
        else
            g.setColor(Color.green.darker());
        float dash[] = { 4.0f };
        g.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER, 5.0f, dash, 0.0f));
        g.draw(new Line2D.Double(renderer.getScreenPosition(src), renderer.getScreenPosition(dst)));

        Point2D dstPt = renderer.getScreenPosition(dst);

        if (!arrived.get(vehicle))
            g.setColor(Color.red.darker());
        else
            g.setColor(Color.green.darker());

        g.fill(new Ellipse2D.Double(dstPt.getX() - 4, dstPt.getY() - 4, 8, 8));
    }

    g.dispose();
}