问题
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:
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.
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)
来源:https://stackoverflow.com/questions/13077858/java-graphic-image