private void renderWidgetText(Graphics2D graphics, Rectangle bounds, int itemId, Color color) { if (itemId == -1) { return; } String text = itemId + ""; FontMetrics fm = graphics.getFontMetrics(); Rectangle2D textBounds = fm.getStringBounds(text, graphics); int textX = (int) (bounds.getX() + (bounds.getWidth() / 2) - (textBounds.getWidth() / 2)); int textY = (int) (bounds.getY() + (bounds.getHeight() / 2) + (textBounds.getHeight() / 2)); graphics.setColor(Color.BLACK); graphics.drawString(text, textX + 1, textY + 1); graphics.setColor(color); graphics.drawString(text, textX, textY); }
public void draw(Graphics2D graphics) { // the coordinate system of this group of shape Rectangle2D interior = getShape().getInteriorAnchor(); // anchor of this group relative to the parent shape Rectangle2D exterior = getShape().getAnchor(); AffineTransform tx = (AffineTransform)graphics.getRenderingHint(Drawable.GROUP_TRANSFORM); AffineTransform tx0 = new AffineTransform(tx); double scaleX = interior.getWidth() == 0. ? 1.0 : exterior.getWidth() / interior.getWidth(); double scaleY = interior.getHeight() == 0. ? 1.0 : exterior.getHeight() / interior.getHeight(); tx.translate(exterior.getX(), exterior.getY()); tx.scale(scaleX, scaleY); tx.translate(-interior.getX(), -interior.getY()); DrawFactory drawFact = DrawFactory.getInstance(graphics); AffineTransform at2 = graphics.getTransform(); for (Shape<?,?> child : getShape()) { // remember the initial transform and restore it after we are done with the drawing AffineTransform at = graphics.getTransform(); graphics.setRenderingHint(Drawable.GSAVE, true); Drawable draw = drawFact.getDrawable(child); draw.applyTransform(graphics); draw.draw(graphics); // restore the coordinate system graphics.setTransform(at); graphics.setRenderingHint(Drawable.GRESTORE, true); } graphics.setTransform(at2); graphics.setRenderingHint(Drawable.GROUP_TRANSFORM, tx0); }
final Rectangle2D bounds = shape.getBounds2D(); final double ww = bounds.getMaxX() - bounds.getMinX(); final double hh = bounds.getMaxY() - bounds.getMinY(); destination = new BufferedImage((int) w, (int) h, BufferedImage.TYPE_INT_ARGB); final Graphics2D gg = destination.createGraphics(); gg.scale(dpiFactor, dpiFactor); gg.translate(deltaShadow - bounds.getMinX(), deltaShadow - bounds.getMinY()); final boolean isLine = shape instanceof Line2D.Double; if (isLine) { gg.setColor(colorLine); gg.draw(shape); } else { final AffineTransform at = g2d.getTransform(); g2d.scale(1 / dpiFactor, 1 / dpiFactor); g2d.drawImage(destination, (int) (bounds.getMinX() * dpiFactor), (int) (bounds.getMinY() * dpiFactor), null); g2d.setTransform(at);
/** * Fills the given {@link RectF} with the path bounds. * * @param bounds the RectF to be filled. */ public void fillBounds(RectF bounds) { Rectangle2D rect = mPath.getBounds2D(); bounds.left = (float) rect.getMinX(); bounds.right = (float) rect.getMaxX(); bounds.top = (float) rect.getMinY(); bounds.bottom = (float) rect.getMaxY(); }
static Rectangle2D move(Rectangle2D rect, double dx, double dy) { return new Rectangle2D.Double(rect.getX() + dx, rect.getY() + dy, rect.getWidth(), rect.getHeight()); }
Rectangle2D annotationRectangle = textLayout.getBounds(); double x = dataPoint.x + dataPoint.w / 2 - annotationRectangle.getWidth() / 2 - MARGIN; double y = dataPoint.y - 3 * MARGIN - annotationRectangle.getHeight(); double w = annotationRectangle.getWidth() + 2 * MARGIN; double h = annotationRectangle.getHeight() + 2 * MARGIN; double halfHeight = h / 2; g.setColor(styler.getToolTipHighlightColor()); g.fill(dataPoint.shape); y = Math.max(y, topEdge); y = Math.min(y, bottomEdge - h); Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h); g.setColor(styler.getToolTipBackgroundColor()); g.fill(rectangle); g.setFont(styler.getToolTipFont()); AffineTransform orig = g.getTransform(); AffineTransform at = new AffineTransform(); at.translate(x + MARGIN - 1, y + MARGIN - 1 + halfHeight); g.transform(at); g.fill(shape);
graphics.setPaint(fillPaint); Rectangle2D cellAnc = tc.getAnchor(); graphics.fill(cellAnc); continue; graphics.setStroke(getStroke(stroke)); Paint linePaint = drawPaint.getPaint(graphics, stroke.getPaint()); graphics.setPaint(linePaint); double x=cellAnc.getX(), y=cellAnc.getY(), w=cellAnc.getWidth(), h=cellAnc.getHeight(); Line2D line; switch (edge) { default: case bottom: line = new Line2D.Double(x-borderSize, y+h, x+w+borderSize, y+h); break; case left: line = new Line2D.Double(x, y, x, y+h+borderSize); break; case right: line = new Line2D.Double(x+w, y, x+w, y+h+borderSize); break; case top:
boolean isScaledUp = pdImage.getWidth() < Math.round(at.getScaleX()) || pdImage.getHeight() < Math.round(at.getScaleY()); graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); Rectangle2D bounds = at.createTransformedShape(unitRect).getBounds2D(); BufferedImage renderedPaint = new BufferedImage((int) Math.ceil(bounds.getWidth()), (int) Math.ceil(bounds.getHeight()), BufferedImage.TYPE_INT_ARGB); Graphics2D g = (Graphics2D) renderedPaint.getGraphics(); g.translate(-bounds.getMinX(), -bounds.getMinY()); g.setPaint(paint); g.fill(bounds); g.dispose(); BufferedImage renderedMask = new BufferedImage((int) Math.ceil(bounds.getWidth()), (int) Math.ceil(bounds.getHeight()), BufferedImage.TYPE_INT_RGB); g = (Graphics2D) renderedMask.getGraphics(); g.translate(-bounds.getMinX(), -bounds.getMinY()); AffineTransform imageTransform = new AffineTransform(at); imageTransform.scale(1.0 / mask.getWidth(), -1.0 / mask.getHeight()); graphics.drawImage(renderedPaint, AffineTransform.getTranslateInstance(bounds.getMinX(), bounds.getMinY()), null);
if (ga instanceof TextAttributes) { final TextAttributes ta = (TextAttributes) ga; final Color currentColor = _graphics.getColor(); Color fontColor = ta.getTextColor(); if (fontColor == null) { fontColor = _defaultColor; _graphics.setColor(fontColor); final double scale = _graphics.getTransform().getScaleX(); final String str = text.getText(); AffineTransform before = _graphics.getTransform(); _graphics.setTransform(new AffineTransform()); fontTransform.rotate(-ta.getOrientationAngle() * Math.PI / 180); 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();
g.setFont(chart.getStyler().getChartTitleFont()); 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); chart.getPlot().getBounds().getX() + (chart.getPlot().getBounds().getWidth() - textBounds.getWidth()) / 2.0; yOffset = chart.getStyler().getChartPadding() + textBounds.getHeight() + chart.getStyler().getChartTitlePadding(); 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(), width, height);
at.translate(x, y); -(icon.getIconWidth() * icoStyle.getAnchorPointX()), (icon.getIconHeight() * (icoStyle.getAnchorPointY() - 1))); graphics.setTransform(at); new Rectangle2D.Double( x, y, icon.getIconWidth(), icon.getIconHeight())); labelCache.put(transformedShape.getBounds2D()); if (anchorPoint != null) { alignedRect = new Rectangle2D.Double( Math.round(anchorPoint.getX()), Math.round(anchorPoint.getY()), cornerRect.getWidth(), cornerRect.getHeight()); } else { alignedRect = new Rectangle2D.Double( 0.0, 0.0, cornerRect.getWidth(), cornerRect.getHeight());
clip.intersect(new Area(transformedBox)); Rectangle2D clipRect = clip.getBounds2D(); if (clipRect.isEmpty()) return; this.bbox = new PDRectangle((float)clipRect.getX(), (float)clipRect.getY(), (float)clipRect.getWidth(), (float)clipRect.getHeight()); AffineTransform dpiTransform = AffineTransform.getScaleInstance(Math.abs(m.getScalingFactorX()), Math.abs(m.getScalingFactorY())); Rectangle2D bounds = dpiTransform.createTransformedShape(clip.getBounds2D()).getBounds2D(); minX = (int) Math.floor(bounds.getMinX()); minY = (int) Math.floor(bounds.getMinY()); maxX = (int) Math.floor(bounds.getMaxX()) + 1; maxY = (int) Math.floor(bounds.getMaxY()) + 1; xform = AffineTransform.getScaleInstance(Math.abs(m.getScalingFactorX()), Math.abs(m.getScalingFactorY())); PDRectangle pageSizeOriginal = pageSize; pageSize = new PDRectangle(minX / Math.abs(m.getScalingFactorX()), minY / Math.abs(m.getScalingFactorY()), (float) bounds.getWidth() / Math.abs(m.getScalingFactorX()), (float) bounds.getHeight() / Math.abs(m.getScalingFactorY())); int clipWindingRuleOriginal = clipWindingRule; clipWindingRule = -1; g.translate(-clipRect.getX(), -clipRect.getY());
xOffset = (int) (yAxis.getAxisTick().getBounds().getX() + yAxis.getAxisTick().getBounds().getWidth() + nonRotatedRectangle.getHeight()); } else { xOffset = (int) (yAxis.getBounds().getX() + nonRotatedRectangle.getHeight()); ((yAxis.getBounds().getHeight() + nonRotatedRectangle.getWidth()) / 2.0 + yAxis.getBounds().getY()); new Rectangle2D.Double( xOffset - nonRotatedRectangle.getHeight(), yOffset - nonRotatedRectangle.getWidth(), nonRotatedRectangle.getHeight() + chart.getStyler().getAxisTitlePadding(), nonRotatedRectangle.getWidth()); new Rectangle2D.Double( yAxis.getBounds().getX(), yAxis.getBounds().getY(), 0, yAxis.getBounds().getHeight()); chart.getXAxis().getBounds().getX() + (chart.getXAxis().getBounds().getWidth() - rectangle.getWidth()) / 2.0; double yOffset = chart.getXAxis().getBounds().getY() + chart.getXAxis().getBounds().getHeight() - rectangle.getHeight();
private void renderWidgetOverlay(Graphics2D graphics, Portal portal, String text, Color color) { Widget shield = client.getWidget(portal.getShield()); Widget icon = client.getWidget(portal.getIcon()); Widget hp = client.getWidget(portal.getHitpoints()); Widget bar = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_BAR).getChild(0); Rectangle2D barBounds = bar.getBounds().getBounds2D(); // create one rectangle from two different widget bounds Rectangle2D bounds = union(shield.getBounds().getBounds2D(), icon.getBounds().getBounds2D()); bounds = union(bounds, hp.getBounds().getBounds2D()); graphics.setColor(color); graphics.draw(new Rectangle2D.Double(bounds.getX(), bounds.getY() - 2, bounds.getWidth(), bounds.getHeight() - 3)); FontMetrics fm = graphics.getFontMetrics(); Rectangle2D textBounds = fm.getStringBounds(text, graphics); int x = (int) (bounds.getX() + (bounds.getWidth() / 2) - (textBounds.getWidth() / 2)); int y = (int) (bounds.getY() + bounds.getHeight() + textBounds.getHeight() + barBounds.getHeight()); graphics.setColor(Color.BLACK); graphics.drawString(text, x + 1, y + 5); graphics.setColor(color); graphics.drawString(text, x, y + 4); }
g2.setFont(getTickLabelFont(tick.getCategory())); g2.setPaint(getTickLabelPaint(tick.getCategory())); x1 = x0 - state.getMax(); Rectangle2D area = new Rectangle2D.Double(x0, y0, (x1 - x0), (y1 - y0)); if(r==null || !r.intersects(area)) { Point2D anchorPoint = RectangleAnchor.coordinates(area, position.getCategoryAnchor()); TextBlock block = tick.getLabel(); block.draw(g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getLabelAnchor(), (float) anchorPoint.getX(), (float) anchorPoint.getY(), position.getAngle()); Shape bounds = block.calculateBounds(g2, 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 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; }
private void paintSum(Graphics2D g, Rectangle2D pieBounds, double total) { // draw total value if visible if (pieStyler.isSumVisible()) { SumFormatter sumFormatter = pieStyler.getSumFormatter(); String annotation = sumFormatter.format(total); TextLayout textLayout = new TextLayout( annotation, pieStyler.getSumFont(), new FontRenderContext(null, true, false)); Shape shape = textLayout.getOutline(null); g.setColor(pieStyler.getChartFontColor()); // compute center Rectangle2D annotationRectangle = textLayout.getBounds(); double xCenter = pieBounds.getX() + pieBounds.getWidth() / 2 - annotationRectangle.getWidth() / 2; double yCenter = pieBounds.getY() + pieBounds.getHeight() / 2 + annotationRectangle.getHeight() / 2; // set text AffineTransform orig = g.getTransform(); AffineTransform at = new AffineTransform(); at.translate(xCenter, yCenter); g.transform(at); g.fill(shape); g.setTransform(orig); } } }
private void drawTextWithWhiteBackGround(Graphics2D g2, String text, Point2D p) { TextLayout tl = new TextLayout(text, g2.getFont(), g2.getFontRenderContext()); g2.setColor(Color.WHITE); Rectangle2D textBounds = tl.getBounds(); g2.fill(new Rectangle2D.Double(textBounds.getX() + p.getX(), textBounds.getY() + p.getY(), textBounds.getWidth(), textBounds.getHeight())); g2.setColor(Color.BLACK); tl.draw(g2, (float) p.getX(), (float) p.getY()); }
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);