overlap Components - Java java.awt

Java examples for java.awt:Component

Description

overlap Components

Demo Code

// Copyright (c) 1996 - 1999, 2003 by Yoshiki Shibata. All rights reserved.
//package com.java2s;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;

import java.awt.Toolkit;

public class Main {
    private static Dimension SCREEN_SIZE = Toolkit.getDefaultToolkit()
            .getScreenSize();/*from   ww  w .  j a  v  a 2  s  .c o  m*/

    static public void overlapComponents(Component top, Component bottom,
            int offset, boolean preserveZeroLocation) {
        //
        // If top's location is (0,0), then adjust the location so that
        // the first appearance will be on the top of the parent frame.
        //
        Point location = top.getLocation();

        //
        // if preserveZeroLocation is true, it means that location(0,0) is
        // a valid location. [V1.75]
        //
        if (location.x == 0 && location.y == 0 && !preserveZeroLocation) {
            //
            // With Windows 95, if the main frame is iconified, its location()
            // might be out of screen. Therefore, if the its location is out
            // of screen, then position the top component into the center 
            // of the screen.
            //
            location = bottom.getLocation();

            if ((location.x > SCREEN_SIZE.width)
                    || (location.y > SCREEN_SIZE.height))
                centerComponent(top, new Point(0, 0), SCREEN_SIZE);
            else {
                location.x += offset;
                location.y += offset;
                fitComponentIntoScreen(top, location);
            }
        } else
            fitComponentIntoScreen(top, location);
    }

    static public void centerComponent(Component child,
            Point parentLocation, Dimension parentSize) {
        Dimension childSize = child.getSize();

        if (childSize.width > parentSize.width)
            childSize.width = parentSize.width;
        if (childSize.height > parentSize.height)
            childSize.height = parentSize.height;
        child.setLocation(parentLocation.x + parentSize.width / 2
                - childSize.width / 2, parentLocation.y + parentSize.height
                / 2 - childSize.height / 2);
    }

    static public void fitComponentIntoScreen(Component component,
            Point location) {
        Point newLocation = fitComponentInsideScreen(component, location);

        component.setLocation(newLocation);
    }

    static public void fitComponentIntoScreen(Component component) {
        fitComponentIntoScreen(component, component.getLocation());
    }

    static public Point fitComponentInsideScreen(Dimension componentSize,
            Point location) {
        Point newLocation = new Point(location.x, location.y);

        if (newLocation.x < 0)
            newLocation.x = 0;
        if (newLocation.y < 0)
            newLocation.y = 0;

        if ((newLocation.x + componentSize.width) > SCREEN_SIZE.width)
            newLocation.x = SCREEN_SIZE.width - componentSize.width;
        if ((newLocation.y + componentSize.height) > SCREEN_SIZE.height)
            newLocation.y = SCREEN_SIZE.height - componentSize.height;

        return (newLocation);
    }

    static public Point fitComponentInsideScreen(Component component,
            Point location) {
        return (fitComponentInsideScreen(component.getSize(), location));
    }
}

Related Tutorials