1 30 31 package rcm.util; 32 33 import java.awt.*; 34 35 public abstract class Win { 36 public static void center (Window window, Component ref) { 37 position (window, ref, 0.5, 0.5); 38 } 39 40 public static void position (Window frame, Component ref, 41 double xfrac, double yfrac) { 42 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 43 Dimension size = frame.getSize(); 44 Dimension refSize = (ref != null) 45 ? ref.getSize() 46 : screenSize; 47 Point origin = (ref != null) 48 ? ref.getLocationOnScreen () 49 : new Point (0, 0); 50 51 int x = origin.x + relativePoint (xfrac, refSize.width, size.width); 52 int y = origin.y + relativePoint (yfrac, refSize.height, size.height); 53 54 x = Math.max (0, Math.min (screenSize.width - size.width, x)); 56 y = Math.max (0, Math.min (screenSize.height - size.height, y)); 57 58 frame.setLocation (x, y); 59 } 60 61 static int relativePoint (double frac, int parentLength, int childLength) { 62 if (frac < 0) 63 return (int) (frac * childLength); 64 else if (frac > 1) 65 return (int) (parentLength + (frac - 2) * childLength); 66 else 67 return (int) (frac * (parentLength - childLength)); 68 } 69 70 71 public static Frame findFrame (Component comp) { 72 for (; comp!=null; comp = comp.getParent ()) 73 if (comp instanceof Frame) { 74 return (Frame)comp; 75 } 76 return null; 77 } 78 79 public static Frame findFrameOrMakeFrame (Component parent) { 80 return (parent != null) ? findFrame (parent) : new Frame (); 81 } 82 83 84 } 85 | Popular Tags |