get Position Within Window - Java Swing

Java examples for Swing:Screen

Description

get Position Within Window

Demo Code


//package com.java2s;
import java.awt.Component;

import java.awt.Point;

public class Main {
    public static Point getPositionWithinWindow(Component component,
            Component parent, Point p) {
        Point[] pointCheck = new Point[] { (Point) p.clone(),
                (Point) p.clone(), (Point) p.clone(), (Point) p.clone() };
        int w = component.getWidth();
        int h = component.getHeight();
        pointCheck[0].translate(w, h);// w  w w.  j a v a2s .  com
        pointCheck[1].translate(0, h);
        pointCheck[2].translate(w, 0);
        pointCheck[3].translate(0, 0);
        for (Point p2 : pointCheck) {
            if (parent.getBounds().contains(p2)) {
                p2.translate(-w, -h);
                return p2;
            }
        }
        return p;
    }
}

Related Tutorials