【问题标题】:Java Canvas - Rectangle2D Scales when MovingJava Canvas - Rectangle2D 移动时缩放
【发布时间】:2015-10-16 11:42:53
【问题描述】:

我正在画布上绘制一系列矩形。矩形应该以一定角度移动。出于某种原因,当它们移动时,它们会按比例放大:

xPos += xSpeed;
yPos += ySpeed;
updateBounds(xPos, yPos, width, height);

我的 UpdateBounds 方法:

public void updateBounds(double x, double y, double w, double h) {
    bounds.setRect(x, y, w, h);
}

Bounds 是一个 Rectangle2D 对象。 还有我的绘图方法:

g.fillRect((int) bounds.getX(), (int) bounds.getY(),
                (int) bounds.getMaxX(), (int) bounds.getMaxY());

为什么我会出现这种行为?

【问题讨论】:

  • 什么是bounds 变量类?
  • 代码中的widthheight 变量是否有任何变化,而您的问题中没有包含这些变化?
  • 没有。发生在边界上的一切。如代码所示。

标签: java java-canvas


【解决方案1】:

Graphics.fillRect() 接受宽度和高度参数,而不是要绘制的矩形的最大 x 和 y 位置。

fillRect的第三个和第四个参数应该是Rectangle2D的getWidth()getHeight()

作为参考,link to what getMaxX() would give you

【讨论】:

  • 不错的答案,没有提到 +1
猜你喜欢
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-05
  • 1970-01-01
  • 1970-01-01
  • 2012-09-15
相关资源
最近更新 更多