• Post Reply Bookmark Topic Watch Topic
  • New Topic

Originally a question about JPanel 'contains'; later a warning it may be broken.  RSS feed

 
Daniel B. Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the API for JComponent#contains(...) and Component#contains(...).
... where x and y are defined to be relative to the coordinate system of this component.
The top-left of the component is [0, 0].
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not broken, it just doesn't do what you think it does. contains() works in a coordinate system relative to the component. What you think of as (101, 401) is (1, 1) relative to the component, so see what contains(1, 1) returns.
 
Daniel B. Davis
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank both of you for replies. I now do understand it. Since I was doing absolute layout and dispensing with the layout manager, I made an incorrect assumption about Component. But Rectangle has no self-imposed coordinate system, so it behaved as I expected. Thanks.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel B. Davis wrote:... I was doing absolute layout and dispensing with the layout manager, ...

That's usually a bad idea, and it's usually much better to learn and use layout managers.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!