-1

It appears that the contains() method in Rectangle is not inclusive to the bottom right corner.

For example the following code returns "false";

Rectangle r = new Rectangle(0,0,100,100);
System.out.println(r.contains(100, 100));
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Maxwell
  • 133
  • 1
  • 7
  • The `Rectangle` API does not specifically state inclusion or exclusion, unfortunately`...... https://docs.oracle.com/javase/8/docs/api/java/awt/Rectangle.html#contains-int-int- – Rann Lifshitz Apr 27 '19 at 04:37
  • Seems like the correct behaviour to me. These co-ordinates are actual pixels on the screen, not mathematical points. A rectangle that includes both `(0,0)` and `(100,100)` would have to have a width and height of at least `101` pixels. – Dawood ibn Kareem Apr 27 '19 at 06:02

2 Answers2

2

As quoted from the Rectangle API (Java 8):

public Rectangle(int x, int y, int width, int height)
Constructs a new Rectangle whose upper-left corner is specified as (x,y) and whose width and height are specified by the arguments of the same name.

Using Width and Height with the starting Point of (0,0) means the Rectangle has points from (0,0) to (99,99) - 100 pixels of width and 100 pixels of height, based on the given starting pixel of (0,0) which is always included in the Rectangle.

This means that (100,100) will indeed not be included in the constructed Rectangle. Based on the logic above, (100,100) will be contained in the following (verified using an online java compiler):

Rectangle r = new Rectangle(1,1,100,100);

References:

Rann Lifshitz
  • 4,040
  • 4
  • 22
  • 42
0

It seems that the API wrongly states that the "upper left corner" is (x,y) when according to the accepted answer and my own experience, (x,y) is the lower left corner.

MLRAL
  • 51
  • 7