Java:矩形宽度和高度

时间:2014-05-12 23:40:11

标签: java

当我绘制如下的矩形

drawRect(0,0,39,39);

宽度和高度(以像素为单位)都是40?当我在我知道的图像上绘制矩形为40x40像素并将此矩形放在它上面时,它非常适合。所以我有点困惑,因为java网站说第3和第4个变量是指宽度和高度,但如果是这种情况它并不真正引用实际宽度,只是实际宽度-1。我觉得我错过了什么或者犯了错误。小号

2 个答案:

答案 0 :(得分:3)

它将是40x40。

http://developer.android.com/reference/android/graphics/Rect.html#Rect(int,int,int,int)

public Rect (int left, int top, int right, int bottom)

Added in API level 1
Create a new rectangle with the specified coordinates. Note: no range checking is performed, so the caller must ensure that left <= right and top <= bottom.

Parameters
left    The X coordinate of the left side of the rectangle
top The Y coordinate of the top of the rectangle
right   The X coordinate of the right side of the rectangle
bottom  The Y coordinate of the bottom of the rectangle

最后2个参数表示右侧和底部,因此如果您从0开始并转到39那个40像素。

答案 1 :(得分:1)

你的问题让两个课程混乱。

  • Java java.awt.Rectangle类有一个构造函数,其中包含您所描述的参数:Rectangle(int x, int y, int width, int height)

  • Android android.graphics.Rect类有一个构造函数,它使用不同的参数:Rect (int left, int top, int right, int bottom)

Java网站上没有记录Rect,因为它不是Java的一部分。如果您在Java站点上找到了文档,则它适用于Rectangle类......这是不同的。


如果您错误地提供Rect参数作为x,y,width,height,则可能会被烧毁。 Rect构造函数的javadoc说:

  

&#34;注意:不执行范围检查,因此调用者必须确保左&lt; = right和top&lt; = bottom。&#34;

如果你不希望发生奇怪的事情。

(请注意Rect(0,0,39,39)违反了记录的约束...)