1

当我尝试对当前的 g2d 对象应用旋转时,它不会旋转它,而是将它呈现在同一个位置(在我的上下文中位于另一个之上)。根据我对旋转方法的理解,它将转换应用于当前图形上下文,转换其后出现的任何渲染的像素(这可能是我出错的地方)。这是有问题的代码:

@Override
  public void paint(final Graphics graphics) {
    super.paint(graphics);
    final Graphics2D g2d = (Graphics2D) graphics;
    ....
    ....
    g2d.setColor(Color.RED);
    g2d.setStroke(new BasicStroke(SMALL_LINE_THICKNESS));
    if (isLattice1Drawn) {
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
      // lattice1 and lattice2 are Polygon objects
      g2d.draw(lattice1);
      // This fades in the second Polygon over the first
      g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
      // This line should rotate it, but doesn't
      g2d.rotate(Math.toRadians(210));
      g2d.draw(lattice2);
      .....

谢谢,迈克

编辑 1 作为 Jeff 的建议,我尝试只进行旋转和绘画,留下以下代码:

@Override
public void paint(final Graphics graphics) {
  super.paint(graphics);
  final Graphics2D g2d = (Graphics2D) graphics;
  g2d.rotate(Math.toRadians(210));
  g2d.draw(lattice2);
  return;
  // Rest of paint .................

不幸的是,这没有帮助,任何其他建议都将受到欢迎。

编辑2:当我不调用旋转时,会渲染多边形,但是当我什么都不做时。谁能解释一下?

4

1 回答 1

3

我从编辑 2中了解到的是:旋转确实有效。但是,由于旋转是围绕原点进行的,所以多边形的旋转坐标最终会超出可见区域。您可以通过旋转较小的度数来测试它。

然后,如果所需的操作是围绕其质心旋转多边形,请改用以下 Graphics2D 方法:

void rotate(double theta, double x, double y) 
于 2012-07-18T17:25:52.763 回答