![](/img/trans.png)
[英]Java method to find the rectangle that is the intersection of two rectangles using only left bottom point, width and height?
[英]Java: Rectangles Width and Height
当我绘制如下的矩形时
drawRect(0,0,39,39);
像素的宽度和高度是否均为40? 当我在我知道是40x40像素的图像上绘制矩形并将该矩形放在其上时,它就非常合适。 所以我有点困惑,因为java网站说第3个和第4个变量是指width和height,但是如果是这种情况,它实际上并不是指实际宽度,而只是指实际的width-1。 我感觉自己缺少某些东西或可能犯了一个错误。 小号
它将是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像素。
您的问题使两个类混淆。
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)
。
Rect
未在Java网站上记录,因为它不是Java的一部分。 如果您在Java网站上找到了文档,那是针对Rectangle
类的...有所不同。
如果将Rect
参数错误地提供为x,y,width,height,则很可能会被烧毁。 Rect
构造函数的javadoc说:
“注意:不执行范围检查,因此调用方必须确保左<=右和上<=下。”
如果您不这样做,那么会发生奇怪的事情。
(请注意, Rect(0,0,39,39)
违反了记录的约束...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.