Example usage for javax.swing JComponent getVisibleRect

List of usage examples for javax.swing JComponent getVisibleRect

Introduction

In this page you can find the example usage for javax.swing JComponent getVisibleRect.

Prototype

@BeanProperty(bound = false)
public Rectangle getVisibleRect() 

Source Link

Document

Returns the Component's "visible rectangle" - the intersection of this component's visible rectangle, new Rectangle(0, 0, getWidth(), getHeight()), and all of its ancestors' visible rectangles.

Usage

From source file:Main.java

public static boolean isVisible(JComponent c, Rectangle r) {
    return c.getVisibleRect().contains(r);
}

From source file:Main.java

public static boolean isVerticallyVisible(JComponent c, Rectangle r) {
    Rectangle visible = c.getVisibleRect();

    return visible.y <= r.y && visible.y + visible.height >= r.y + r.height;
}

From source file:Main.java

public static boolean isVerticallyVisible(JComponent c, int from, int to) {
    Rectangle visible = c.getVisibleRect();

    return visible.y <= from && visible.y + visible.height >= to;
}

From source file:Main.java

/**
 * Implements autoscrolling./*w  w w  .j ava2 s  .c o  m*/
 *
 * @param comp
 *          the component
 * @param cursorLocn
 *          the cursor location
 */
public static void defaultAutoScroll(JComponent comp, Point cursorLocn) {
    Rectangle visible = comp.getVisibleRect();
    int x = 0, y = 0, width = 0, height = 0;

    // Scroll left.
    if (cursorLocn.x < visible.x + AUTOSCROLL_INSET_SIZE) {
        x = -SCROLL_AMOUNT;
        width = SCROLL_AMOUNT;
    } // Scroll right.
    else if (cursorLocn.x > visible.x + visible.width - AUTOSCROLL_INSET_SIZE) {
        x = visible.width + SCROLL_AMOUNT;
        width = SCROLL_AMOUNT;
    }

    // Scroll up.
    if (cursorLocn.y < visible.y + AUTOSCROLL_INSET_SIZE) {
        y = -SCROLL_AMOUNT;
        height = SCROLL_AMOUNT;
    } // Scroll down.
    else if (cursorLocn.y > visible.y + visible.height - AUTOSCROLL_INSET_SIZE) {
        y = visible.height + SCROLL_AMOUNT;
        height = SCROLL_AMOUNT;
    }

    ((JComponent) comp.getParent()).scrollRectToVisible(new Rectangle(x, y, width, height));
}

From source file:Main.java

public static void scrollVertically(JComponent c, int from, int to) {
    Rectangle visible = c.getVisibleRect();

    if (visible.y <= from && visible.y + visible.height >= to)
        return;//w  ww. j  ava2  s  .  com

    visible.y = from;
    visible.height = to - from;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static void scrollHorizontally(JComponent c, int from, int to) {
    Rectangle visible = c.getVisibleRect();

    if (visible.x <= from && visible.x + visible.width >= to)
        return;/*from  w  ww  . j  ava  2 s. co  m*/

    visible.x = from;
    visible.width = to - from;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static void center(JComponent c, Rectangle r, boolean withInsets) {
    Rectangle visible = c.getVisibleRect();

    visible.x = r.x - (visible.width - r.width) / 2;
    visible.y = r.y - (visible.height - r.height) / 2;

    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? EMPTY_INSETS : c.getInsets();
    bounds.x = i.left;/*from   w ww  .  j  a  v a  2 s .com*/
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    if (visible.x < bounds.x)
        visible.x = bounds.x;

    if (visible.x + visible.width > bounds.x + bounds.width)
        visible.x = bounds.x + bounds.width - visible.width;

    if (visible.y < bounds.y)
        visible.y = bounds.y;

    if (visible.y + visible.height > bounds.y + bounds.height)
        visible.y = bounds.y + bounds.height - visible.height;

    c.scrollRectToVisible(visible);
}

From source file:Main.java

public static void scrollVertically(JComponent c, int from, int to, int bias) {
    Rectangle visible = c.getVisibleRect(), dest = new Rectangle(visible);

    dest.y = from;//  w  ww . ja va 2s . c o  m
    dest.height = to - from;

    if (dest.height > visible.height) {
        if (bias == VIEWPORT) {
            // leave as is
        } else if (bias == UNCHANGED) {
            if (dest.y <= visible.y && dest.y + dest.height >= visible.y + visible.height) {
                dest.y = visible.y;
                dest.height = visible.height;
            }
        } else {
            if (bias == CENTER)
                dest.y += (dest.height - visible.height) / 2;
            else if (bias == LAST)
                dest.y += dest.height - visible.height;

            dest.height = visible.height;
        }
    }

    if (!visible.contains(dest))
        c.scrollRectToVisible(dest);
}

From source file:Main.java

public static void scrollHorizontally(JComponent c, int from, int to, int bias) {
    Rectangle visible = c.getVisibleRect(), dest = new Rectangle(visible);

    dest.x = from;/*from  w  ww .j  a  v a 2s  .c o  m*/
    dest.width = to - from;

    if (dest.width > visible.width) {
        if (bias == VIEWPORT) {
            // leave as is
        } else if (bias == UNCHANGED) {
            if (dest.x <= visible.x && dest.x + dest.width >= visible.x + visible.width) {
                dest.x = visible.x;
                dest.width = visible.width;
            }
        } else {
            if (bias == CENTER)
                dest.x += (dest.width - visible.width) / 2;
            else if (bias == LAST)
                dest.x += dest.width - visible.width;

            dest.width = visible.width;
        }
    }

    if (!visible.contains(dest))
        c.scrollRectToVisible(dest);
}

From source file:Main.java

public static void centerHorizontally(JComponent c, int from, int to, boolean withInsets) {
    Rectangle bounds = c.getBounds();
    Insets i = withInsets ? EMPTY_INSETS : c.getInsets();
    bounds.x = i.left;/*from  ww w. j  a v  a 2s .co  m*/
    bounds.y = i.top;
    bounds.width -= i.left + i.right;
    bounds.height -= i.top + i.bottom;

    Rectangle visible = c.getVisibleRect();

    visible.x = from - (visible.width + from - to) / 2;

    if (visible.x < bounds.x)
        visible.x = bounds.x;

    if (visible.x + visible.width > bounds.x + bounds.width)
        visible.x = bounds.x + bounds.width - visible.width;

    c.scrollRectToVisible(visible);
}