Java graphic image

2019-03-06 21:44发布

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? –

1条回答
叼着烟拽天下
2楼-- · 2019-03-06 22:35

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)

查看更多
登录 后发表回答