posted 10 years ago
I am having trouble understanding what 'contains' means.
Using debug, I have verified that free1, a JPanel, lies in rectangle 100, 400, 100, 100 for x, y, width, height, respectively.
Point p1 is 101, 401 for x, y resp., and point p2 is 101, 499 for ditto. According to my understanding, both points lie
within the rectangle of the JPanel. Yet, given the statement: if (free1.contains(p1) && free2.contains(p2) ...
both parts of the statement return false, as shown by inspect. Please, what is going on? According to my understanding,
the points diagonally one in from each corner would be: ne 101, 401, se 101, 499, nw 199, 401, sw 199, 499. if not
correct, What points would be diagonally one in from each corner? Why is 'contains' returning false? Under what circumstances
would it return true? Will I need to replace it with something which reflects my understanding that if a point lies wholly
within the bounds, it is 'contained'.
Aha! before sending this, I tried replacing free1 in the statement with a rectangle obtained by getBounds. That seems to
work. So I am left with the idea that 'contains' is broken insofar as JComponent is concerned. Nice that there is a
workaround. I am sending this as a warning to the unwary. But an answer might also inform me of an error on my part
for which I am always happy to acknowledge and grateful to receive.