Coordinate Transformations in Java

Transformations are very useful in practice. They allow you to work with coordinate(x,y) values. The scale method of the Graphics2D class sets the coordinates transformation of the graphics context to a scaling transformation. There are four fundamental transformations 1)Scaling   2)Rotation 3)Translation 4)Shear

 

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import java.awt.geom.*;

public class TransformTest

{

public static void main(String aa[])

{

JFrame frame=new TransformTestFramee();

frame.show();

}}

class TransformTestFramee extends JFrame implements ActionListener

{

public TransformTestFramee()

{

setTitle("TransformTest");

setSize(500,500);

addWindowListener(new WindowAdapter()

{

public void windowClosing(WindowEvent wee)

{

System.exit(0);

}

});

Container contentPane=getContentPane();

canvas=new TransformPanel();

contentPane.add(canvas,"Center");

JPanel buttonPanel=new JPanel();

ButtonGroup group=new ButtonGroup();

rotateButton=new JRadioButton("Rotation",true);

buttonPanel.add(rotateButton);

group.add(rotateButton);

rotateButton.addActionListener(this);

translateButton=new JRadioButton("Translation",false);

buttonPanel.add(translateButton);

group.add(translateButton);

translateButton.addActionListener(this);

scaleButton=new JRadioButton("Scaling",true);

buttonPanel.add(scaleButton);

group.add(scaleButton);

scaleButton.addActionListener(this);

shearButton=new JRadioButton("Shearing",false);

buttonPanel.add(shearButton);

group.add(shearButton);

shearButton.addActionListener(this);

contentPane.add(buttonPanel,"North");

}

public void actionPerformed(ActionEvent aeventt)

{

Object source=aeventt.getSource();

if(source==rotateButton)

canvas.setRotate();

else if(source==translateButton)

canvas.setTranslate();

else if(source==scaleButton)

canvas.setScale();

else if(source==shearButton)

canvas.setShear();

}

private TransformPanel canvas;

private JRadioButton rotateButton;

private JRadioButton translateButton;

private JRadioButton scaleButton;

private JRadioButton shearButton;

}

class TransformPanel extends JPanel

{

public TransformPanel()

{

square=new Rectangle2D.Double(-50,-50,100,100);/* the paintComponent method draws a square that is centered around the origin*/

t=new AffineTransform();

setRotate();

}

public void paintComponent(Graphics gg)

{

super.paintComponent(gg);

Graphics2D g2=(Graphics2D)gg;

g2.translate(getWidth()/2,getHeight()/2);//translation moves the origin to the center of the component

g2.setPaint(Color.gray);

g2.draw(square);

g2.transform(t);//composes current transformation with t(apply temporary transform)

g2.setPaint(Color.black);

g2.draw(square);

}

public void setRotate()

{

t.setToRotation(Math.toRadians(30));//sets t to a rotation

repaint();

}

public void setTranslate()

{

t.setToTranslation(20,15);//sets t to a translation

repaint();

}

public void setScale()

{

t.setToScale(2.0,1.5);//sets t to a scaling

repaint();

}

public void setShear()

{

t.setToShear(-0.2,0);//sets t to a shear

repaint();

}

private Rectangle2D square;

private AffineTransform t;//for Affine transformation

}

Rotation

Translation

Explanation..

Scaling—blowing up or shrinking(small or large ), all distances from a fixed point.

Rotation—rotating all points around a fixed amount.

Translation—moving all points(of the object) by a fixed amount.

Shear—leaving one line fixed and “sliding” the lines parallel to it by an amount that is proportional to the distance from the fixed line.

The Scale, Rotate, Translate, and Shear methods of the Graphics2D class set the coordinates transformation of the graphics context .

Affine Transformations is also called COMPOSING TRANSFORMATION.

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @