Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Example Java Swing Graphics2D Affine Transform Example

Related Tutorials


 
 

Share on Google+Share on Google+

Affine Transform Example

Advertisement
The sequences of translations, scales, flips, rotations, and shears are provided by the class AffineTransform. To show shearing, we are providing you an example. We have create combo box to show shearing for x-axis and y-axis.

Affine Transform Example

     

This section illustrates you the use of  class AffineTransform.

The sequences of translations, scales, flips, rotations, and shears are provided by the class AffineTransform. To show shearing, we are providing you an example. We have create combo box to show shearing for x-axis and y-axis. The values for shearing is defined. The method setSelectedItem() sets the selected value for the combo boxes shearX, shearY. To perform all the functions ActionListener class is called. 

The method getDefaultToolkit() of class Toolkit provides the default toolkit. The method getImage() returns the image. The MediaTracker class provides a media objects including images. Create an instance of MediaTracker and call addImage() method which adds an image. The method waitForAll() of this class loads the image which is added.

The methods setToShear() of class AffineTransform sets the shearing transformation. The AffineTransformOp class uses the instance of AffineTransform class and performs a linear mapping from source image to destination image. The method filter() of this class transforms the source image(bufferedImage1) and stores the results into the destination image(bufferedImage2).

Here is the code of AffineTransformExample.java

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.geom.AffineTransform;

public class AffineTransformExample extends JFrame {
 ShowLabel label;
  JComboBox shearX, shearY;
  String[] shear = { "0.00""0.25""0.50""0.75""1.00" };

  public AffineTransformExample() {
  super("Affine Transform Example");
  Container container = getContentPane();
  label = new ShowLabel();
  container.add(label);

  JPanel panel = new JPanel();
  panel.setLayout(new GridLayout(2455));
  
  shearX = new JComboBox(shear);
  shearX.setSelectedItem("0.00");
  panel.add(new JLabel("Shear X:"));
  panel.add(shearX);
  shearX.addActionListener(new ComboBoxListener());
 
  shearY = new JComboBox(shear);
  shearY.setSelectedItem("0.00");
    panel.add(new JLabel("Shear Y:"));
  panel.add(shearY);
  shearY.addActionListener(new ComboBoxListener());
  container.add(BorderLayout.NORTH, panel);
  setSize(350,300);
  setVisible(true);
  }
  public static void main(String arg[]) {
  new AffineTransformExample();
  }
  class ComboBoxListener implements ActionListener {
  public void actionPerformed(ActionEvent e) {
  JComboBox box = (JComboBox) e.getSource();
  if (box == shearX) {
  label.shearx = Double.parseDouble((String) box.getSelectedItem());
  label.value(true);
  label.filter();
  label.repaint();
  else if (box == shearY) {
  label.sheary = Double.parseDouble((String) box.getSelectedItem());
  label.value(true);
  label.filter();
  label.repaint();
  }
  }
  }
}
class ShowLabel extends JLabel {
  Image image;
  BufferedImage bufferedImage1, bufferedImage2;
  BufferedImage bufferedImage;
  Graphics2D g2d;
  AffineTransform affineTransform;
  double shearx = 1.0, sheary = 1.0;

  ShowLabel() {
  image = Toolkit.getDefaultToolkit().getImage("image4.jpg");
  MediaTracker mediaTracker = new MediaTracker(this);
  mediaTracker.addImage(image, 1);
  try {
  mediaTracker.waitForAll();
  catch (Exception e) {}
  createImages();
  affineTransform = new AffineTransform();
  }
  public void createImages() {
  bufferedImage1 = new BufferedImage(image.getWidth(this), image
  .getHeight(this), BufferedImage.TYPE_INT_RGB);
  g2d = bufferedImage1.createGraphics();
  g2d.drawImage(image, 00this);
  bufferedImage = bufferedImage1;
  bufferedImage2 = new BufferedImage(image.getWidth(this), image
  .getHeight(this), BufferedImage.TYPE_INT_RGB);
  }
  public void value(boolean scale, boolean shear) {
  if (shear) {
  affineTransform.setToShear(shearx, sheary);
  shear = true;
  }
  }
  public void filter() {
  AffineTransformOp affineTransformOp = new AffineTransformOp
  (affineTransform, 
null);
  Graphics2D G2D = bufferedImage2.createGraphics();
  G2D.clearRect(00, bufferedImage2.getWidth(this), bufferedImage2.
   getHeight(
this));
  affineTransformOp.filter(bufferedImage1, bufferedImage2);
  bufferedImage = bufferedImage2;
  }
  public void paintComponent(Graphics g) {
  super.paintComponent(g);
  Graphics2D g2D = (Graphics2D) g;
  g2D.drawImage(bufferedImage, 00this);
  }
}

Output will be displayed as:

The image will rotate with x-axis and y-axis if you sets shear.

Download Source Code

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 15, 2008

Related Tutorials

Discuss: Affine Transform Example  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com