0
我目前正在努力與一些矩形的旋轉。 我有一個由9個小矩形組成的矩形。 http://i57.tinypic.com/msn8ue.jpg旋轉矩形和調整位置
現在我旋轉大矩形相應的仿射變換,它工作得很好。 http://i61.tinypic.com/25phlqp.jpg
我的問題是,我現在需要移動矩形,以便每個矩形的左上角位置在點x,y。
我不知道該怎麼做,我希望你能幫助我。
謝謝!
public class MainClass {
public static void main(String[] args) {
JFrame jf = new JFrame("Demo");
Container cp = jf.getContentPane();
MyCanvas tl = new MyCanvas();
cp.add(tl);
jf.setSize(800, 800);
jf.setVisible(true);
}
}
class MyCanvas extends JComponent {
private static final long serialVersionUID = 5703217428905757134L;
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int x = 400;
int y = 400;
int width = 100;
int height = 200;
final int OVERLAPPING_OUTLINE = 10;
Rectangle[] rect = new Rectangle[9];
rect[0] = new Rectangle(x + OVERLAPPING_OUTLINE, y + OVERLAPPING_OUTLINE, width - 2 * OVERLAPPING_OUTLINE, height - 2
* OVERLAPPING_OUTLINE);
rect[1] = new Rectangle(x - OVERLAPPING_OUTLINE, y - OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2,
OVERLAPPING_OUTLINE * 2);
rect[2] = new Rectangle(x - OVERLAPPING_OUTLINE + width, y - OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2,
OVERLAPPING_OUTLINE * 2);
rect[3] = new Rectangle(x - OVERLAPPING_OUTLINE, y + height - OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2,
OVERLAPPING_OUTLINE * 2);
rect[4] = new Rectangle(x - OVERLAPPING_OUTLINE + width, y + height - OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2,
OVERLAPPING_OUTLINE * 2);
rect[5] = new Rectangle(x - OVERLAPPING_OUTLINE, y + OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2, height
- OVERLAPPING_OUTLINE * 2);
rect[6] = new Rectangle(x - OVERLAPPING_OUTLINE + width, y + OVERLAPPING_OUTLINE, OVERLAPPING_OUTLINE * 2, height
- OVERLAPPING_OUTLINE * 2);
rect[7] = new Rectangle(x + OVERLAPPING_OUTLINE, y - OVERLAPPING_OUTLINE, width - OVERLAPPING_OUTLINE * 2,
2 * OVERLAPPING_OUTLINE);
rect[8] = new Rectangle(x + OVERLAPPING_OUTLINE, y + height - OVERLAPPING_OUTLINE, width - OVERLAPPING_OUTLINE * 2,
2 * OVERLAPPING_OUTLINE);
for (Rectangle r : rect)
g2.draw(r);
g2.setColor(Color.RED);
AffineTransform af = new AffineTransform();
for (int i = 90; i < 360; i += 90) {
af.rotate(Math.toRadians(i), x - OVERLAPPING_OUTLINE, y - OVERLAPPING_OUTLINE);
for (Rectangle r : rect) {
Shape shape = af.createTransformedShape(r);
g2.draw(shape);
}
}
}
}