java:如何获取“屏幕” Graphics 对象的尺寸
我正在开发一个几何程序,我需要绘制“无限”线。我的类 Line 有一个方法
public void draw(Graphics2D g){
//... calculate x1,y1,x2,y2 here ...
g.draw(new Line2D.Double(x1,y1, x2,y2));
}
我的想法是选择足够大的坐标,以便它们远离可见表面。但我不知道,这是我的问题,我如何知道可见表面的角点的坐标? Graphic 的方法 getClip() 听起来不错,但显然它只返回用户之前设置的自定义剪辑。显然我需要的是文档中的“设备剪辑”。
在你建议一个大的长度,比如 10000 之前,我在这里指的并不是像素大小。我使用变换进行缩放和平移等,因此 10000 很可能是可见的。
编辑: 我只是想告诉你我最终做了什么:我为最大屏幕宽度和高度定义了一个相当大的常量(它们可能需要在 10 年内调整),然后我将当前显示转换的逆值应用到这个“屏幕”知道我的“无限”线的必要长度。 即问题没有解决,仅局限于代码中的单个位置。
I am working on a geometry program where I need to draw 'infinite' lines. My class Line has a method
public void draw(Graphics2D g){
//... calculate x1,y1,x2,y2 here ...
g.draw(new Line2D.Double(x1,y1, x2,y2));
}
My idea is to choose the coordinates large enough so they will be off the visible surface. But I don't know, and this is my question, how do I know the coordinates of the corners of the visible surface? Graphic's method getClip() sounded nice, but apparently it only returns a custom clip the user set before. Apparently what I need is called 'device clip' in the docs.
And before you suggest a big length, like 10000, I don't mean pixel size here. I use transforms for zooming and translating and such, so 10000 might well be visible.
edit:
I just wanted to tell you what I ended up doing: I defined a reasonably large constants for maximum screen width and height (they might need adjusting in 10 years), then I apply the inverse of my current display transformation to this 'screen' to know the necessary length of my 'infinite' lines.
I.e. the problem is not solved, only confined to a single spot in the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许是
你所追求的?我不了解自己,但只是浏览文档,这看起来是一个合理的选择......
Is
what you're after perhaps? I don't know myself, but just browsing the docs it looks like a reasonable bet...
对我来说,这有效:
示例:
For me, this works:
Example:
怎么样
返回一个具有屏幕大小的 Dimension 对象
?希望这有帮助。
How about
which returns a Dimension object with the size of the screen.
Hope this helps.