Getting drawable area of an AWT frame in Mac OS X?

只谈情不闲聊 提交于 2019-12-13 13:25:00

问题


I have subclassed java.awt.Frame and have overridden the paint() method as I wish to draw the entire contents of the window manually.

However, on the graphics object, (0,0) corresponds to the upper left hand corner of the window inside the title bar decoration, not the first drawable pixel.

Can I determine the co-ordinate of the first drawable pixel (ie, the height of the decoration) in a cross-platform manner, avoiding using a Mac OS X-specific fudge factor? Will I be forced to nest a Panel component in order to find the actual drawable area of the window?

Here, my code fails to centre the blue square inside the paintable area of the window:

@Override
public void paint (Graphics g) {
    g.setColor(Color.BLUE);
    g.setPaintMode();
    g.fillRect(30, 30, getWidth()-60, getHeight()-60);
}

回答1:


You can find the frame insets by calling the getInsets method (defined in Container). Frame insets are discussed at the top of the Frame API docs.




回答2:


So you want to paint the whole area and don't want a title bar at all?

Assuming that you use JDk 1.4 (at least) then you can declare the frame to be "undecorated" (java.awt.Frame#setUndecorated(boolean)). This way no title bar is created and therefore the frames-paintable area is the same as the frames-consumed area.



来源:https://stackoverflow.com/questions/193457/getting-drawable-area-of-an-awt-frame-in-mac-os-x

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