translate Example
The coordinate system of the graphics context can be transformed using translate(). Here a rectangle is rendered with the unchanged coordinate system. Then a translation is applied which moves the origin of the graphics context from 0,0 to 50,80.
The same rectangle, with the same top-left coordinates, is rendered again in blue. This time however, its position is changed by the translation parameters.
// Translate Example
import javax.swing.*;
import java.awt.geom.*;
import java.awt.*;
class TranslateExample extends JApplet {
@Override public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.blue);
g2.setStroke(new BasicStroke(10));
Rectangle2D r = new Rectangle2D.Double(100, 100, 100, 100);
g2.draw(r);
g2.translate(50, 80);
g2.setPaint(Color.red);
g2.draw(r);
}
}
