135 lines
4.7 KiB
Java
135 lines
4.7 KiB
Java
import java.awt.*;
|
|
import java.awt.geom.*;
|
|
|
|
/**
|
|
* An example for a shear transformation applied to a rectangle. The drawing is carried out
|
|
* w.r.t. standard coordinates, not w.r.t. window coordinates.
|
|
*
|
|
* @author Frank Klawonn
|
|
* Last change 07.01.2005
|
|
*/
|
|
public class ShearingExample extends Frame
|
|
{
|
|
private int windowHeight;
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param height The window height.
|
|
*/
|
|
ShearingExample(int height)
|
|
{
|
|
//Enables the closing of the window.
|
|
addWindowListener(new MyFinishWindow());
|
|
windowHeight = height;
|
|
}
|
|
|
|
|
|
public void paint(Graphics g)
|
|
{
|
|
Graphics2D g2d = (Graphics2D) g;
|
|
/*yUp defines a translation allowing the specification of objects in "real"
|
|
coordinates where the y-axis points upwards and the origin of the coordinate
|
|
system is located in the lower left corner of the window.
|
|
*/
|
|
AffineTransform yUp = new AffineTransform();
|
|
yUp.setToScale(1,-1);
|
|
AffineTransform translate = new AffineTransform();
|
|
translate.setToTranslation(40,windowHeight-50);
|
|
yUp.preConcatenate(translate);
|
|
//Apply the transformation to the Graphics2D object to draw everything
|
|
//in "real" coordinates.
|
|
g2d.transform(yUp);
|
|
//The lines should have a thickness of 3.0 instead of 1.0.
|
|
g2d.setStroke(new BasicStroke(3.0f));
|
|
//Generate and draw a rectangle to which the shear
|
|
//transformation will applied later on.
|
|
Rectangle2D.Double rect = new Rectangle2D.Double(60,120,100,40);
|
|
g2d.draw(rect);
|
|
//Define the shear transformation.
|
|
AffineTransform shear = new AffineTransform();
|
|
shear.setToShear(1,0);
|
|
//Draw the transformed rectangle with dashed lines.
|
|
g2d.setStroke(new BasicStroke(3.0f,BasicStroke.CAP_BUTT,
|
|
BasicStroke.JOIN_BEVEL,8.0f,
|
|
new float[] {20.0f, 10.0f},4.0f));
|
|
g2d.draw(shear.createTransformedShape(rect));
|
|
g2d.setStroke(new BasicStroke(1.0f));
|
|
//Draw a coordinate system.
|
|
drawSimpleCoordinateSystem(320,200,g2d);
|
|
}
|
|
|
|
|
|
/**
|
|
* Draws a coordinate system.
|
|
*
|
|
* @param xmax x-coordinate to which the x-axis should extend.
|
|
* @param ymax y-coordinate to which the y-axis should extend.
|
|
* @param g2d Graphics2D object for drawing.
|
|
*/
|
|
public static void drawSimpleCoordinateSystem(int xmax, int ymax,
|
|
Graphics2D g2d)
|
|
{
|
|
int xOffset = 0;
|
|
int yOffset = 0;
|
|
int step = 20;
|
|
String s;
|
|
//Remember the actual font.
|
|
Font fo = g2d.getFont();
|
|
//Use a small font.
|
|
int fontSize = 13;
|
|
Font fontCoordSys = new Font("serif",Font.PLAIN,fontSize);
|
|
/*
|
|
Unfortunately, the transformation yUp applied to the Graphics2D object
|
|
will cause the letters to occur upside down. Therefore, generate an
|
|
upside down font which will appear correctly when drawn upside down.
|
|
*/
|
|
//To make the font upside down, a reflection w.r.t. the x-axis is needed.
|
|
AffineTransform flip = new AffineTransform();
|
|
flip.setToScale(1,-1);
|
|
//Shift the font back to the baseline after reflection.
|
|
AffineTransform lift = new AffineTransform();
|
|
lift.setToTranslation(0,fontSize);
|
|
flip.preConcatenate(lift);
|
|
//Generate the font with the letters upside down.
|
|
Font fontUpsideDown = fontCoordSys.deriveFont(flip);
|
|
g2d.setFont(fontUpsideDown);
|
|
//x-axis
|
|
g2d.drawLine(xOffset,yOffset,xmax,yOffset);
|
|
//Marks and labels for the x-axis.
|
|
for (int i=xOffset+step; i<=xmax; i=i+step)
|
|
{
|
|
g2d.drawLine(i,yOffset-2,i,yOffset+2);
|
|
g2d.drawString(String.valueOf(i),i-7,yOffset-30);
|
|
}
|
|
//y-axis
|
|
g2d.drawLine(xOffset,yOffset,xOffset,ymax);
|
|
//Marks and labels for the y-axis.
|
|
s=" "; //for indention of numbers < 100
|
|
for (int i=yOffset+step; i<=ymax; i=i+step)
|
|
{
|
|
g2d.drawLine(xOffset-2,i,xOffset+2,i);
|
|
if (i>99)
|
|
{
|
|
s="";
|
|
}
|
|
g2d.drawString(s+String.valueOf(i),xOffset-25,i-20);
|
|
}
|
|
//Reset to the original font.
|
|
g2d.setFont(fo);
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] argv)
|
|
{
|
|
int height = 300;
|
|
ShearingExample f = new ShearingExample(height);
|
|
f.setTitle("Example: shear transformation");
|
|
f.setSize(400,height);
|
|
f.setVisible(true);
|
|
}
|
|
|
|
}
|
|
|