下面列出了java.awt.Shape#getBounds2D ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns a rectangle that encloses the axis label. This is typically
* used for layout purposes (it gives the maximum dimensions of the label).
*
* @param g2 the graphics device.
* @param edge the edge of the plot area along which the axis is measuring.
*
* @return The enclosing rectangle.
*/
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
Rectangle2D result = new Rectangle2D.Double();
String axisLabel = getLabel();
if (axisLabel != null && !axisLabel.equals("")) {
FontMetrics fm = g2.getFontMetrics(getLabelFont());
Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
RectangleInsets insets = getLabelInsets();
bounds = insets.createOutsetRectangle(bounds);
double angle = getLabelAngle();
if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
angle = angle - Math.PI / 2.0;
}
double x = bounds.getCenterX();
double y = bounds.getCenterY();
AffineTransform transformer
= AffineTransform.getRotateInstance(angle, x, y);
Shape labelBounds = transformer.createTransformedShape(bounds);
result = labelBounds.getBounds2D();
}
return result;
}
/**
* Returns a rectangle that encloses the axis label. This is typically
* used for layout purposes (it gives the maximum dimensions of the label).
*
* @param g2 the graphics device.
* @param edge the edge of the plot area along which the axis is measuring.
*
* @return The enclosing rectangle.
*/
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
Rectangle2D result = new Rectangle2D.Double();
String axisLabel = getLabel();
if (axisLabel != null && !axisLabel.equals("")) {
FontMetrics fm = g2.getFontMetrics(getLabelFont());
Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
RectangleInsets insets = getLabelInsets();
bounds = insets.createOutsetRectangle(bounds);
double angle = getLabelAngle();
if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
angle = angle - Math.PI / 2.0;
}
double x = bounds.getCenterX();
double y = bounds.getCenterY();
AffineTransform transformer
= AffineTransform.getRotateInstance(angle, x, y);
Shape labelBounds = transformer.createTransformedShape(bounds);
result = labelBounds.getBounds2D();
}
return result;
}
/**
* Returns a rectangle that encloses the axis label. This is typically
* used for layout purposes (it gives the maximum dimensions of the label).
*
* @param g2 the graphics device.
* @param edge the edge of the plot area along which the axis is measuring.
*
* @return The enclosing rectangle.
*/
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
Rectangle2D result = new Rectangle2D.Double();
String axisLabel = getLabel();
if (axisLabel != null && !axisLabel.equals("")) {
FontMetrics fm = g2.getFontMetrics(getLabelFont());
Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
RectangleInsets insets = getLabelInsets();
bounds = insets.createOutsetRectangle(bounds);
double angle = getLabelAngle();
if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
angle = angle - Math.PI / 2.0;
}
double x = bounds.getCenterX();
double y = bounds.getCenterY();
AffineTransform transformer
= AffineTransform.getRotateInstance(angle, x, y);
Shape labelBounds = transformer.createTransformedShape(bounds);
result = labelBounds.getBounds2D();
}
return result;
}
/**
* Returns a rectangle that encloses the axis label. This is typically
* used for layout purposes (it gives the maximum dimensions of the label).
*
* @param g2 the graphics device.
* @param edge the edge of the plot area along which the axis is measuring.
*
* @return The enclosing rectangle.
*/
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
Rectangle2D result = new Rectangle2D.Double();
String axisLabel = getLabel();
if (axisLabel != null && !axisLabel.equals("")) {
FontMetrics fm = g2.getFontMetrics(getLabelFont());
Rectangle2D bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
RectangleInsets insets = getLabelInsets();
bounds = insets.createOutsetRectangle(bounds);
double angle = getLabelAngle();
if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
angle = angle - Math.PI / 2.0;
}
double x = bounds.getCenterX();
double y = bounds.getCenterY();
AffineTransform transformer
= AffineTransform.getRotateInstance(angle, x, y);
Shape labelBounds = transformer.createTransformedShape(bounds);
result = labelBounds.getBounds2D();
}
return result;
}
private void reportFailure(String id, Shape good, Shape bad) {
Rectangle2D r = good.getBounds2D();
r.add(bad.getBounds2D());
BufferedImage image = new BufferedImage(500, 500,
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.transform(RectangularTransform.create(r,
new Rectangle(0, 0, image.getWidth(), image.getHeight())));
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(Color.blue);
g.fill(good);
g.setColor(new Color(255, 0, 0, 128));
g.fill(bad);
g.dispose();
printStream.println("the resulting shape for \"" + id
+ "\" wasn't correct.");
try {
ImageIO.write(image, "png", new File("comparison " + (ctr++)
+ ".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Creates a simple vertical gradient using the shape for bounds and the
* colors for top, two middle, and bottom colors.
*
* @param s the shape to use for bounds.
* @param colors the colors to use for the gradient.
*
* @return the gradient.
*/
protected Paint createVerticalGradient(Shape s, FourColors colors) {
Rectangle2D bounds = s.getBounds2D();
float xCenter = (float) bounds.getCenterX();
float yMin = (float) bounds.getMinY();
float yMax = (float) bounds.getMaxY();
return createGradient(xCenter, yMin, xCenter, yMax, new float[] { 0f, 0.45f, 0.62f, 1f },
new Color[] { colors.top, colors.upperMid, colors.lowerMid, colors.bottom });
}
private Paint decodeSplitPaneDividerBorderGradient(Shape s, Color border1, Color border2) {
Rectangle2D bounds = s.getBounds2D();
float midX = (float) bounds.getCenterX();
float y = (float) bounds.getY();
float h = (float) bounds.getHeight();
return createGradient(midX, y, midX, y + h, new float[] { 0.20645161f, 0.5f, 0.7935484f }, new Color[] {
border1,
decodeColor(border1, border2, 0.5f),
border2 });
}
/**
* Returns a rectangle that encloses the axis label. This is typically
* used for layout purposes (it gives the maximum dimensions of the label).
*
* @param g2 the graphics device.
* @param edge the edge of the plot area along which the axis is measuring.
*
* @return The enclosing rectangle.
*/
protected Rectangle2D getLabelEnclosure(Graphics2D g2, RectangleEdge edge) {
Rectangle2D result = new Rectangle2D.Double();
Rectangle2D bounds = null;
if (this.attributedLabel != null) {
TextLayout layout = new TextLayout(
this.attributedLabel.getIterator(),
g2.getFontRenderContext());
bounds = layout.getBounds();
} else {
String axisLabel = getLabel();
if (axisLabel != null && !axisLabel.equals("")) {
FontMetrics fm = g2.getFontMetrics(getLabelFont());
bounds = TextUtilities.getTextBounds(axisLabel, g2, fm);
}
}
if (bounds != null) {
RectangleInsets insets = getLabelInsets();
bounds = insets.createOutsetRectangle(bounds);
double angle = getLabelAngle();
if (edge == RectangleEdge.LEFT || edge == RectangleEdge.RIGHT) {
angle = angle - Math.PI / 2.0;
}
double x = bounds.getCenterX();
double y = bounds.getCenterY();
AffineTransform transformer
= AffineTransform.getRotateInstance(angle, x, y);
Shape labelBounds = transformer.createTransformedShape(bounds);
result = labelBounds.getBounds2D();
}
return result;
}
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, Vector displacement)
throws IOException {
super.showGlyph(textRenderingMatrix, font, code, displacement);
Shape shape = calculateGlyphBounds(textRenderingMatrix, font, code);
if (shape != null) {
Rectangle2D rect = shape.getBounds2D();
add(rect);
}
}
/**
* Draws the bounds around the image.
*
* @param panel the drawing panel
* @param g the graphics context
*/
private void drawFixedBounds(DrawingPanel panel, Graphics g) {
Point2D pt = new Point2D.Double(x, y);
pt = toPixels.transform(pt, pt);
Shape temp = new Rectangle2D.Double(pt.getX()-width/2, pt.getY()-height/2, width, height);
computeFixedHotSpots(temp.getBounds2D());
pixelBounds = temp.getBounds2D();
if(theta!=0) {
pixelBounds = AffineTransform.getRotateInstance(-theta, pt.getX(), pt.getY()).createTransformedShape(pixelBounds);
}
if(!selected) {
return;
}
Graphics2D g2 = ((Graphics2D) g);
g2.setPaint(boundsColor);
g2.draw(pixelBounds);
if(xyDrag) {
g2.fillRect((int) hotSpots[CENTER].getX()-delta, (int) hotSpots[CENTER].getY()-delta, d2, d2);
g2.setColor(edgeColor);
g2.fillOval((int) hotSpots[CENTER].getX()-1, (int) hotSpots[CENTER].getY()-1, 3, 3);
g2.setPaint(boundsColor);
}
if(rotateDrag) {
g2.fillOval((int) hotSpots[CORNER].getX()-delta, (int) hotSpots[CORNER].getY()-delta, d2, d2);
}
if(heightDrag) {
g2.fillRect((int) hotSpots[TOP].getX()-delta, (int) hotSpots[TOP].getY()-delta, d2, d2);
g2.fillRect((int) hotSpots[BOTTOM].getX()-delta, (int) hotSpots[BOTTOM].getY()-delta, d2, d2);
}
if(widthDrag) {
g2.fillRect((int) hotSpots[LEFT].getX()-delta, (int) hotSpots[LEFT].getY()-delta, d2, d2);
g2.fillRect((int) hotSpots[RIGHT].getX()-delta, (int) hotSpots[RIGHT].getY()-delta, d2, d2);
}
g.setColor(Color.BLACK);
}
@Override
protected void showGlyph(Matrix textRenderingMatrix, PDFont font, int code, Vector displacement)
throws IOException {
super.showGlyph(textRenderingMatrix, font, code, displacement);
Shape shape = calculateGlyphBounds(textRenderingMatrix, font, code);
if (shape != null) {
Rectangle2D rect = shape.getBounds2D();
addVerticalUseSection(rect.getMinY(), rect.getMaxY());
}
}
private Paint createToolbarToggleButtonGradient(Shape s, TwoColors colors) {
Rectangle2D bounds = s.getBounds2D();
float x = (float) bounds.getX();
float y = (float) bounds.getY();
float w = (float) bounds.getWidth();
float h = (float) bounds.getHeight();
return createGradient((0.5f * w) + x, y, (0.5f * w) + x, h + y, new float[] { 0f, 0.35f, 0.65f, 1f }, new Color[] {
colors.top,
colors.bottom,
colors.bottom,
colors.top });
}
@Override
public void apply(Graphics2D g, Shape clipShape, int width, int height) {
// opacity support added by lbalazscs
Composite savedComposite = g.getComposite();
if (opacity < 1.0f) {
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
}
// create a rect to hold the bounds
Rectangle2D clipShapeBounds = clipShape.getBounds2D();
if (clipShapeBounds.isEmpty()) {
// check added by lbalazscs
return;
}
width = (int) (clipShapeBounds.getWidth() + clipShapeBounds.getX());
height = (int) (clipShapeBounds.getHeight() + clipShapeBounds.getY());
Rectangle effectBounds = new Rectangle(0, 0, width + 2, height + 2);
if (effectBounds.isEmpty()) {
// check added by lbalazscs
// this can be empty even if the clip shape bounds is not
// when the clip shape starts at large negative coordinates
return;
}
// Apply the border glow effect
BufferedImage clipImage = getClipImage(effectBounds);
Graphics2D g2 = clipImage.createGraphics();
// lbalazscs: moved here from getClipImage
// in order to avoid two createGraphics calls
g2.clearRect(0, 0, clipImage.getWidth(), clipImage.getHeight());
try {
// clear the buffer
g2.setPaint(Color.BLACK);
g2.setComposite(AlphaComposite.Clear);
g2.fillRect(0, 0, effectBounds.width, effectBounds.height);
// turn on smoothing
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
paintBorderGlow(g2, clipShape, width, height);
// clip out the parts we don't want
g2.setComposite(AlphaComposite.Clear);
g2.setColor(Color.WHITE);
// clip the outside
Area area = new Area(effectBounds);
area.subtract(new Area(clipShape));
g2.fill(area);
} finally {
// draw the final image
g2.dispose();
}
g.drawImage(clipImage, 0, 0, null);
//g.setColor(Color.MAGENTA);
//g.draw(clipShape.getBounds2D());
//g.drawRect(0,0,width,height);
g.setComposite(savedComposite);
}
/**
* Add the rectangle 'rect' to the area representing
* the part of the page which is drawn into.
*/
private void addDrawingRect(Rectangle2D rect) {
/* For testing purposes the following line can be uncommented.
When uncommented it causes the entire page to be rasterized
thus eliminating errors caused by a faulty bounding box
calculation.
*/
//mDrawingArea.addInfinite();
AffineTransform matrix = getTransform();
Shape transShape = matrix.createTransformedShape(rect);
Rectangle2D transRect = transShape.getBounds2D();
mDrawingArea.add((float) transRect.getMinY(),
(float) transRect.getMaxY());
}
/**
* Add the rectangle 'rect' to the area representing
* the part of the page which is drawn into.
*/
private void addDrawingRect(Rectangle2D rect) {
/* For testing purposes the following line can be uncommented.
When uncommented it causes the entire page to be rasterized
thus eliminating errors caused by a faulty bounding box
calculation.
*/
//mDrawingArea.addInfinite();
AffineTransform matrix = getTransform();
Shape transShape = matrix.createTransformedShape(rect);
Rectangle2D transRect = transShape.getBounds2D();
mDrawingArea.add((float) transRect.getMinY(),
(float) transRect.getMaxY());
}
/**
* Add the rectangle 'rect' to the area representing
* the part of the page which is drawn into.
*/
private void addDrawingRect(Rectangle2D rect) {
/* For testing purposes the following line can be uncommented.
When uncommented it causes the entire page to be rasterized
thus eliminating errors caused by a faulty bounding box
calculation.
*/
//mDrawingArea.addInfinite();
AffineTransform matrix = getTransform();
Shape transShape = matrix.createTransformedShape(rect);
Rectangle2D transRect = transShape.getBounds2D();
mDrawingArea.add((float) transRect.getMinY(),
(float) transRect.getMaxY());
}
/**
* Add the rectangle 'rect' to the area representing
* the part of the page which is drawn into.
*/
private void addDrawingRect(Rectangle2D rect) {
/* For testing purposes the following line can be uncommented.
When uncommented it causes the entire page to be rasterized
thus eliminating errors caused by a faulty bounding box
calculation.
*/
//mDrawingArea.addInfinite();
AffineTransform matrix = getTransform();
Shape transShape = matrix.createTransformedShape(rect);
Rectangle2D transRect = transShape.getBounds2D();
mDrawingArea.add((float) transRect.getMinY(),
(float) transRect.getMaxY());
}
/**
* Add the rectangle 'rect' to the area representing
* the part of the page which is drawn into.
*/
private void addDrawingRect(Rectangle2D rect) {
/* For testing purposes the following line can be uncommented.
When uncommented it causes the entire page to be rasterized
thus eliminating errors caused by a faulty bounding box
calculation.
*/
//mDrawingArea.addInfinite();
AffineTransform matrix = getTransform();
Shape transShape = matrix.createTransformedShape(rect);
Rectangle2D transRect = transShape.getBounds2D();
mDrawingArea.add((float) transRect.getMinY(),
(float) transRect.getMaxY());
}
/**
* Add the rectangle 'rect' to the area representing
* the part of the page which is drawn into.
*/
private void addDrawingRect(Rectangle2D rect) {
/* For testing purposes the following line can be uncommented.
When uncommented it causes the entire page to be rasterized
thus eliminating errors caused by a faulty bounding box
calculation.
*/
//mDrawingArea.addInfinite();
AffineTransform matrix = getTransform();
Shape transShape = matrix.createTransformedShape(rect);
Rectangle2D transRect = transShape.getBounds2D();
mDrawingArea.add((float) transRect.getMinY(),
(float) transRect.getMaxY());
}
/**
* Creates a simple horizontal gradient using the shape for bounds and the
* colors for top and bottom colors.
*
* @param s the shape to use for bounds.
* @param colors the colors to use for the gradient.
*
* @return the gradient.
*/
protected Paint createHorizontalGradient(Shape s, TwoColors colors) {
Rectangle2D bounds = s.getBounds2D();
float xMin = (float) bounds.getMinX();
float xMax = (float) bounds.getMaxX();
float yCenter = (float) bounds.getCenterY();
return createGradient(xMin, yCenter, xMax, yCenter, new float[] { 0f, 1f }, new Color[] { colors.top, colors.bottom });
}