java:如何获取“屏幕” Graphics 对象的尺寸

发布于 2024-12-15 17:11:44 字数 524 浏览 5 评论 0原文

我正在开发一个几何程序,我需要绘制“无限”线。我的类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

守护在此方 2024-12-22 17:11:44

也许是

Rectangle bounds = g.getDeviceConfiguration().getBounds()

你所追求的?我不了解自己,但只是浏览文档,这看起来是一个合理的选择......

Is

Rectangle bounds = g.getDeviceConfiguration().getBounds()

what you're after perhaps? I don't know myself, but just browsing the docs it looks like a reasonable bet...

旧时浪漫 2024-12-22 17:11:44

对我来说,这有效:

Rectangle rect = g.getClipBounds();

示例:

public class GetBoundsThroughGraphics extends JPanel {

    public GetBoundsThroughGraphics() {
        setPreferredSize(new Dimension(300, 300));
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Rectangle rect = g.getClipBounds();
        int width = (int)rect.getWidth(); // 300
        int height = (int)rect.getHeight(); // 300
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new GetBoundsThroughGraphics());
        frame.pack();
        frame.setVisible(true);
    }

}

For me, this works:

Rectangle rect = g.getClipBounds();

Example:

public class GetBoundsThroughGraphics extends JPanel {

    public GetBoundsThroughGraphics() {
        setPreferredSize(new Dimension(300, 300));
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Rectangle rect = g.getClipBounds();
        int width = (int)rect.getWidth(); // 300
        int height = (int)rect.getHeight(); // 300
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.add(new GetBoundsThroughGraphics());
        frame.pack();
        frame.setVisible(true);
    }

}
﹏雨一样淡蓝的深情 2024-12-22 17:11:44

怎么样

java.awt.Toolkit.getDefaultToolkit().getScreenSize()

返回一个具有屏幕大小的 Dimension 对象

?希望这有帮助。

How about

java.awt.Toolkit.getDefaultToolkit().getScreenSize()

which returns a Dimension object with the size of the screen.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文