001 package com.croftsoft.core.gui; 002 003 import java.awt.*; 004 005 /********************************************************************* 006 * Supplementary static methods for the java.awt.Window class. 007 * 008 * @version 009 * $Date: 2005/03/21 23:08:31 $ 010 * @since 011 * 1998-10-28 012 * @author 013 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 014 *********************************************************************/ 015 016 public final class WindowLib 017 ////////////////////////////////////////////////////////////////////// 018 ////////////////////////////////////////////////////////////////////// 019 { 020 021 public static void centerOnScreen ( Window window ) 022 ////////////////////////////////////////////////////////////////////// 023 { 024 centerOnScreen ( window, window.getSize ( ) ); 025 } 026 027 public static void centerOnScreen ( 028 Window window, 029 int width, 030 int height ) 031 ////////////////////////////////////////////////////////////////////// 032 { 033 Dimension screenSize 034 = Toolkit.getDefaultToolkit ( ).getScreenSize ( ); 035 036 window.setBounds ( 037 ( screenSize.width - width ) / 2, 038 ( screenSize.height - height ) / 2, 039 width, 040 height ); 041 } 042 043 public static void centerOnScreen ( 044 Window window, Dimension size ) 045 ////////////////////////////////////////////////////////////////////// 046 { 047 centerOnScreen ( window, size.width, size.height ); 048 } 049 050 public static void centerOnScreen ( 051 Window window, double screenRatio ) 052 ////////////////////////////////////////////////////////////////////// 053 { 054 Dimension screenSize 055 = Toolkit.getDefaultToolkit ( ).getScreenSize ( ); 056 057 int width = ( int ) ( screenSize.width * screenRatio ); 058 059 int height = ( int ) ( screenSize.height * screenRatio ); 060 061 window.setBounds ( 062 ( screenSize.width - width ) / 2, 063 ( screenSize.height - height ) / 2, 064 width, 065 height ); 066 } 067 068 public static void centerAboveParent ( 069 Window child, Dimension size ) 070 ////////////////////////////////////////////////////////////////////// 071 { 072 Container parent = child.getParent ( ); 073 Rectangle parentBounds = parent.getBounds ( ); 074 child.setBounds ( 075 parentBounds.x + ( parentBounds.width - size.width ) / 2, 076 parentBounds.y + ( parentBounds.height - size.height ) / 2, 077 size.width, size.height ); 078 } 079 080 /* 081 public static void centerAboveParent ( 082 Window child, double parentRatio ) 083 ////////////////////////////////////////////////////////////////////// 084 { 085 Container parent = child.getParent ( ); 086 087 Rectangle parentBounds = parent.getBounds ( ); 088 089 int width = ( int ) ( parentBounds.width * parentRatio ); 090 091 int height = ( int ) ( parentBounds.height * parentRatio ); 092 093 child.setBounds ( 094 parentBounds.x + ( parentBounds.width - width ) / 2, 095 parentBounds.y + ( parentBounds.height - height ) / 2, 096 width, 097 height ); 098 } 099 */ 100 101 public static void centerAboveParent ( Window child ) 102 ////////////////////////////////////////////////////////////////////// 103 { 104 centerAboveParent ( child, child.getSize ( ) ); 105 } 106 107 public static Window getParentWindow ( Component component ) 108 ////////////////////////////////////////////////////////////////////// 109 { 110 Component parent = component; 111 112 while ( ( parent = parent.getParent ( ) ) != null ) 113 { 114 if ( parent instanceof Window ) 115 { 116 break; 117 } 118 } 119 120 return ( Window ) parent; 121 } 122 123 ////////////////////////////////////////////////////////////////////// 124 ////////////////////////////////////////////////////////////////////// 125 126 private WindowLib ( ) { } 127 128 ////////////////////////////////////////////////////////////////////// 129 ////////////////////////////////////////////////////////////////////// 130 }