@Override public void paint(Graphics2D g2) { if (snappingWidth == 0 || snappingHeight == 0) return; Color oldColor = g2.getColor(); Rectangle2D.Double bounds = getBounds(g2); g2.setColor(Color.WHITE); g2.fill(bounds); g2.setColor(ThemeManager.getInstance().getTheme().getGridColor()); Stroke oldStroke = g2.getStroke(); for (double x = bounds.getX(); x < bounds.getMaxX(); x += snappingWidth) g2.draw(new Line2D.Double(x, bounds.getY(), x, bounds.getMaxY())); for (double y = bounds.getY(); y < bounds.getMaxY(); y += snappingHeight) g2.draw(new Line2D.Double(bounds.getX(), y, bounds.getMaxX(), y)); g2.setStroke(oldStroke); g2.setColor(oldColor); }
widht = freeR.getMaxX() - rIntersection.getMaxX(); height = freeR.getHeight(); if (widht > 0) {
symbol_width = Math.max(symbol_width, (int) rect.getMaxX()); symbol_height = Math.max(symbol_height, (int) rect.getHeight());
symbol_width = Math.max(symbol_width, (int) rect.getMaxX()); symbol_height = Math.max(symbol_height, (int) rect.getHeight());
public static Line2D.Double intersection(Line2D.Double line, Rectangle2D.Double bounds) { //line.x1, line.y1, line.x2, line.y2 Point2D.Double bottom = computeSegmentIntersection(line.x1, line.y1, line.x2, line.y2, bounds.getMinX(), bounds.getMaxY(), bounds.getMaxX(), bounds.getMaxY()); Point2D.Double right = computeSegmentIntersection(line.x1, line.y1, line.x2, line.y2, bounds.getMaxX(), bounds.getMaxY(), bounds.getMaxX(), bounds.getMinY()); Point2D.Double top = computeSegmentIntersection(line.x1, line.y1, line.x2, line.y2, bounds.getMinX(), bounds.getMinY(), bounds.getMaxX(), bounds.getMinY()); Point2D.Double left = computeSegmentIntersection(line.x1, line.y1, line.x2, line.y2, bounds.getMinX(), bounds.getMinY(), bounds.getMinX(), bounds.getMaxY()); ArrayList<Point2D.Double> pts = new ArrayList<Point2D.Double>(); if (bottom != null) { pts.add(bottom); } if (right != null) { pts.add(right); } if (top != null) { pts.add(top); } if (left != null) { pts.add(left); } if (pts.size() != 2) { return null; } else { return new Line2D.Double(pts.get(0), pts.get(1)); } }
child.setBounds( new Point2D.Double(r.getMinX(), r.getMinY()), new Point2D.Double(r.getMaxX(), r.getMaxY()) ); child.changed();
/** * Zooms to the boundaries of a particular box. * @param gc associated component * @param newMin min of zoom box * @param newMax max of zoom box * @return timer running the animation */ public static javax.swing.Timer zoomCoordBoxAnimated(final JGraphicComponent gc, final Point2D newMin, final Point2D newMax) { final Rectangle2D.Double rect = getLocalBounds(gc); final double xMin = rect.getX(); final double yMin = rect.getY(); final double xMax = rect.getMaxX(); final double yMax = rect.getMaxY(); final double nxMin = newMin.getX(); final double nyMin = newMin.getY(); final double nxMax = newMax.getX(); final double nyMax = newMax.getY(); return AnimationStep.animate(0, ANIM_STEPS, ANIM_DELAY_MILLIS, new AnimationStep(){ @Override @InvokedFromThread("AnimationStep") public void run(int idx, double pct) { double x1 = xMin + (nxMin - xMin) * pct; double y1 = yMin + (nyMin - yMin) * pct; double x2 = xMax + (nxMax - xMax) * pct; double y2 = yMax + (nyMax - yMax) * pct; setDesiredLocalBounds(gc, new Rectangle2D.Double(x1, y1, x2 - x1, y2 - y1)); } }); }
private void drawTextBox(Graphics2D g2D, String text, int x0, int y0, Color color) { final FontMetrics fontMetrics = g2D.getFontMetrics(); final Rectangle2D textBounds = fontMetrics.getStringBounds(text, g2D); x0 -= textBounds.getWidth() / 2; textBounds.setRect(textBounds.getX() - 1, textBounds.getY(), textBounds.getWidth(), textBounds.getHeight()); Rectangle2D.Double r = new Rectangle2D.Double(x0 + textBounds.getX() - 2.0, y0 + textBounds.getY() - 2.0, textBounds.getWidth() + 4.0, textBounds.getHeight() + 4.0); if (r.getMaxX() > getWidth()) { r.setRect(getWidth() - r.getWidth(), r.getY(), r.getWidth(), r.getHeight()); } if (r.getMinX() < 0) { r.setRect(0, r.getY(), r.getWidth(), r.getHeight()); } if (r.getMaxY() > getHeight()) { r.setRect(r.getX(), getHeight() - r.getHeight(), r.getWidth(), r.getHeight()); } if (r.getMinY() < 0) { r.setRect(r.getX(), 0, r.getWidth(), r.getHeight()); } g2D.setColor(color); g2D.fill(r); g2D.setColor(Color.black); g2D.draw(r); g2D.drawString(text, x0, y0); }
private void drawTextBox(Graphics2D g2D, String text, int x0, int y0, Color color) { final FontMetrics fontMetrics = g2D.getFontMetrics(); final Rectangle2D textBounds = fontMetrics.getStringBounds(text, g2D); x0 -= textBounds.getWidth() / 2; textBounds.setRect(textBounds.getX() - 1, textBounds.getY(), textBounds.getWidth(), textBounds.getHeight()); Rectangle2D.Double r = new Rectangle2D.Double(x0 + textBounds.getX() - 2.0, y0 + textBounds.getY() - 2.0, textBounds.getWidth() + 4.0, textBounds.getHeight() + 4.0); if (r.getMaxX() > getWidth()) { r.setRect(getWidth() - r.getWidth(), r.getY(), r.getWidth(), r.getHeight()); } if (r.getMinX() < 0) { r.setRect(0, r.getY(), r.getWidth(), r.getHeight()); } if (r.getMaxY() > getHeight()) { r.setRect(r.getX(), getHeight() - r.getHeight(), r.getWidth(), r.getHeight()); } if (r.getMinY() < 0) { r.setRect(r.getX(), 0, r.getWidth(), r.getHeight()); } g2D.setColor(color); g2D.fill(r); g2D.setColor(Color.black); g2D.draw(r); g2D.drawString(text, x0, y0); }
/** * Divides the rectangular space where a piece was just placed following the * maximal rectangles splitting strategy. Method called from * {@link #boundingBoxPacking} * * @param usedFreeArea rectangular area that contains the newly placed piece. * @param justPlacedPieceBB bounding box of the newly placed piece. * @param freeRectangles list of free spaces in the bin. */ private void splitScheme(Rectangle2D.Double usedFreeArea, Rectangle2D.Double justPlacedPieceBB, ArrayList<Rectangle2D.Double> freeRectangles) { freeRectangles.remove(usedFreeArea); // top double widht = usedFreeArea.getWidth(); double height = justPlacedPieceBB.getY() - usedFreeArea.getY(); if (height > 0) { Rectangle2D.Double upR = new Rectangle2D.Double(usedFreeArea.getX(), usedFreeArea.getY(), widht, height); freeRectangles.add(upR); } // right widht = usedFreeArea.getMaxX() - justPlacedPieceBB.getMaxX(); height = usedFreeArea.getHeight(); if (widht > 0) { Rectangle2D.Double rightR = new Rectangle2D.Double(justPlacedPieceBB.getMaxX(), usedFreeArea.getY(), widht, height); freeRectangles.add(rightR); } }
private Rectangle2D getTextBox(final Graphics2D g2d, final String text, final int x0, final int y0) { final FontMetrics fontMetrics = g2d.getFontMetrics(); final Rectangle2D textBounds = fontMetrics.getStringBounds(text, g2d); Rectangle2D.Double r = new Rectangle2D.Double(x0 + textBounds.getX() - 2.0, y0 + textBounds.getY() - 2.0, textBounds.getWidth() + 4.0, textBounds.getHeight() + 4.0); if (r.getMaxX() > getWidth()) { r.setRect(getWidth() - r.getWidth(), r.getY(), r.getWidth(), r.getHeight()); } if (r.getMinX() < 0) { r.setRect(0, r.getY(), r.getWidth(), r.getHeight()); } if (r.getMaxY() > getHeight()) { r.setRect(r.getX(), getHeight() - r.getHeight(), r.getWidth(), r.getHeight()); } if (r.getMinY() < 0) { r.setRect(r.getX(), 0, r.getWidth(), r.getHeight()); } return r; }
/** * Moves the figure. This is the * method that subclassers override. Clients usually * call displayBox. */ @Override public void setBounds(Point2D.Double anchor, Point2D.Double lead) { if (getLayouter() == null) { super.setBounds(anchor, lead); basicSetPresentationFigureBounds(anchor, lead); } else { Rectangle2D.Double r = getLayouter().layout(this, anchor, lead); basicSetPresentationFigureBounds(new Point2D.Double(r.getX(), r.getY()), new Point2D.Double( Math.max(lead.x, (int) r.getMaxX()), Math.max(lead.y, (int) r.getMaxY()))); invalidate(); } }
@Test public void testCorrectBoundsIfNecessary_noCorrection() { final Rectangle2D.Double toCorrect = new Rectangle2D.Double(-175.0, -85.0, 140.0, 110.0); RegionSelectableWorldMapPane.correctBoundsIfNecessary(toCorrect); assertEquals(-175.0, toCorrect.getMinX(), 1e-8); assertEquals(-85.0, toCorrect.getMinY(), 1e-8); assertEquals(-175.0 + 140.0, toCorrect.getMaxX(), 1e-8); assertEquals(-85.0 + 110.0, toCorrect.getMaxY(), 1e-8); }
@Test public void testCorrectBoundsIfNecessary_correction_positive_x() { final Rectangle2D.Double toCorrect = new Rectangle2D.Double(-175.499991, -90.0, 360.0, 180.0); RegionSelectableWorldMapPane.correctBoundsIfNecessary(toCorrect); assertEquals(-175.499991, toCorrect.getMinX(), 1e-8); assertEquals(180.0, toCorrect.getMaxX(), 1e-8); assertEquals(-90.0, toCorrect.getMinY(), 1e-8); assertEquals(90.0, toCorrect.getMaxY(), 1e-8); assertTrue(toCorrect.getMinX() + toCorrect.getWidth() <= 180.0); assertTrue(toCorrect.getMinY() + toCorrect.getHeight() <= 90.0); }
@Test public void testCorrectBoundsIfNecessary_correction_negative_x() { final Rectangle2D.Double toCorrect = new Rectangle2D.Double(-185.499991, -90.0, 360.0, 180.0); RegionSelectableWorldMapPane.correctBoundsIfNecessary(toCorrect); assertEquals(-180.0, toCorrect.getMinX(), 1e-8); assertEquals(174.50000899999895, toCorrect.getMaxX(), 1e-8); assertEquals(-90.0, toCorrect.getMinY(), 1e-8); assertEquals(90.0, toCorrect.getMaxY(), 1e-8); assertTrue(toCorrect.getMinX() + toCorrect.getWidth() <= 180.0); assertTrue(toCorrect.getMinY() + toCorrect.getHeight() <= 90.0); } }