Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 914
peter Avatar asked Nov 15 '11 19:11

peter


People also ask

How do I change the size of a JFrame?

Change window Size of a JFrame To resize a frame, There is a method JFrame. setSize(int width, int height) which takes two parameters width and height.


2 Answers

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

like image 81
Jon Skeet Avatar answered Oct 16 '22 02:10

Jon Skeet


How about

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

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

Hope this helps.

like image 43
cjstehno Avatar answered Oct 16 '22 02:10

cjstehno