Example usage for java.awt.geom AffineTransform getRotateInstance

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

Introduction

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

Prototype

public static AffineTransform getRotateInstance(double theta, double anchorx, double anchory) 

Source Link

Document

Returns a transform that rotates coordinates around an anchor point.

Usage

From source file:AffineTransformGetRotateInstance.java

public void paint(Graphics g) {
    AffineTransform atrans = null;

    Graphics2D g2d = (Graphics2D) g;
    atrans = AffineTransform.getRotateInstance(Math.PI / 4, 50, 50);

    if (atrans != null)
        g2d.setTransform(atrans);// w  w w  .j  a  va  2  s .c  o  m

    g2d.fillRect(50, 50, 100, 50);
}

From source file:Main.java

public void paint(Graphics g) {
    Shape shape = new Rectangle2D.Float(100, 50, 80, 80);

    Graphics2D g2 = (Graphics2D) g;

    AffineTransform at = AffineTransform.getRotateInstance(0.2, -Math.PI / 6, -Math.PI / 6);

    g2.setTransform(at);// ww  w .j a v a2  s  .  com
    g2.draw(shape);

}

From source file:MainClass.java

public void paint(Graphics g) {
    Shape shape = new Rectangle2D.Float(100, 50, 80, 80);

    Graphics2D g2 = (Graphics2D) g;

    float cm = 72 / 2.54f;
    AffineTransform at = AffineTransform.getRotateInstance(-Math.PI / 6, 3 * cm, 2 * cm);

    g2.setTransform(at);/*from  w w w .  j  a v  a 2s  . c o m*/
    g2.draw(shape);

}

From source file:Main.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g.create();
    String text = "I don't see the problem";
    FontMetrics fm = g2d.getFontMetrics();
    int x = (getWidth() - fm.stringWidth(text)) / 2;
    int y = ((getHeight() - fm.getHeight()) / 2) + fm.getDescent();
    g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(45), getWidth() / 2, getHeight() / 2));
    g2d.drawString(text, x, y);/*from   ww  w  . j a va 2 s.  c om*/
    g2d.dispose();
}

From source file:Main.java

/**
 * Rotates a shape about the specified coordinates.
 *
 * @param base  the shape (<code>null</code> permitted, returns
 *              <code>null</code>).
 * @param angle  the angle (in radians).
 * @param x  the x coordinate for the rotation point (in Java2D space).
 * @param y  the y coordinate for the rotation point (in Java2D space).
 *
 * @return the rotated shape.//ww w. ja va  2  s. c  o  m
 */
public static Shape rotateShape(final Shape base, final double angle, final float x, final float y) {
    if (base == null) {
        return null;
    }
    final AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y);
    final Shape result = rotate.createTransformedShape(base);
    return result;
}

From source file:Main.java

/**
 * Draws a shape with the specified rotation about <code>(x, y)</code>.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param shape  the shape (<code>null</code> not permitted).
 * @param angle  the angle (in radians).
 * @param x  the x coordinate for the rotation point.
 * @param y  the y coordinate for the rotation point.
 *//*from  w ww  .ja v a 2 s  . c  o  m*/
public static void drawRotatedShape(final Graphics2D g2, final Shape shape, final double angle, final float x,
        final float y) {

    final AffineTransform saved = g2.getTransform();
    final AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y);
    g2.transform(rotate);
    g2.draw(shape);
    g2.setTransform(saved);

}

From source file:TransformTranslatedRotation.java

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

    // Use antialiasing.
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Move the origin to 75, 75.
    AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
    g2.transform(at);/* w  w w. j  av a 2s.  co m*/

    // Draw the shapes in their original locations.
    g2.setPaint(Color.black);
    g2.draw(axes);
    g2.draw(shape);

    // Transform the Graphics2D.
    float cm = 72 / 2.54f;
    g2.transform(AffineTransform.getRotateInstance(-Math.PI / 6, 3 * cm, 2 * cm));

    // Draw the "new" shapes in dashed.
    Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 },
            0);
    g2.setStroke(stroke);
    g2.draw(axes);
    g2.draw(shape);
}

From source file:Sampler.java

private void createTransformations() {
    AffineTransform at;//from   www .j  a v a 2 s  .c om
    at = AffineTransform.getRotateInstance(Math.PI / 6, 0, 285);
    mOps.put("Rotate nearest neighbor", new AffineTransformOp(at, null));

    RenderingHints rh = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    mOps.put("Rotate bilinear", new AffineTransformOp(at, rh));

    at = AffineTransform.getScaleInstance(.5, .5);
    mOps.put("Scale .5, .5", new AffineTransformOp(at, null));

    at = AffineTransform.getRotateInstance(Math.PI / 6);
    mOps.put("Rotate bilinear (origin)", new AffineTransformOp(at, rh));
}

From source file:ExtendedGeneralPath.java

