Java2D: An Introduction and Tutorial
7. Coordinate Transformations in Java2D
7.1 Coordinate Transformations: Overview
Java 1.2 allows you to translate, rotate, scale, or shear the coordinate system.
Think of drawing similar shapes in different locations and at different angles and scales by moving, rotating, or stretching the piece of paper, drawing onto it at the same location and scale as you'd draw an untransformed image, then transforming the sheet of paper back (along with the image on it).
This is very convenient: it is often much easier to move the coordinate system than to calculate new coordinates for each of your points.
Besides, for some data structures like ellipses and strings there is no other way to get rotated or stretched versions.
The meanings of translate, rotate, and scale are clear:
to move, to spin, or to stretch/shrink evenly in the x and/or y direction.
Shear means to stretch unevenly: an x shear moves points to the right based on how far they are from the y axis;
a y shear moves points down based on how far they are from the x axis.
You can also perform more complex transformations (e.g. creating a mirror image by flipping around a line) by directly manipulating the underlying arrays that control the transformations.
There are two basic ways to use transformations.
You can create an AffineTransform object, set its parameters, and then assign that AffineTransform to the Graphics2D object via setTransform.
Alternatively, for the basic transformations you can call translate, rotate, scale, and shear directly on the Graphics2D object.
7.2 Coordinate Translations and Rotations: Example Code
Download the source:
RotationExample.java
(plus StrokeThicknessExample.java,
FontExample.java,
GradientPaintExample.java,
ShapeExample.java,
WindowUtilities.java,
and ExitListener.java if you don't have them from the previous examples).
import java.awt.*;
/** An example of coordinate translations and
* rotations with Java2D in Java 1.2.
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class RotationExample extends StrokeThicknessExample {
private Color[] colors = { Color.white, Color.black };
public void paintComponent(Graphics g) {
clear(g);
Graphics2D g2d = (Graphics2D)g;
drawGradientCircle(g2d);
drawThickCircleOutline(g2d);
// Move the origin to the center of the circle.
g2d.translate(185.0, 185.0);
for (int i=0; i<16; i++) {
// Rotate the coordinate system around current
// origin, which is at the center of the circle.
g2d.rotate(Math.PI/8.0);
g2d.setPaint(colors[i%2]);
g2d.drawString("Java", 0, 0);
}
}
public static void main(String[] args) {
WindowUtilities.openInJFrame(new RotationExample(), 380, 400);
}
}
7.3 Coordinate Translations and Rotations: Example Output
7.4 Shear Transformations
If you specify a non-zero x shear, then x values will be more and more shifted to the right the farther they are away from the y axis.
For example, an x shear of 0.1 means that the x value will be shifted 10% of the distance the point is away from the y axis.
Y shears are similar: points are shifted down in proportion to the distance they are away from the x axis.
7.5 Shear Transformations: Example Code
Download the source:
ShearExample.java
(plus WindowUtilities.java
and ExitListener.java if you don't have them from the previous examples).
import com.sun.java.swing.*;
import java.awt.*;
import java.awt.geom.*;
/** An example of shear transformations with Java2D in Java 1.2.
* 1998 Marty Hall, http://www.apl.jhu.edu/~hall/java/
*/
public class ShearExample extends JPanel {
private static int gap=10, width=100;
private Rectangle rect = new Rectangle(gap, gap, 100, 100);
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
for (int i=0; i<5; i++) {
g2d.setPaint(Color.red);
g2d.fill(rect);
// Each new square gets 0.2 more x shear
g2d.shear(0.2, 0.0);
g2d.translate(2*gap + width, 0);
}
}
public static void main(String[] args) {
String title =
"Shear: x shear ranges from 0.0 for the leftmost 'square' " +
"to 0.8 for the rightmost one.";
WindowUtilities.openInJFrame(new ShearExample(),
20*gap + 5*width, 5*gap + width,
title);
}
}
7.6 Shear Transformations: Example Output
Next ->
This tutorial was prepared by Marty Hall for work in the Research and Technology Development Center of the Johns Hopkins University Applied Physics Lab, for courses in the Johns Hopkins Part-Time MS Program in Computer Science, and for various industry seminars and courses.
© 1998 Marty Hall.Java 1.2beta4 version.
This article first appeared in November, 1998 and is reprinted with permission of the author.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|