下面列出了java.awt.geom.Rectangle2D#createUnion ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public ProoferClient() {
try {
this.robot = new Robot();
} catch (AWTException e) {
System.err.println("Error getting robot.");
e.printStackTrace();
System.exit(1);
}
GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice[] screenDevices = environment.getScreenDevices();
Rectangle2D tempBounds = new Rectangle();
for (GraphicsDevice screenDevice : screenDevices) {
tempBounds = tempBounds.createUnion(
screenDevice.getDefaultConfiguration().getBounds());
}
screenBounds = tempBounds.getBounds();
}
@Override
public Rectangle2D getBoundingBox() {
//Returns null if linePoints ArrayList does not have any points
if (points.size() < 1) {
return null;
}
//Retrieves bounding box of first point of line
Point firstPoint = getPointN(0);
//Assign bounding box to variable
Rectangle2D bb = firstPoint.getBoundingBox();
//Iterate over remaining points and retrieve bounding boxes
for (Point point : points) {
//creates new bounding box that includes previous points and current point
bb = bb.createUnion(point.getBoundingBox());
}
return bb;
}
public static Rectangle2D.Double getDiffRect(Rectangle2D rr, Rectangle2D rp)
{
if (rr != null && rp != null) {
Rectangle2D union = rr.createUnion(rp);
double y = union.getMinY();
double rx = rr.getMaxX();
double px = rp.getMinX();
double ry = union.getMinY();
double py = union.getMaxY();
Rectangle2D.Double res = new Rectangle2D.Double(
rx < px ? rx : px,
ry < py ? ry : py,
Math.abs(px - rx),
Math.abs(py - ry));
// System.out.println("getDiffRect (1)" + rr);
// System.out.println("getDiffRect (2)" + rp);
// System.out.println("getDiffRect (out)" + res);
return res;
} else {
return null;
}
}
public static Rectangle2D getBoundingRect(ExtendedMolecule[] mols)
{
if (mols == null || mols.length == 0) {
return new Rectangle2D.Double(0,0,0,0);
}
Rectangle2D r = getBoundingRect(mols[0]);
for (int i = 1; i < mols.length; i++) {
Rectangle2D t = getBoundingRect(mols[i]);
if (t != null) {
if (r == null)
r = t;
else
r = r.createUnion(getBoundingRect(mols[i]));
}
}
if (r == null)
r = new Rectangle2D.Double(0,0,0,0);
return r;
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
public Rectangle2D getBounds() {
Rectangle2D boundingBox = null;
for (Color color : collector.getColors()) {
for (Line2D seg : collector.getSegments(color)) {
Rectangle2D shapeBox = seg.getBounds2D();
if (boundingBox == null) {
boundingBox = shapeBox;
}
boundingBox = boundingBox.createUnion(shapeBox);
}
}
return boundingBox;
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
/**
* Creates a LayoutPaintable.
*
* @param width
* the width of this entire Paintable. This must be greater than
* the rightmost edge of all inner cells.
* @param height
* the height of this entire Paintable. This must be greater than
* the bottom-most edge of all inner cells.
* @param rects
* an array of cell bounds. Negative widths and heights are used
* to indicate a cell should be flipped horizontally/vertically.
* @param cells
* an array of cells, each element corresponding to the previous
* array of destination rectangles.
* @param scaleProportionally
* whether each paintable should be scaled to full fit the cell
* bounds, or whether it should be scaled proportionally. It is
* recommended this value be true, although this may result in
* "dead space" in each cell.
*/
LayoutPaintable(int width, int height, Rectangle2D[] rects,
Paintable[] cells, boolean scaleProportionally) {
if (rects.length != cells.length)
throw new IllegalArgumentException("the number of rects ("
+ rects.length + ") must equal the number of cells ("
+ cells.length + ")");
this.width = width;
this.height = height;
this.rects = new Rectangle2D[rects.length];
this.cells = new Paintable[rects.length];
this.scaleProportionally = scaleProportionally;
System.arraycopy(rects, 0, this.rects, 0, rects.length);
System.arraycopy(cells, 0, this.cells, 0, rects.length);
Rectangle2D sum = null;
for (int a = 0; a < rects.length; a++) {
if (sum == null) {
sum = new Rectangle2D.Double(rects[a].getX(), rects[a].getY(),
rects[a].getWidth(), rects[a].getHeight());
} else {
sum = sum.createUnion(rects[a]);
}
}
if (sum.getX() + sum.getWidth() > width
|| sum.getY() + sum.getHeight() > height) {
for (int a = 0; a < rects.length; a++) {
System.err.println("rects[" + a + "] = " + rects[a]);
}
throw new IllegalArgumentException(
"the width and height specified ("
+ width
+ "x"
+ height
+ ") do not enclose the sum of all the cell bounds ( "
+ sum + " )");
}
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
/**
* Recalculates the bounding box by taking the union of the bounding boxes of all children. Caches the result.
*/
public void calcBoundingBox() throws SVGException {
// Rectangle2D retRect = new Rectangle2D.Float();
Rectangle2D retRect = null;
for (final Object element : children) {
final SVGElement ele = (SVGElement) element;
if (ele instanceof RenderableElement) {
final RenderableElement rendEle = (RenderableElement) ele;
final Rectangle2D bounds = rendEle.getBoundingBox();
if (bounds != null) {
if (retRect == null) {
retRect = bounds;
} else {
retRect = retRect.createUnion(bounds);
}
}
}
}
// If no contents, use degenerate rectangle
if (retRect == null) {
retRect = new Rectangle2D.Float();
}
boundingBox = boundsToParent(retRect);
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
@Override
public Rectangle2D getBoundingBox() {
if (geometryList.size() < 1) {
return null;
}
Geometry firstGeom = getGeometryN(0);
Rectangle2D bb = firstGeom.getBoundingBox();
for (Geometry geometry : geometryList) {
bb = bb.createUnion(geometry.getBoundingBox());
}
return bb;
}
public static Rectangle2D getProductsBoundingRect(Reaction r)
{
if (r == null)
throw new NullPointerException("Cannot pass null reaction");
int rn = r.getProducts();
Rectangle2D rc = null;
for (int i = 0; i < rn; i++) {
Rectangle2D rt = getBoundingRect(r.getProduct(i));
if (rc != null && rt != null)
rc = rc.createUnion(rt);
else
rc = rt;
}
return rc;
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
/**
* Performs the layout with no constraint, so the content size is
* determined by the bounds of the shape and/or line drawn to represent
* the series.
*
* @param g2 the graphics device.
*
* @return The content size.
*/
protected Size2D arrangeNN(Graphics2D g2) {
Rectangle2D contentSize = new Rectangle2D.Double();
if (this.line != null) {
contentSize.setRect(this.line.getBounds2D());
}
if (this.shape != null) {
contentSize = contentSize.createUnion(this.shape.getBounds2D());
}
return new Size2D(contentSize.getWidth(), contentSize.getHeight());
}
/**
* Paint a demo paintable.
*
* @param g
* the Graphics2D to paint to.
* @param width
* the width of the painted area
* @param height
* the height of the painted area
* @param colors
* two colors to paint in a checkerboard pattern on the
* background
* @param id
* text to paint in dark gray in the center
*/
public static void paint(Graphics2D g, int width, int height,
Color[] colors, String id) {
if (colors == null)
colors = DemoPaintable.colors;
if (colors.length == 2) {
g.setColor(colors[0]);
g.fillRect(0, 0, width / 2, height / 2);
g.setColor(colors[1]);
g.fillRect(width / 2, 0, width / 2, height / 2);
g.setColor(colors[1]);
g.fillRect(0, height / 2, width / 2, height / 2);
g.setColor(colors[0]);
g.fillRect(width / 2, height / 2, width / 2, height / 2);
} else {
g.setColor(colors[(id.hashCode() + 0) % colors.length]);
g.fillRect(0, 0, width / 2, height / 2);
g.setColor(colors[(id.hashCode() + 1) % colors.length]);
g.fillRect(width / 2, 0, width / 2, height / 2);
g.setColor(colors[(id.hashCode() + 2) % colors.length]);
g.fillRect(0, height / 2, width / 2, height / 2);
g.setColor(colors[(id.hashCode() + 3) % colors.length]);
g.fillRect(width / 2, height / 2, width / 2, height / 2);
}
// When testing printing, we should allow for
// double-digit numbers, so let's make sure "MM" can fit
GlyphVector maxGv = font.createGlyphVector(frc, "MM");
Shape maxShape = maxGv.getOutline();
GeneralPath maxPath = new GeneralPath(maxShape);
Rectangle2D maxR = ShapeBounds.getBounds(maxPath.getPathIterator(null),
null);
GlyphVector gv = font.createGlyphVector(frc, id);
Shape shape = gv.getOutline();
GeneralPath path = new GeneralPath(shape);
Rectangle2D r = ShapeBounds.getBounds(path.getPathIterator(null), null);
maxR = maxR.createUnion(r); // in case we're not testing printing
// anymore
AffineTransform magnify = createAffineTransform(maxR,
new Rectangle2D.Double(0, 0, width, height));
// don't stretch the text to fill the page:
// instead stretch it to fill the page proportionally
double zoom = Math.min(magnify.getScaleX(), magnify.getScaleY());
zoom = zoom * .8; // add some padding on the sides
magnify = createAffineTransform(r,
new Rectangle2D.Double(width / 2 - r.getWidth() / 2 * zoom,
height / 2 - r.getHeight() / 2 * zoom, r.getWidth()
* zoom, r.getHeight() * zoom));
path.transform(magnify);
g.setColor(Color.darkGray);
g.fill(path);
}
private Rectangle2D createGeoRectangle(GeoPosition southWest, GeoPosition northEast) {
Rectangle2D tmp = new Rectangle2D.Double(southWest.getLatitude(), southWest.getLongitude(), 0, 0);
tmp = tmp.createUnion(new Rectangle2D.Double(northEast.getLatitude(), northEast.getLongitude(), 0, 0));
return tmp;
}