/**
 * Adds an elliptical arc, defined by two radii, an angle from the
 * x-axis, a flag to choose the large arc or not, a flag to
 * indicate if we increase or decrease the angles and the final
 * point of the arc./*from w  w  w. j av  a 2 s .c o m*/
 *
 * @param rx the x radius of the ellipse
 * @param ry the y radius of the ellipse
 *
 * @param angle the angle from the x-axis of the current
 * coordinate system to the x-axis of the ellipse in degrees.
 *
 * @param largeArcFlag the large arc flag. If true the arc
 * spanning less than or equal to 180 degrees is chosen, otherwise
 * the arc spanning greater than 180 degrees is chosen
 *
 * @param sweepFlag the sweep flag. If true the line joining
 * center to arc sweeps through decreasing angles otherwise it
 * sweeps through increasing angles
 *
 * @param x the absolute x coordinate of the final point of the arc.
 * @param y the absolute y coordinate of the final point of the arc.
 */
public synchronized void arcTo(float rx, float ry, float angle, boolean largeArcFlag, boolean sweepFlag,
        float x, float y) {

    // Ensure radii are valid
    if (rx == 0 || ry == 0) {
        lineTo(x, y);
        return;
    }

    checkMoveTo(); // check if prev command was moveto

    // Get the current (x, y) coordinates of the path
    double x0 = cx;
    double y0 = cy;
    if (x0 == x && y0 == y) {
        // If the endpoints (x, y) and (x0, y0) are identical, then this
        // is equivalent to omitting the elliptical arc segment entirely.
        return;
    }

    Arc2D arc = computeArc(x0, y0, rx, ry, angle, largeArcFlag, sweepFlag, x, y);
    if (arc == null)
        return;

    AffineTransform t = AffineTransform.getRotateInstance(Math.toRadians(angle), arc.getCenterX(),
            arc.getCenterY());
    Shape s = t.createTransformedShape(arc);
    path.append(s, true);

    makeRoom(7);
    types[numSeg++] = ExtendedPathIterator.SEG_ARCTO;
    values[numVals++] = rx;
    values[numVals++] = ry;
    values[numVals++] = angle;
    values[numVals++] = largeArcFlag ? 1 : 0;
    values[numVals++] = sweepFlag ? 1 : 0;
    cx = values[numVals++] = x;
    cy = values[numVals++] = y;
}

From source file:ImageProcessingTest.java

public ImageProcessingFrame() {
    setTitle("ImageProcessingTest");
    setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

    add(new JComponent() {
        public void paintComponent(Graphics g) {
            if (image != null)
                g.drawImage(image, 0, 0, null);
        }//from   w  ww . j a v  a  2s . c o m
    });

    JMenu fileMenu = new JMenu("File");
    JMenuItem openItem = new JMenuItem("Open");
    openItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            openFile();
        }
    });
    fileMenu.add(openItem);

    JMenuItem exitItem = new JMenuItem("Exit");
    exitItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            System.exit(0);
        }
    });
    fileMenu.add(exitItem);

    JMenu editMenu = new JMenu("Edit");
    JMenuItem blurItem = new JMenuItem("Blur");
    blurItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            float weight = 1.0f / 9.0f;
            float[] elements = new float[9];
            for (int i = 0; i < 9; i++)
                elements[i] = weight;
            convolve(elements);
        }
    });
    editMenu.add(blurItem);

    JMenuItem sharpenItem = new JMenuItem("Sharpen");
    sharpenItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 5.f, -1.0f, 0.0f, -1.0f, 0.0f };
            convolve(elements);
        }
    });
    editMenu.add(sharpenItem);

    JMenuItem brightenItem = new JMenuItem("Brighten");
    brightenItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            float a = 1.1f;
            // float b = 20.0f;
            float b = 0;
            RescaleOp op = new RescaleOp(a, b, null);
            filter(op);
        }
    });
    editMenu.add(brightenItem);

    JMenuItem edgeDetectItem = new JMenuItem("Edge detect");
    edgeDetectItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            float[] elements = { 0.0f, -1.0f, 0.0f, -1.0f, 4.f, -1.0f, 0.0f, -1.0f, 0.0f };
            convolve(elements);
        }
    });
    editMenu.add(edgeDetectItem);

    JMenuItem negativeItem = new JMenuItem("Negative");
    negativeItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            short[] negative = new short[256 * 1];
            for (int i = 0; i < 256; i++)
                negative[i] = (short) (255 - i);
            ShortLookupTable table = new ShortLookupTable(0, negative);
            LookupOp op = new LookupOp(table, null);
            filter(op);
        }
    });
    editMenu.add(negativeItem);

    JMenuItem rotateItem = new JMenuItem("Rotate");
    rotateItem.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            if (image == null)
                return;
            AffineTransform transform = AffineTransform.getRotateInstance(Math.toRadians(5),
                    image.getWidth() / 2, image.getHeight() / 2);
            AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
            filter(op);
        }
    });
    editMenu.add(rotateItem);

    JMenuBar menuBar = new JMenuBar();
    menuBar.add(fileMenu);
    menuBar.add(editMenu);
    setJMenuBar(menuBar);
}