java: How to get the 'screen' dimensions from a Graphics object
Asked Answered
C

3

9

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.

Coping answered 15/11, 2011 at 19:18 Comment(4)
What are you drawing too? Fullscreen, or something like a JPanel?Gentianaceous
a JPanel. but this method should not care about that, it only has g to go on.Coping
I'm not 100% sure but I think in this kind of instance you always draw relative to 0,0 in the top corner. The viewport as it were, never actually moves. If you wanted to do something where conceptually it moves, you have to code over the top of it to manage a viewport yourselfGentianaceous
that's wrong. but even if it were true, top left 0,0, how does that give me the other corners?Coping
D
11

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...

Deary answered 15/11, 2011 at 19:21 Comment(6)
i hadn't come across that yet, and it actually is a step forward. it seems to give me the canvas dimensions in pixels. i guess i could now extract the transform and inverse transform it. i will try and if there is no direct solution in the Grahpic's coordinate system, i will accept this answer.Coping
@peter: It's entirely possible that there's a better solution - that was just what I came across. It's ages since I've done graphics in Java I'm afraid :(Deary
i just tried it, and unfortunately it doesnt seem to update when i resize the window. not even if i make it larger. that's a strange method to have...Coping
@peter: Ah, it wasn't clear that you wanted the clip for the window - that method will give you the device (i.e. the screen).Deary
no, that is not true either, it seems to give only the initial size of the JPanel, close to the setSize values of my JFrame. i now tried g.getDeviceConfiguration().getDevice().getDisplayMode().getWidth() but it does the same. only the value from the program start will be reported.Coping
works fine on jdk8, but returns 2147483647 x 2147483647 on jdk13 😒Hulbard
F
2

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);
    }

}
Fulbright answered 6/10, 2022 at 2:7 Comment(1)
From the javadoc: This method refers to the user clip, which is independent of the clipping associated with device bounds and window visibility. If no clip has previously been set, or if the clip has been cleared using setClip(null), this method returns null. I wouldn't expect your solution to work in all circumstances (user sets and removes their own clip, window gets resized, ...)Explosive
G
1

How about

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

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

Hope this helps.

Gipon answered 15/11, 2011 at 19:34 Comment(2)
Oops... just noticed the comment above about being in window. This probably wont help then.Gipon
i came across this before and ignored it. but now it actually inspired me to a crude solution of the infinite lines problem. however i am still interested in the actual dimensions, e.g. to translate the origin to the canvas center.Coping

© 2022 - 2025 — McMap. All rights reserved.