📄 rectangleinsets.java
字号:
y = y + topMargin;
h = h - topMargin - calculateBottomInset(h);
}
return new Rectangle2D.Double(x, y, w, h);
}
/**
* Creates an 'inset' rectangle.
*
* @param base the base rectangle (<code>null</code> not permitted).
*
* @return The inset rectangle.
*/
public Rectangle2D createInsetRectangle(final Rectangle2D base) {
return createInsetRectangle(base, true, true);
}
/**
* Creates an 'inset' rectangle.
*
* @param base the base rectangle (<code>null</code> not permitted).
* @param horizontal apply horizontal insets?
* @param vertical apply vertical insets?
*
* @return The inset rectangle.
*/
public Rectangle2D createInsetRectangle(final Rectangle2D base,
final boolean horizontal,
final boolean vertical) {
if (base == null) {
throw new IllegalArgumentException("Null 'base' argument.");
}
double topMargin = 0.0;
double bottomMargin = 0.0;
if (vertical) {
topMargin = calculateTopInset(base.getHeight());
bottomMargin = calculateBottomInset(base.getHeight());
}
double leftMargin = 0.0;
double rightMargin = 0.0;
if (horizontal) {
leftMargin = calculateLeftInset(base.getWidth());
rightMargin = calculateRightInset(base.getWidth());
}
return new Rectangle2D.Double(
base.getX() + leftMargin,
base.getY() + topMargin,
base.getWidth() - leftMargin - rightMargin,
base.getHeight() - topMargin - bottomMargin
);
}
/**
* Creates an outset rectangle.
*
* @param base the base rectangle (<code>null</code> not permitted).
*
* @return An outset rectangle.
*/
public Rectangle2D createOutsetRectangle(final Rectangle2D base) {
return createOutsetRectangle(base, true, true);
}
/**
* Creates an outset rectangle.
*
* @param base the base rectangle (<code>null</code> not permitted).
* @param horizontal apply horizontal insets?
* @param vertical apply vertical insets?
*
* @return An outset rectangle.
*/
public Rectangle2D createOutsetRectangle(final Rectangle2D base,
final boolean horizontal,
final boolean vertical) {
if (base == null) {
throw new IllegalArgumentException("Null 'base' argument.");
}
double topMargin = 0.0;
double bottomMargin = 0.0;
if (vertical) {
topMargin = calculateTopOutset(base.getHeight());
bottomMargin = calculateBottomOutset(base.getHeight());
}
double leftMargin = 0.0;
double rightMargin = 0.0;
if (horizontal) {
leftMargin = calculateLeftOutset(base.getWidth());
rightMargin = calculateRightOutset(base.getWidth());
}
return new Rectangle2D.Double(
base.getX() - leftMargin,
base.getY() - topMargin,
base.getWidth() + leftMargin + rightMargin,
base.getHeight() + topMargin + bottomMargin
);
}
/**
* Returns the top margin.
*
* @param height the height of the base rectangle.
*
* @return The top margin (in Java2D units).
*/
public double calculateTopInset(final double height) {
double result = this.top;
if (this.unitType == UnitType.RELATIVE) {
result = (this.top * height);
}
return result;
}
/**
* Returns the top margin.
*
* @param height the height of the base rectangle.
*
* @return The top margin (in Java2D units).
*/
public double calculateTopOutset(final double height) {
double result = this.top;
if (this.unitType == UnitType.RELATIVE) {
result = (height / (1 - this.top - this.bottom)) * this.top;
}
return result;
}
/**
* Returns the bottom margin.
*
* @param height the height of the base rectangle.
*
* @return The bottom margin (in Java2D units).
*/
public double calculateBottomInset(final double height) {
double result = this.bottom;
if (this.unitType == UnitType.RELATIVE) {
result = (this.bottom * height);
}
return result;
}
/**
* Returns the bottom margin.
*
* @param height the height of the base rectangle.
*
* @return The bottom margin (in Java2D units).
*/
public double calculateBottomOutset(final double height) {
double result = this.bottom;
if (this.unitType == UnitType.RELATIVE) {
result = (height / (1 - this.top - this.bottom)) * this.bottom;
}
return result;
}
/**
* Returns the left margin.
*
* @param width the width of the base rectangle.
*
* @return The left margin (in Java2D units).
*/
public double calculateLeftInset(final double width) {
double result = this.left;
if (this.unitType == UnitType.RELATIVE) {
result = (this.left * width);
}
return result;
}
/**
* Returns the left margin.
*
* @param width the width of the base rectangle.
*
* @return The left margin (in Java2D units).
*/
public double calculateLeftOutset(final double width) {
double result = this.left;
if (this.unitType == UnitType.RELATIVE) {
result = (width / (1 - this.left - this.right)) * this.left;
}
return result;
}
/**
* Returns the right margin.
*
* @param width the width of the base rectangle.
*
* @return The right margin (in Java2D units).
*/
public double calculateRightInset(final double width) {
double result = this.right;
if (this.unitType == UnitType.RELATIVE) {
result = (this.right * width);
}
return result;
}
/**
* Returns the right margin.
*
* @param width the width of the base rectangle.
*
* @return The right margin (in Java2D units).
*/
public double calculateRightOutset(final double width) {
double result = this.right;
if (this.unitType == UnitType.RELATIVE) {
result = (width / (1 - this.left - this.right)) * this.right;
}
return result;
}
/**
* Trims the given width to allow for the insets.
*
* @param width the width.
*
* @return The trimmed width.
*/
public double trimWidth(final double width) {
return width - calculateLeftInset(width) - calculateRightInset(width);
}
/**
* Extends the given width to allow for the insets.
*
* @param width the width.
*
* @return The extended width.
*/
public double extendWidth(final double width) {
return width + calculateLeftOutset(width) + calculateRightOutset(width);
}
/**
* Trims the given height to allow for the insets.
*
* @param height the height.
*
* @return The trimmed height.
*/
public double trimHeight(final double height) {
return height
- calculateTopInset(height) - calculateBottomInset(height);
}
/**
* Extends the given height to allow for the insets.
*
* @param height the height.
*
* @return The extended height.
*/
public double extendHeight(final double height) {
return height
+ calculateTopOutset(height) + calculateBottomOutset(height);
}
/**
* Shrinks the given rectangle by the amount of these insets.
*
* @param area the area (<code>null</code> not permitted).
*/
public void trim(final Rectangle2D area) {
final double w = area.getWidth();
final double h = area.getHeight();
final double l = calculateLeftInset(w);
final double r = calculateRightInset(w);
final double t = calculateTopInset(h);
final double b = calculateBottomInset(h);
area.setRect(area.getX() + l, area.getY() + t, w - l - r, h - t - b);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -