Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CoreUIUtils.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.ui;
  18. import java.awt.GraphicsConfiguration;
  19. import java.awt.GraphicsDevice;
  20. import java.awt.MouseInfo;
  21. import java.awt.PointerInfo;
  22. import java.awt.Rectangle;
  23. import java.awt.Window;
  24. /**
  25. * Core UI Utilities.
  26. */
  27. public final class CoreUIUtils {
  28. /** Prevent creation. */
  29. private CoreUIUtils() {
  30. }
  31. /**
  32. * Centre the specified window on the active monitor.
  33. *
  34. * @param window Window to centre
  35. */
  36. public static void centreWindow(final Window window) {
  37. // Get the Location of the mouse pointer
  38. final PointerInfo myPointerInfo = MouseInfo.getPointerInfo();
  39. if (myPointerInfo == null) {
  40. return;
  41. }
  42. // Get the Device (screen) the mouse pointer is on
  43. final GraphicsDevice myDevice = myPointerInfo.getDevice();
  44. if (myDevice == null) {
  45. return;
  46. }
  47. // Get the configuration for the device
  48. final GraphicsConfiguration myGraphicsConfig = myDevice.getDefaultConfiguration();
  49. if (myGraphicsConfig == null) {
  50. return;
  51. }
  52. // Get the bounds of the device
  53. final Rectangle gcBounds = myGraphicsConfig.getBounds();
  54. // Calculate the centre of the screen
  55. // gcBounds.x and gcBounds.y give the co ordinates where the screen
  56. // starts. gcBounds.width and gcBounds.height return the size in pixels
  57. // of the screen.
  58. final int xPos = gcBounds.x + ((gcBounds.width - window.getWidth()) / 2);
  59. final int yPos = gcBounds.y + ((gcBounds.height - window.getHeight()) / 2);
  60. // Set the location of the window
  61. window.setLocation(xPos, yPos);
  62. }
  63. }