📄 abstractblock.java
字号:
public void setPadding(double top, double left, double bottom,
double right) {
setPadding(new RectangleInsets(top, left, bottom, right));
}
/**
* Arranges the contents of the block, with no constraints, and returns
* the block size.
*
* @param g2 the graphics device.
*
* @return The block size (in Java2D units, never <code>null</code>).
*/
public Size2D arrange(Graphics2D g2) {
return arrange(g2, RectangleConstraint.NONE);
}
/**
* Arranges the contents of the block, within the given constraints, and
* returns the block size.
*
* @param g2 the graphics device.
* @param constraint the constraint (<code>null</code> not permitted).
*
* @return The block size (in Java2D units, never <code>null</code>).
*/
public Size2D arrange(Graphics2D g2, RectangleConstraint constraint) {
Size2D base = new Size2D(getWidth(), getHeight());
return constraint.calculateConstrainedSize(base);
}
/**
* Returns the current bounds of the block.
*
* @return The bounds.
*/
public Rectangle2D getBounds() {
return this.bounds;
}
/**
* Sets the bounds of the block.
*
* @param bounds the bounds (<code>null</code> not permitted).
*/
public void setBounds(Rectangle2D bounds) {
if (bounds == null) {
throw new IllegalArgumentException("Null 'bounds' argument.");
}
this.bounds = bounds;
}
/**
* Calculate the width available for content after subtracting
* the margin, border and padding space from the specified fixed
* width.
*
* @param fixedWidth the fixed width.
*
* @return The available space.
*/
protected double trimToContentWidth(double fixedWidth) {
double result = this.margin.trimWidth(fixedWidth);
result = this.border.getInsets().trimWidth(result);
result = this.padding.trimWidth(result);
return Math.max(result, 0.0);
}
/**
* Calculate the height available for content after subtracting
* the margin, border and padding space from the specified fixed
* height.
*
* @param fixedHeight the fixed height.
*
* @return The available space.
*/
protected double trimToContentHeight(double fixedHeight) {
double result = this.margin.trimHeight(fixedHeight);
result = this.border.getInsets().trimHeight(result);
result = this.padding.trimHeight(result);
return Math.max(result, 0.0);
}
/**
* Returns a constraint for the content of this block that will result in
* the bounds of the block matching the specified constraint.
*
* @param c the outer constraint (<code>null</code> not permitted).
*
* @return The content constraint.
*/
protected RectangleConstraint toContentConstraint(RectangleConstraint c) {
if (c == null) {
throw new IllegalArgumentException("Null 'c' argument.");
}
if (c.equals(RectangleConstraint.NONE)) {
return c;
}
double w = c.getWidth();
Range wr = c.getWidthRange();
double h = c.getHeight();
Range hr = c.getHeightRange();
double ww = trimToContentWidth(w);
double hh = trimToContentHeight(h);
Range wwr = trimToContentWidth(wr);
Range hhr = trimToContentHeight(hr);
return new RectangleConstraint(
ww, wwr, c.getWidthConstraintType(),
hh, hhr, c.getHeightConstraintType()
);
}
private Range trimToContentWidth(Range r) {
if (r == null) {
return null;
}
double lowerBound = 0.0;
double upperBound = Double.POSITIVE_INFINITY;
if (r.getLowerBound() > 0.0) {
lowerBound = trimToContentWidth(r.getLowerBound());
}
if (r.getUpperBound() < Double.POSITIVE_INFINITY) {
upperBound = trimToContentWidth(r.getUpperBound());
}
return new Range(lowerBound, upperBound);
}
private Range trimToContentHeight(Range r) {
if (r == null) {
return null;
}
double lowerBound = 0.0;
double upperBound = Double.POSITIVE_INFINITY;
if (r.getLowerBound() > 0.0) {
lowerBound = trimToContentHeight(r.getLowerBound());
}
if (r.getUpperBound() < Double.POSITIVE_INFINITY) {
upperBound = trimToContentHeight(r.getUpperBound());
}
return new Range(lowerBound, upperBound);
}
/**
* Adds the margin, border and padding to the specified content width.
*
* @param contentWidth the content width.
*
* @return The adjusted width.
*/
protected double calculateTotalWidth(double contentWidth) {
double result = contentWidth;
result = this.padding.extendWidth(result);
result = this.border.getInsets().extendWidth(result);
result = this.margin.extendWidth(result);
return result;
}
/**
* Adds the margin, border and padding to the specified content height.
*
* @param contentHeight the content height.
*
* @return The adjusted height.
*/
protected double calculateTotalHeight(double contentHeight) {
double result = contentHeight;
result = this.padding.extendHeight(result);
result = this.border.getInsets().extendHeight(result);
result = this.margin.extendHeight(result);
return result;
}
/**
* Reduces the specified area by the amount of space consumed
* by the margin.
*
* @param area the area (<code>null</code> not permitted).
*
* @return The trimmed area.
*/
protected Rectangle2D trimMargin(Rectangle2D area) {
// defer argument checking...
this.margin.trim(area);
return area;
}
/**
* Reduces the specified area by the amount of space consumed
* by the border.
*
* @param area the area (<code>null</code> not permitted).
*
* @return The trimmed area.
*/
protected Rectangle2D trimBorder(Rectangle2D area) {
// defer argument checking...
this.border.getInsets().trim(area);
return area;
}
/**
* Reduces the specified area by the amount of space consumed
* by the padding.
*
* @param area the area (<code>null</code> not permitted).
*
* @return The trimmed area.
*/
protected Rectangle2D trimPadding(Rectangle2D area) {
// defer argument checking...
this.padding.trim(area);
return area;
}
/**
* Draws the border around the perimeter of the specified area.
*
* @param g2 the graphics device.
* @param area the area.
*/
protected void drawBorder(Graphics2D g2, Rectangle2D area) {
this.border.draw(g2, area);
}
/**
* Tests this block for equality with an arbitrary object.
*
* @param obj the object (<code>null</code> permitted).
*
* @return A boolean.
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof AbstractBlock)) {
return false;
}
AbstractBlock that = (AbstractBlock) obj;
if (!this.border.equals(that.border)) {
return false;
}
if (!this.bounds.equals(that.bounds)) {
return false;
}
if (!this.margin.equals(that.margin)) {
return false;
}
if (!this.padding.equals(that.padding)) {
return false;
}
if (this.height != that.height) {
return false;
}
if (this.width != that.width) {
return false;
}
return true;
}
/**
* Provides serialization support.
*
* @param stream the output stream.
*
* @throws IOException if there is an I/O error.
*/
private void writeObject(ObjectOutputStream stream) throws IOException {
stream.defaultWriteObject();
SerialUtilities.writeShape(this.bounds, stream);
}
/**
* Provides serialization support.
*
* @param stream the input stream.
*
* @throws IOException if there is an I/O error.
* @throws ClassNotFoundException if there is a classpath problem.
*/
private void readObject(ObjectInputStream stream)
throws IOException, ClassNotFoundException {
stream.defaultReadObject();
this.bounds = (Rectangle2D) SerialUtilities.readShape(stream);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -