📄 rectangleinsets.java
字号:
/* ========================================================================
* JCommon : a free general purpose class library for the Java(tm) platform
* ========================================================================
*
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
*
* Project Info: http://www.jfree.org/jcommon/index.html
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This library 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 Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* --------------------
* RectangleInsets.java
* --------------------
* (C) Copyright 2004, 2005, by Object Refinery Limited.
*
* Original Author: David Gilbert (for Object Refinery Limited);
* Contributor(s): -;
*
* $Id: RectangleInsets.java,v 1.14 2005/11/16 15:58:41 taqua Exp $
*
* Changes:
* --------
* 11-Feb-2004 : Version 1 (DG);
* 14-Jun-2004 : Implemented Serializable (DG);
* 02-Feb-2005 : Added new methods and renamed some existing methods (DG);
* 22-Feb-2005 : Added a new constructor for convenience (DG);
* 19-Apr-2005 : Changed order of parameters in constructors to match
* java.awt.Insets (DG);
*
*/
package org.jfree.ui;
import java.awt.geom.Rectangle2D;
import java.io.Serializable;
import org.jfree.util.UnitType;
/**
* Represents the insets for a rectangle, specified in absolute or relative
* terms. This class is immutable.
*
* @author David Gilbert
*/
public class RectangleInsets implements Serializable {
/** For serialization. */
private static final long serialVersionUID = 1902273207559319996L;
/**
* A useful constant representing zero insets.
*/
public static final RectangleInsets ZERO_INSETS = new RectangleInsets(
UnitType.ABSOLUTE, 0.0, 0.0, 0.0, 0.0);
/** Absolute or relative units. */
private UnitType unitType;
/** The top insets. */
private double top;
/** The left insets. */
private double left;
/** The bottom insets. */
private double bottom;
/** The right insets. */
private double right;
/**
* Creates a new instance with the specified insets (as 'absolute' units).
*
* @param top the top insets.
* @param left the left insets.
* @param bottom the bottom insets.
* @param right the right insets.
*/
public RectangleInsets(final double top, final double left,
final double bottom, final double right) {
this(UnitType.ABSOLUTE, top, left, bottom, right);
}
/**
* Creates a new instance.
*
* @param unitType absolute or relative units (<code>null</code> not
* permitted).
* @param top the top insets.
* @param left the left insets.
* @param bottom the bottom insets.
* @param right the right insets.
*/
public RectangleInsets(final UnitType unitType,
final double top, final double left,
final double bottom, final double right) {
if (unitType == null) {
throw new IllegalArgumentException("Null 'unitType' argument.");
}
this.unitType = unitType;
this.top = top;
this.bottom = bottom;
this.left = left;
this.right = right;
}
/**
* Returns the unit type (absolute or relative). This specifies whether
* the insets are measured as Java2D units or percentages.
*
* @return The unit type (never <code>null</code>).
*/
public UnitType getUnitType() {
return this.unitType;
}
/**
* Returns the top insets.
*
* @return The top insets.
*/
public double getTop() {
return this.top;
}
/**
* Returns the bottom insets.
*
* @return The bottom insets.
*/
public double getBottom() {
return this.bottom;
}
/**
* Returns the left insets.
*
* @return The left insets.
*/
public double getLeft() {
return this.left;
}
/**
* Returns the right insets.
*
* @return The right insets.
*/
public double getRight() {
return this.right;
}
/**
* Tests this instance for equality with an arbitrary object.
*
* @param obj the object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof RectangleInsets)) {
return false;
}
final RectangleInsets that = (RectangleInsets) obj;
if (that.unitType != this.unitType) {
return false;
}
if (this.left != that.left) {
return false;
}
if (this.right != that.right) {
return false;
}
if (this.top != that.top) {
return false;
}
if (this.bottom != that.bottom) {
return false;
}
return true;
}
/**
* Returns a hash code for the object.
*
* @return A hash code.
*/
public int hashCode() {
int result;
long temp;
result = (this.unitType != null ? this.unitType.hashCode() : 0);
temp = this.top != +0.0d ? Double.doubleToLongBits(this.top) : 0L;
result = 29 * result + (int) (temp ^ (temp >>> 32));
temp = this.bottom != +0.0d ? Double.doubleToLongBits(this.bottom) : 0L;
result = 29 * result + (int) (temp ^ (temp >>> 32));
temp = this.left != +0.0d ? Double.doubleToLongBits(this.left) : 0L;
result = 29 * result + (int) (temp ^ (temp >>> 32));
temp = this.right != +0.0d ? Double.doubleToLongBits(this.right) : 0L;
result = 29 * result + (int) (temp ^ (temp >>> 32));
return result;
}
/**
* Returns a textual representation of this instance, useful for debugging
* purposes.
*
* @return A string representing this instance.
*/
public String toString() {
return "RectangleInsets[t=" + this.top + ",l=" + this.left
+ ",b=" + this.bottom + ",r=" + this.right + "]";
}
/**
* Creates an adjusted rectangle using the supplied rectangle, the insets
* specified by this instance, and the horizontal and vertical
* adjustment types.
*
* @param base the base rectangle (<code>null</code> not permitted).
* @param horizontal the horizontal adjustment type (<code>null</code> not
* permitted).
* @param vertical the vertical adjustment type (<code>null</code> not
* permitted).
*
* @return The inset rectangle.
*/
public Rectangle2D createAdjustedRectangle(final Rectangle2D base,
final LengthAdjustmentType horizontal,
final LengthAdjustmentType vertical) {
if (base == null) {
throw new IllegalArgumentException("Null 'base' argument.");
}
double x = base.getX();
double y = base.getY();
double w = base.getWidth();
double h = base.getHeight();
if (horizontal == LengthAdjustmentType.EXPAND) {
final double leftOutset = calculateLeftOutset(w);
x = x - leftOutset;
w = w + leftOutset + calculateRightOutset(w);
}
else if (horizontal == LengthAdjustmentType.CONTRACT) {
final double leftMargin = calculateLeftInset(w);
x = x + leftMargin;
w = w - leftMargin - calculateRightInset(w);
}
if (vertical == LengthAdjustmentType.EXPAND) {
final double topMargin = calculateTopOutset(h);
y = y - topMargin;
h = h + topMargin + calculateBottomOutset(h);
}
else if (vertical == LengthAdjustmentType.CONTRACT) {
final double topMargin = calculateTopInset(h);
y = y + topMargin;
h = h - topMargin - calculateBottomInset(h);
}
return new Rectangle2D.Double(x, y, w, h);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -