LonelyKing
11-30-2002, 03:52 AM
So I'm making a Battleship game for a class project and I figured it would be nice to be able to drag/drop ships into place. But I've run into a little problem (which I fixed, but still) with JLabels and ImageIcons. In order to test that the mouse is dragging the label, which is only an image, I have it using
if(label.getBounds().contains(e.getPoint()))
{ //do code to allow dragging }
in the mousePressed() method where e is the MouseEvent and label is the JLabel. It works all well and good... except that the bounds on where the event fires is shifted upwards 50%, i.e. if the image's y-coordinate is 50 and it has a height of 20, you'd be able to drag from the area 40 to 60 instead of 50 to 70 like it should be. Here's a little picture.. the dots are the image, the hyphens are where it will drag from:
---
.......
--- .......
.......
The x-coordinates are fine, but the y's are all jacked. Obviously, this wasn't hard to get around, but it's still annoying. I've tried pretty much everything to make it work correctly without writing my own method, but to no avail.... I'm just wondering if this is normal or if I'm doing something horribly wrong?
EDIT:
Strange coincidence, but a little more clarification. It's 'just' adding 30 pixels to the top of the bounds and stealing that from the bottom of the image. It was just pure coincidence that the image I was testing was 60 pixels high. :rolleyes:
EDIT 2:
It only happens when I use LayoutManagers. I set it to null and just used absolute positioning and the problem mysteriously went away. It also seems that *any* component that is affected by a layout manager has this 30 pixel 'padding' at the top... seriously, is this normal? I'm not a huge Java guy, I only took the class this semester because I had nothing else to take...
if(label.getBounds().contains(e.getPoint()))
{ //do code to allow dragging }
in the mousePressed() method where e is the MouseEvent and label is the JLabel. It works all well and good... except that the bounds on where the event fires is shifted upwards 50%, i.e. if the image's y-coordinate is 50 and it has a height of 20, you'd be able to drag from the area 40 to 60 instead of 50 to 70 like it should be. Here's a little picture.. the dots are the image, the hyphens are where it will drag from:
---
.......
--- .......
.......
The x-coordinates are fine, but the y's are all jacked. Obviously, this wasn't hard to get around, but it's still annoying. I've tried pretty much everything to make it work correctly without writing my own method, but to no avail.... I'm just wondering if this is normal or if I'm doing something horribly wrong?
EDIT:
Strange coincidence, but a little more clarification. It's 'just' adding 30 pixels to the top of the bounds and stealing that from the bottom of the image. It was just pure coincidence that the image I was testing was 60 pixels high. :rolleyes:
EDIT 2:
It only happens when I use LayoutManagers. I set it to null and just used absolute positioning and the problem mysteriously went away. It also seems that *any* component that is affected by a layout manager has this 30 pixel 'padding' at the top... seriously, is this normal? I'm not a huge Java guy, I only took the class this semester because I had nothing else to take...