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



Home Java Example Java Swing Graphics2D Show Shearing property of class AffineTransform

Related Tutorials


 
 

Share on Google+Share on Google+

Show Shearing property of class AffineTransform

Advertisement
To show the shearing, we have used Ellipse2D class to draw the oval. The method getTranslateInstance() returns the translation transformation.

Show Shearing property of class AffineTransform

     

This section shows the shearing property.

To show the shearing, we have used Ellipse2D class to draw the oval. The method getTranslateInstance() returns the translation transformation. The method draw(shape) draws the specified shape in the original location. The method shear(-.8, 0) performs a shearing transformation.

 

 

 

Following code transform the Graphics2D:

AffineTransform affineTransform2 = AffineTransform.getTranslateInstance(0, 0);
affineTransform2.shear(-.8, 0);
g2d.transform(affineTransform2);

The stylistic representation of the outline for the specified shape has been given by the interface Stroke. To outline the graphics primitives, which are rendered with an object of Graphics2D, we have used BasicStroke class, which defines a set of rendering attributes. The BasicStroke.CAP_BUTT gives the dash segments. The BasicStroke.JOIN_BEVEL joins the path segments by connecting the outer corners of their wide outlines with a straight segment.

 Following code draws the specified shape in dashed format:

Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 }, 0);
g2d.setStroke(stroke);
g2d.draw(shape);

The method setStroke() sets the stroke settings for the Graphics2D context, when you draw the shape.

 Here is the code of ShearWithTransform.java

import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;

public class ShearWithTransform extends JComponent {
  Shape shape;
  
 public ShearWithTransform() {
  shape = create();
  }
  private Shape create() {
  return new Ellipse2D.Double(1005010050);
  }
  public void paint(Graphics g) {
  Graphics2D g2d = (Graphics2D) g;
  AffineTransform affineTransform1 = AffineTransform.
  getTranslateInstance(
00);
  g2d.transform(affineTransform1);
  g2d.setPaint(Color.red);
  g2d.draw(shape);

  AffineTransform affineTransform2 = AffineTransform.
  getTranslateInstance(
00);
  affineTransform2.shear(-.80);
  g2d.transform(affineTransform2);
  g2d.transform(AffineTransform.getTranslateInstance(100100));
  
  Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
  BasicStroke.JOIN_BEVEL, 0new float[] { 3}, 0);
  g2d.setStroke(stroke);
  g2d.draw(shape);
  }
  public static void main(String[] a) {
  JFrame frame = new JFrame("Shear the oval");
  frame.getContentPane().add(new ShearWithTransform());
  frame.setSize(300250);
  frame.show();
  }
}

Output will be displayed as:

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 22, 2008

Related Tutorials

Discuss: Show Shearing property of class AffineTransform  

Post your Comment


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