static Rectangle2D move(Rectangle2D rect, double dx, double dy) { return new Rectangle2D.Double(rect.getX() + dx, rect.getY() + dy, rect.getWidth(), rect.getHeight()); }
/** * This method calculates ellipse intersection with line * @param shape Bounds of this shape used to calculate parameters of inscribed into this bounds ellipse. * @param line * @return Intersection point */ private static Point getEllipseIntersection(Shape shape, Line2D.Double line) { double angle = Math.atan2(line.y2 - line.y1, line.x2 - line.x1); double x = shape.getBounds2D().getWidth() / 2 * Math.cos(angle) + shape.getBounds2D().getCenterX(); double y = shape.getBounds2D().getHeight() / 2 * Math.sin(angle) + shape.getBounds2D().getCenterY(); Point p = new Point(); p.setLocation(x, y); return p; }
x1 = x0 - state.getMax(); Rectangle2D area = new Rectangle2D.Double(x0, y0, (x1 - x0), (y1 - y0)); if(r==null || !r.intersects(area)) { r = bounds.getBounds2D(); r.add(r.getMaxX() + r.getWidth()/2, r.getCenterY()); r.add(r.getMinX() - r.getWidth()/2, r.getCenterY()); r.add(r.getCenterX(), r.getMinY() - r.getHeight()/2); r.add(r.getCenterX(), r.getMaxX() + r.getHeight()/2);
private void drawProgessBar(Graphics g, int intValue, int totalValue) { if (totalValue == 0) { return; } g.setFont(font); final String label = "" + intValue + "/" + totalValue; final Rectangle2D rect = getUsed(g, label); final int x = 10; final int y = 33; final int barWidth = 170; final int barHeight = (int) (rect.getHeight() + 2); final int grey = 230; g.setColor(new Color(grey, grey, grey)); final int value = barWidth * intValue / totalValue; g.fillRect(x, y, value, barHeight); g.setColor(Color.BLACK); g.drawRect(x, y, barWidth, barHeight); final double xLabel = x + (barWidth - rect.getWidth()) / 2; final double yLabel = y - rect.getY() + (barHeight - rect.getHeight()) / 2; g.drawString(label, (int) xLabel, (int) yLabel); }
width = pageDim.getWidth() - anchor.getX(); break; case VERTICAL: width = pageDim.getHeight() - anchor.getX(); break; case VERTICAL_270: width = anchor.getX(); break; switch (textDir) { default: width = anchor.getWidth() - leftInset - rightInset - leftMargin - rightMargin; break; case VERTICAL: case VERTICAL_270: width = anchor.getHeight() - leftInset - rightInset - leftMargin - rightMargin; break;
private void renderProgressWidget(Graphics2D graphics) { Widget bar = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_BAR).getChild(0); Rectangle2D bounds = bar.getBounds().getBounds2D(); Widget prgs = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_PROGRESS).getChild(0); int perc = (int) ((prgs.getBounds().getWidth() / bounds.getWidth()) * 100); Color color = Color.GREEN; if (perc < 25) { color = Color.RED; } String text = String.valueOf(perc) + "%"; FontMetrics fm = graphics.getFontMetrics(); Rectangle2D textBounds = fm.getStringBounds(text, graphics); int x = (int) (bounds.getX() - textBounds.getWidth()); int y = (int) (bounds.getY() + fm.getHeight() - 2); graphics.setColor(Color.BLACK); graphics.drawString(text, x + 1, y + 1); graphics.setColor(color); graphics.drawString(text, x, y); }
public Rectangle2D apply(Rectangle2D rect) { return new Rectangle2D.Double(rect.getX() + dx, rect.getY() + dy, rect.getWidth(), rect.getHeight()); }
final Rectangle2D anchorO = tx.createTransformedShape(shapeAnchor).getBounds2D(); anchorT = txs2.createTransformedShape(shapeAnchor).getBounds2D(); final double scaleX2 = safeScale(anchorO.getWidth(), anchorT.getWidth()); final double scaleY2 = safeScale(anchorO.getHeight(), anchorT.getHeight()); txs2.translate(-centerX, -centerY); normalizedShape = txs2.createTransformedShape(shapeAnchor).getBounds2D();
private BufferedImage adjustImage(BufferedImage gray) throws IOException { AffineTransform at = new AffineTransform(xform); Matrix m = new Matrix(at); at.scale(1.0 / Math.abs(m.getScalingFactorX()), 1.0 / Math.abs(m.getScalingFactorY())); Rectangle originalBounds = new Rectangle(gray.getWidth(), gray.getHeight()); Rectangle2D transformedBounds = at.createTransformedShape(originalBounds).getBounds2D(); at.preConcatenate(AffineTransform.getTranslateInstance(-transformedBounds.getMinX(), -transformedBounds.getMinY())); int width = (int) Math.ceil(transformedBounds.getWidth()); int height = (int) Math.ceil(transformedBounds.getHeight()); BufferedImage transformedGray = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY); Graphics2D g2 = (Graphics2D) transformedGray.getGraphics(); g2.drawImage(gray, at, null); g2.dispose(); return transformedGray; }
final double yy = bounds.getHeight() + bounds.getY(); double y = 0; if (ta.getOrientationAngle() == 0) { x = -0.5 * ta.getHorizontalAnchor().getFactor() * bounds.getWidth(); y = 0.5 * ta.getVerticalAnchor().getFactor() * bounds.getHeight() - yy; x = pos.x + x; y = pos.y + y; } else { x = 0.5 * ta.getVerticalAnchor().getFactor() * bounds.getHeight(); y = 0.5 * ta.getHorizontalAnchor().getFactor() * bounds.getWidth();
Rectangle2D textBounds = textLayout.getBounds(); double xOffset = chart.getPlot().getBounds().getX(); // of plot left edge double yOffset = chart.getStyler().getChartPadding(); double chartTitleBoxWidth = chart.getPlot().getBounds().getWidth(); double chartTitleBoxHeight = textBounds.getHeight() + 2 * chart.getStyler().getChartTitlePadding(); new Rectangle2D.Double(xOffset, yOffset, chartTitleBoxWidth, chartTitleBoxHeight); g.setColor(chart.getStyler().getChartTitleBoxBackgroundColor()); g.fill(rect); chart.getPlot().getBounds().getX() + (chart.getPlot().getBounds().getWidth() - textBounds.getWidth()) / 2.0; yOffset = chart.getStyler().getChartPadding() + textBounds.getHeight() + chart.getStyler().getChartTitlePadding(); g.setTransform(orig); double width = 2 * chart.getStyler().getChartTitlePadding() + textBounds.getWidth(); double height = 2 * chart.getStyler().getChartTitlePadding() + textBounds.getHeight(); bounds = new Rectangle2D.Double( xOffset - chart.getStyler().getChartTitlePadding(), yOffset - textBounds.getHeight() - chart.getStyler().getChartTitlePadding(),
@Override public Rectangle2D resizeToFitText(Graphics2D graphics) { Rectangle2D anchor = getAnchor(); if (anchor.getWidth() == 0.) { throw new POIXMLException("Anchor of the shape was not set."); } double height = getTextHeight(graphics); height += 1; // add a pixel to compensate rounding errors Insets2D insets = getInsets(); anchor.setRect(anchor.getX(), anchor.getY(), anchor.getWidth(), height + insets.top + insets.bottom); setAnchor(anchor); return anchor; }
Graphics2D g = ...; Point2D loc = ...; Font font = Font.getFont("Helvetica-bold-italic"); FontRenderContext frc = g.getFontRenderContext(); TextLayout layout = new TextLayout("This is a string", font, frc); layout.draw(g, (float)loc.getX(), (float)loc.getY()); Rectangle2D bounds = layout.getBounds(); bounds.setRect(bounds.getX()+loc.getX(), bounds.getY()+loc.getY(), bounds.getWidth(), bounds.getHeight()); g.draw(bounds);
Rectangle2D applyMargins(int[] margin, Rectangle2D bounds) { if (bounds != null) { double xmin = bounds.getMinX() - margin[3]; double ymin = bounds.getMinY() - margin[0]; double width = bounds.getWidth() + margin[1] + margin[3]; double height = bounds.getHeight() + margin[0] + margin[2]; return new Rectangle2D.Double(xmin, ymin, width, height); } else { return bounds; } }
/** * Returns a suggested value for the {@code flatness} argument in {@link * Shape#getPathIterator(AffineTransform,double)} for the specified shape. * * @param shape The shape for which to compute a flatness factor. * @return The suggested flatness factor. */ public static double getFlatness(final Shape shape) { final Rectangle2D bounds = shape.getBounds2D(); final double dx = bounds.getWidth(); final double dy = bounds.getHeight(); return max(0.025 * min(dx, dy), 0.001 * max(dx, dy)); } }