1

When constructing a new JWindow or JFrame is it possible to right align the edge of the window to the visible edge of a monitor?

This will be used on Windows systems with multiple monitors. Ideally I would like to have the right edge of a new window snap to the right edge of the right most screen. Currently I am doing this by obtaining the GraphicsDevices, sorting, comparing Point location + window size and adjusting the location to 'move' the window before display if I detect if the right edge of the window is out of bounds of the visible screen area. I did this before finding this question which is essentially the same idea (How to know if a JFrame is on screen in a multi screen environment). This accomplishes the goal. I know the SSCCE advertisers would like me to post what I am already doing, and there is already one in the other question if interested, but my intent is to ask if there is a much simpler / more native way to obtain these results? As a side note it take 2.6ms the first call and .066ms each additional call. Originally my nanosecond to millisecond math was wrong, so performance is not a factor now, but I would still be curious if there is a better way to do this.

Here are some example debug sysouts if interested. First two on left of first monitor, third test on right side of second monitor:

Last Monitor Rectangle: java.awt.Rectangle[x=1920,y=150,width=1680,height=1050]
Point to compare : java.awt.Point[x=20,y=100], windowWidth:1270
rightBoundOfMonitor : 3600.0
rightBoundOfNewWindow : 1290.0
In bounds
Adjustment time : 2536893ns,  2.536893ms
Last Monitor Rectangle: java.awt.Rectangle[x=1920,y=150,width=1680,height=1050]
Point to compare : java.awt.Point[x=20,y=100], windowWidth:1270
rightBoundOfMonitor : 3600.0
rightBoundOfNewWindow : 1290.0
In bounds
Adjustment time : 65880ns,  0.06588ms
Last Monitor Rectangle: java.awt.Rectangle[x=1920,y=150,width=1680,height=1050]
Point to compare : java.awt.Point[x=3147,y=315], windowWidth:1270
rightBoundOfMonitor : 3600.0
rightBoundOfNewWindow : 4417.0
Out of bounds
Adjustment time : 152949ns,  0.152949ms

EDIT: Performance was not nearly as much of an issue as I initially thought. (1000000 ns in a ms, not 1000). The first call to GraphicsDevice takes ~2.5ms, which is acceptable, subsequent calls to determine position are much quicker. It appears that currently there are no API calls to do this easier than I came up with, or what others have derived either.

Community
  • 1
  • 1
bobtheowl2
  • 641
  • 2
  • 10
  • 19

1 Answers1

1

For reference, you might compare your approach to this simpler example that pins a frame to the lower right corner of the default screen. The goal would be to determine what part of looping through the GraphicsDevice list dominates the time taken. I doubt that Rectangle#getBounds() is important, but GraphicsDevice#getDefaultConfiguration() may be. A top-level container is owned by the host platform, which typically has to enforce the same single-threaded rules as Swing, so there may be little you can do.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Looks like the first time GraphicsEnvironment.getLocalGraphicsEnvironment and getScreenDevices those are in the 20-30ms range. In subsequent runs those become negligible. Showing ns because most round to 0 in ms: GraphicsEnvironment.getLocalGraphicsEnvironment = 25316284ns, getScreenDevices = 28968839ns, screen.getDefaultConfiguration = 442955ns, gc.getBounds = 514795ns, rect.getX = 34099ns, screen.getDefaultConfiguration = 277756ns, gc.getBounds = 7283ns, rect.getX = 331ns – bobtheowl2 Nov 05 '13 at 20:49
  • I don't see much you can do except pre-flight the slower ones. Can you update your question? I envy your ability to compare multi-digit ns in a comment, :-) – trashgod Nov 05 '13 at 21:23