Java AffineTransform setToRotation(double theta)

PreviousNext

Java AffineTransform setToRotation(double theta) Sets this transform to a rotation transformation.

Introduction

Sets this transform to a rotation transformation.

The matrix representing this transform becomes:

[ cos(theta) -sin(theta) 0 ]
[ sin(theta) cos(theta)  0 ]
[ 0          0           1 ]

Rotating by a positive angle theta rotates points on the positive X axis toward the positive Y axis.

Syntax

The method setToRotation() from AffineTransform is declared as:

public void setToRotation(double theta)

Parameter

The method setToRotation() has the following parameter:

  • double theta - the angle of rotation measured in radians

Example

The following code shows how to use Java AffineTransform setToRotation(double theta)

Example 1

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class BasicDraw {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new MyComponent());
        frame.setSize(300, 300);//   w w    w.    de    m  o2    s  .c    o m 
        frame.setVisible(true);
    }
}

class MyComponent extends JComponent {
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        g2d.drawString("aString", 100, 100);
        AffineTransform at = new AffineTransform();
        at.setToRotation(Math.PI / 4.0);
        g2d.setTransform(at);
        g2d.drawString("aString", 100, 100);

    }
}

Example 2

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class BasicDraw {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new MyComponent());
        frame.setSize(300, 300);//   w   ww   .  d   em  o    2 s .   c o  m  
        frame.setVisible(true);
    }
}

class MyComponent extends JComponent {
    public void paint(Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        AffineTransform at = new AffineTransform();
        at.setToRotation(Math.PI / 4.0);
        g2d.setTransform(at);
        g2d.drawString("aString", 200, 100);

    }
}

Example 3

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;

import javax.swing.JComponent;
import javax.swing.JFrame;

public class BasicDraw {
    public static void main(String[] args) {
        new BasicDraw();
    }//  w   w w  .  d    e  mo  2  s    . c   o m 

    BasicDraw() {
        JFrame frame = new JFrame();

        frame.add(new MyComponent());

        frame.setSize(300, 300);
        frame.setVisible(true);
    }

}

class MyComponent extends JComponent {
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;

        g2d.drawString("aString", 100, 100);
        AffineTransform at = new AffineTransform();
        at.setToRotation(Math.PI / 4.0);
        g2d.setTransform(at);
        g2d.drawString("aString", 100, 100);

    }
}
PreviousNext

Related