【问题标题】:How to synchronize rotation on multiple images in JavaFX Canvas如何在 JavaFX Canvas 中同步多个图像的旋转
【发布时间】:2016-04-17 11:49:58
【问题描述】:

我有多个要一起旋转绘制的对象。当它们在 0 度时(正对),它们是好的,但当它们不是时,它看起来像这样:

我正在使用此代码进行旋转:

private void rotate(GraphicsContext gc, double angle, double px, double py) {
    Rotate r = new Rotate(angle, px, py);
    gc.setTransform(r.getMxx(), r.getMyx(), r.getMxy(), r.getMyy(), r.getTx(), r.getTy());
}

private void drawRotatedImage(GraphicsContext gc, Image image, double angle, double tlpx, double tlpy) {
    gc.save(); // saves the current state on stack, including the current transform
    rotate(gc, angle, tlpx + image.getWidth() / 2, tlpy + image.getHeight() / 2);
    gc.drawImage(image, tlpx, tlpy);
    gc.restore(); // back to original state (before rotation)
}

我有一组图像,我使用一个统一的角度调用drawRotatedImage 来旋转它们。所有图片都是32 x 32。 如何解决图像中的这种变化?

【问题讨论】:

  • 无法重现问题。如果我测试它,代码会围绕它们各自的中心旋转图像......
  • @fabian 有一个度数变量。有一个使用该变量显示的图像列表。它们都是 32 x 32。它们有一个特定的 xy 坐标可供绘制。我以该坐标绘制它们并旋转图像。我怎样才能使它成为一个整体旋转?

标签: java image javafx


【解决方案1】:

要将图像作为一组旋转,只需对所有这些图像使用相同的转换:

代替

gc.save();
gc.setTransform(new Affine(new Rotate(angle, image1CenterX, image1CenterY)));
gc.drawImage(image1, image1X, image1Y);
gc.restore();

gc.save();
gc.setTransform(new Affine(new Rotate(angle, image2CenterX, image2CenterY)));
gc.drawImage(image2, image2X, image2Y);
gc.restore();

...

(这就是你正在做的,如果你为每张图片调用drawRotatedImage

使用

gc.save();
gc.setTransform((new Affine(new Rotate(angle, rotationCenterX, rotationCenterY)));

gc.drawImage(image1, image1unrotatedX, image1unrotatedY);
gc.drawImage(image2, image2unrotatedX, image2unrotatedY);
...

gc.restore();

【讨论】:

  • rotationCenterXrotationCenterY 会是什么?
  • 成功了,谢谢!我将起始 x 和 y 用作 rotationCenterXrotationCenterY。使用中心会更好吗?还是结束?
  • @theTechnoKid 你想围绕某个点旋转所有节点。你需要什么点取决于你。如果你使用P=(x,y) 作为枢轴点(=旋转中心),你会得到相同的效果就像您将绘制的对象移动了-P,围绕原点旋转并将结果移动P。只需决定在旋转期间哪个点应该保持位置(假设在该位置绘制了一些东西)。合理的选择轴心点可以是网格线的交点,网格场的中心(图像在旋转 90° 倍数后与网格场对齐)。如有疑问,请使用网格的中心。
猜你喜欢
  • 2013-08-18
  • 2015-08-06
  • 2017-06-05
  • 1970-01-01
  • 1970-01-01
  • 2011-11-22
  • 1970-01-01
  • 2018-02-23
  • 2020-08-31
相关资源
最近更新 更多