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

peter picture peter · Nov 15, 2011 · Viewed 8.2k times · Source

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.

Answer

Jon Skeet picture Jon Skeet · Nov 15, 2011

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