Java: how to get screen sizes from a Graphics object

I am working on a geometry program where I need to draw “endless” 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 large enough coordinates so that they are on the visible surface. But I do not know, and this is my question, how do I know the coordinates of the angles of the visible surface? The getClip () graphical method sounded good, but apparently it only returns the user clip that the user set earlier. Apparently, I need the documents to be named "device clip".

And before you suggest a large length, such as 10000, I do not mean the pixel size. I use transforms to scale and translate, etc., so 10,000 may well be visible.

edit: I just wanted to tell you what I did: I determined large enough constants for the maximum width and height of the screen (they may need to be adjusted after 10 years), then I apply the inverse of my current display transformation to this “screen” to know the required length my "endless" lines. That is, the problem is not solved, it is limited to only one place in the code.

+6
source share
2 answers

there is

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

what are you after maybe? I don’t know myself, but just looking through the documents, it looks like a reasonable rate ...

+7
source

What about

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

which returns a Dimension object with screen size.

Hope this helps.

+1
source

Source: https://habr.com/ru/post/901548/


All Articles