![]() |
Lahko pa izvajamo bolj zapletene transformacije (naprimer zrcaljenje slike s preslikavo čez premico) z direktno manipulacijo nizov, ki kontrolirajo transformacijo.
Poznamo dva osnovna načina uporabe transformacij. Ustvarimo lahko AffineTransform objekt, mu določimo lastnosti in ga 'položimo' na Graphics2D objekt s proceduro setTransform. Drugi način pa je, da kličemo željeno transformacijo direktno na Graphics2D objekt.
import java.awt.*;
public class DrawExample5 extends DrawExample4 {
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.clearRect(0, 0,
getSize().width, getSize().height);
drawFilledCircle(g2d);
drawHollowCircle(g2d);
g2d.setPaint(Color.black);
// 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.drawString("Java", 0, 0);
}
}
public static void main(String[] args) {
WindowUtil.openInJFrame(new DrawExample5(),
380, 400);
}
}

import com.sun.java.swing.*;
import java.awt.*;
import java.awt.geom.*;
public class Shear extends JPanel {
private static double gap=10, width=100;
private Rectangle2D.Double rect =
new Rectangle2D.Double(gap, gap, 100, 100);
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.clearRect(0, 0,
getSize().width, getSize().height);
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.0*gap + width, 0.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.";
WindowUtil.openInJFrame(new Shear(),
(int)(20.0*gap + 5.0*width),
(int)(5.0*gap + width),
title);
}
}

avtor: Filip
Božič
december,
1998