@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); }
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); }
/** * 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); }
protected void paintEdges(Graphics g, Tree parent) { if (!getTree().isLeaf(parent)) { BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); ((Graphics2D)g).setStroke(stroke); Rectangle2D.Double parentBounds = getBoundsOfNode(parent); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.getMaxY(); for (Tree child : getTree().getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMinY(); if (getUseCurvedEdges()) { CubicCurve2D c = new CubicCurve2D.Double(); double ctrlx1 = x1; double ctrly1 = (y1+y2)/2; double ctrlx2 = x2; double ctrly2 = y1; c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); ((Graphics2D) g).draw(c); } else { g.drawLine((int) x1, (int) y1, (int) x2, (int) y2); } paintEdges(g, child); } } }
protected void paintEdges(Graphics g, Tree parent) { if (!getTree().isLeaf(parent)) { BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); ((Graphics2D)g).setStroke(stroke); Rectangle2D.Double parentBounds = getBoundsOfNode(parent); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.getMaxY(); for (Tree child : getTree().getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMinY(); if (getUseCurvedEdges()) { CubicCurve2D c = new CubicCurve2D.Double(); double ctrlx1 = x1; double ctrly1 = (y1+y2)/2; double ctrlx2 = x2; double ctrly2 = y1; c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); ((Graphics2D) g).draw(c); } else { g.drawLine((int) x1, (int) y1, (int) x2, (int) y2); } paintEdges(g, child); } } }
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();
protected void paintEdges(Graphics g, Tree parent) { if (!getTree().isLeaf(parent)) { BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); ((Graphics2D)g).setStroke(stroke); Rectangle2D.Double parentBounds = getBoundsOfNode(parent); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.getMaxY(); for (Tree child : getTree().getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMinY(); if (getUseCurvedEdges()) { CubicCurve2D c = new CubicCurve2D.Double(); double ctrlx1 = x1; double ctrly1 = (y1+y2)/2; double ctrlx2 = x2; double ctrly2 = y1; c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); ((Graphics2D) g).draw(c); } else { g.drawLine((int) x1, (int) y1, (int) x2, (int) y2); } paintEdges(g, child); } } }
protected void paintEdges(Graphics g, Tree parent) { if (!getTree().isLeaf(parent)) { BasicStroke stroke = new BasicStroke(1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); ((Graphics2D)g).setStroke(stroke); Rectangle2D.Double parentBounds = getBoundsOfNode(parent); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.getMaxY(); for (Tree child : getTree().getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMinY(); if (getUseCurvedEdges()) { CubicCurve2D c = new CubicCurve2D.Double(); double ctrlx1 = x1; double ctrly1 = (y1+y2)/2; double ctrlx2 = x2; double ctrly2 = y1; c.setCurve(x1, y1, ctrlx1, ctrly1, ctrlx2, ctrly2, x2, y2); ((Graphics2D) g).draw(c); } else { g.drawLine((int) x1, (int) y1, (int) x2, (int) y2); } paintEdges(g, child); } } }
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(); } }
protected void generateEdges(Tree parent) { if (!getTree().isLeaf(parent)) { Rectangle2D.Double parentBounds = getBoundsOfNode(parent); // System.out.println("%% parent("+getText(parent)+")="+parentBounds); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.y; for (Tree child : getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); // System.out.println("%% child("+getText(child)+")="+childBounds); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMaxY(); doc.line(x1, y1, x2, y2); generateEdges(child); } } }
protected void generateEdges(Tree parent) { if (!getTree().isLeaf(parent)) { Rectangle2D.Double parentBounds = getBoundsOfNode(parent); // System.out.println("%% parent("+getText(parent)+")="+parentBounds); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.y; for (Tree child : getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); // System.out.println("%% child("+getText(child)+")="+childBounds); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMaxY(); doc.line(x1, y1, x2, y2); generateEdges(child); } } }
protected void generateEdges(Tree parent) { if (!getTree().isLeaf(parent)) { Rectangle2D.Double parentBounds = getBoundsOfNode(parent); // System.out.println("%% parent("+getText(parent)+")="+parentBounds); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.y; for (Tree child : getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); // System.out.println("%% child("+getText(child)+")="+childBounds); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMaxY(); doc.line(x1, y1, x2, y2); generateEdges(child); } } }
protected void generateEdges(Tree parent) { if (!getTree().isLeaf(parent)) { Rectangle2D.Double parentBounds = getBoundsOfNode(parent); // System.out.println("%% parent("+getText(parent)+")="+parentBounds); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.y; for (Tree child : getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); // System.out.println("%% child("+getText(child)+")="+childBounds); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMaxY(); doc.line(x1, y1, x2, y2); generateEdges(child); } } }
protected void generateEdges(Tree parent) { if (!getTree().isLeaf(parent)) { Rectangle2D.Double parentBounds = getBoundsOfNode(parent); // System.out.println("%% parent("+getText(parent)+")="+parentBounds); double x1 = parentBounds.getCenterX(); double y1 = parentBounds.y; for (Tree child : getChildren(parent)) { Rectangle2D.Double childBounds = getBoundsOfNode(child); // System.out.println("%% child("+getText(child)+")="+childBounds); double x2 = childBounds.getCenterX(); double y2 = childBounds.getMaxY(); doc.line(x1, y1, x2, y2); generateEdges(child); } } }
@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_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); } }
@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); }