-1

Hey I am adding the following image in the Jpanel: msu-footprints.org/2011/Aditya/map.jpg

And then adding polygon:

int[] x = new int[]{65, 122, 77, 20}; 
int[] y = new int[]{226, 258, 341, 310}; 
g.setColor(Color.RED); 
g.drawPolygon(x, y, x.length); 

The polygon should coincide with building GER but is a little bit offset. My proff says the co-ordinates are okay. Is there anything wrong with how I put the image position? –

Martin
  • 7,089
  • 3
  • 28
  • 43
aditya parikh
  • 595
  • 4
  • 11
  • 30

1 Answers1

2

If painting directly to the top level container (JFrame), then the coordinate space will be wrong, the content pane is actually offset, taking into account the frame's border and menu bar.

This will occur if you override any of the frame's paint methods OR use the getGraphics method.

JFrame showing coords of origin and (offset) content

Create a custom component (extending from something like JPanel) and override it's paintComponent method and render the image and rectangle directly to it. The coordinate space will then be correct (top, left corner will be 0x0)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • A picture paints a 1000 words. That one even more so. If the text started with *"If painting directly to the top level container (JFrame), then the coordinate space will be wrong, the content pane is actually offset.."* it could almost be a 'canonical answer' to that aspect of TLC rendering. – Andrew Thompson Oct 25 '12 at 23:17
  • I made 1 further edit. If you do not like it, please roll back. OTOH 1. it just reads better like this (IMO). 2. People can look at the [edit revisions](http://stackoverflow.com/posts/13078040/revisions) if they are that interested in 'credits'. ;) – Andrew Thompson Oct 25 '12 at 23:25