1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.awt.Dimension;
  24 import java.awt.EventQueue;
  25 import java.awt.Frame;
  26 import java.awt.Point;
  27 import java.awt.Robot;
  28 import java.awt.event.MouseEvent;
  29 import java.awt.event.MouseAdapter;
  30 import java.awt.event.WindowEvent;
  31 import java.awt.event.WindowAdapter;
  32 
  33 public class FrameBorderCounter {
  34 
  35     private static Frame frame;
  36     private static Frame background;
  37     private static Dimension size;
  38     private static Point location;
  39     private static Point entered;
  40 
  41     public static void main(String[] args) throws Exception {
  42         final Robot robot = new Robot();
  43         EventQueue.invokeAndWait(new Runnable() {
  44             public void run() {
  45                 robot.mouseMove(0, 0);
  46             }
  47         });
  48         EventQueue.invokeAndWait(new Runnable() {
  49             public void run() {
  50                 background = new Frame();
  51                 background.setBounds(100, 100, 300, 300);
  52                 background.addMouseListener(new MouseAdapter() {
  53                     @Override
  54                     public void mouseEntered(MouseEvent e) {
  55                         entered = e.getLocationOnScreen();
  56                         System.err.println("[ENTERED] : " + entered);
  57                     }
  58                 });
  59                 background.setVisible(true);
  60             }
  61         });
  62         EventQueue.invokeAndWait(new Runnable() {
  63             public void run() {
  64                 frame = new Frame("Frame");
  65                 frame.setBounds(200, 200, 100, 100);
  66                 frame.addWindowListener(new WindowAdapter() {
  67                     public void windowClosing(WindowEvent e) {
  68                         System.exit(0);
  69                     }
  70                 });
  71                 frame.setVisible(true);
  72             }
  73         });
  74         Thread.sleep(1000);
  75         EventQueue.invokeAndWait(new Runnable() {
  76             public void run() {
  77                 location = frame.getLocationOnScreen();
  78                 size = frame.getSize();
  79             }
  80         });
  81         int out = 20;
  82         for (int x = location.x + size.width - out; x <= location.x + size.width + out; ++x) {
  83             robot.mouseMove(x, location.y + size.height / 2);
  84             Thread.sleep(50);
  85         }
  86         System.err.println("[LOCATION] : " + location);
  87         System.err.println("[SIZE] : " + size);
  88         Thread.sleep(250);
  89         int shift = entered.x - location.x - size.width - 1;
  90         System.err.println("Done");
  91         System.out.println(shift);
  92         frame.dispose();
  93         background.dispose();
  94     }
  95 }