Java graphic image

╄→гoц情女王★ 提交于 2019-12-04 07:01:42

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!