Gets the interior drawing region of a component. - Java Swing

Java examples for Swing:JComponent

Description

Gets the interior drawing region of a component.

Demo Code

/*//from www . jav a2  s  .  co m
 * Copyright (C) 2012 Jason Gedge <http://www.gedge.ca>
 *
 * This file is part of the OpGraph project.
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

import java.awt.Insets;

import java.awt.Rectangle;

import javax.swing.JComponent;

public class Main {
    /**
     * Gets the interior drawing region of a component. That is, the drawing
     * area inside of its border.
     * 
     * @param comp  the component to get the interior rectangle of
     * 
     * @return the interior rectangle
     */
    public static Rectangle getInterior(JComponent comp) {
        final Insets insets = comp.getInsets();
        final Rectangle rect = new Rectangle(0, 0, comp.getWidth(),
                comp.getHeight());
        rect.x += insets.left;
        rect.y += insets.top;
        rect.width -= insets.left + insets.right;
        rect.height -= insets.top + insets.bottom;
        return rect;
    }
}

Related Tutorials