Refine search
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);
protected TextString convertToCharacters(String text, Graphics2D g, FontFactory fontFactory, ColorFactory colorFactory) { TextString characters = new TextString(); FontRenderContext frc = g.getFontRenderContext(); double lastx = 0; for (int i = 0; i < text.length(); i++) { Font font = fontFactory.getFont(i); char c = text.charAt(i); FontMetrics fm = g.getFontMetrics(font); Rectangle2D bounds = font.getStringBounds(String.valueOf(c), frc); TextCharacter tc = new TextCharacter(); tc.setCharacter(c); tc.setFont(font); tc.setWidth(fm.charWidth(c)); tc.setHeight(fm.getAscent() + fm.getDescent()); tc.setAscent(fm.getAscent()); tc.setDescent(fm.getDescent()); tc.setX(lastx); tc.setY(0); tc.setFont(font); tc.setColor(colorFactory.getColor(i)); lastx += bounds.getWidth(); characters.addCharacter(tc); } return characters; }
private int calculateOffsetForTop(Graphics2D g2d, String text) { if (topTextOffsetX == -1) { // calculate once FontRenderContext frc = g2d.getFontRenderContext(); int textWidth = (int) textFont.getStringBounds(text, frc).getWidth(); int neededImageWidth = (topTextImage == null ? 0 : topTextImage.getWidth(this)); int availableXWidth = imageSize.width - neededImageWidth; topTextOffsetX = (availableXWidth - textWidth) / 2 + neededImageWidth; } return topTextOffsetX; }
private int calculateOffset(Graphics2D g2d) { if (textOffsetX == -1) { // calculate once FontRenderContext frc = g2d.getFontRenderContext(); int textWidth = (int) textFont.getStringBounds(text, frc).getWidth(); if (textWidth > buttonSize.width) { g2d.setFont(textFontMini); useMiniFont = true; frc = g2d.getFontRenderContext(); textWidth = (int) textFontMini.getStringBounds(text, frc).getWidth(); } if (alignTextLeft) { textOffsetX = 0; } else { textOffsetX = (imageSize.width - textWidth) / 2; } } return textOffsetX; }
public static void drawCenteredShadowText(Graphics2D g2, String s, int x, int y, Color shadowColor) { FontRenderContext frc = g2.getFontRenderContext(); Rectangle2D bounds = g2.getFont().getStringBounds(s, frc); int leftX = (int) (x - (float) bounds.getWidth() / 2); drawShadowText(g2, s, leftX, y, shadowColor, 1); }
final int length = message.length(); final Font font = graphics.getFont(); final FontRenderContext context = graphics.getFontRenderContext(); for (int i = 0; i < length; ) { int ir = message.indexOf('\r', i); final GlyphVector line = font.createGlyphVector(context, message.substring(i, irn)); final Rectangle2D rect = line.getVisualBounds(); final double w = rect.getWidth(); if (w > width) width = w; height += rect.getHeight();
&& chart.getStyler().isYAxisTitleVisible()) { FontRenderContext frc = g.getFontRenderContext(); TextLayout nonRotatedTextLayout = new TextLayout(yAxisTitle, chart.getStyler().getAxisTitleFont(), frc); (int) (yAxis.getAxisTick().getBounds().getX() + yAxis.getAxisTick().getBounds().getWidth() + nonRotatedRectangle.getHeight()); } else { ((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()); && chart.getStyler().isXAxisTitleVisible()) { FontRenderContext frc = g.getFontRenderContext(); TextLayout textLayout = new TextLayout(chart.getXAxisTitle(), chart.getStyler().getAxisTitleFont(), frc);
FontRenderContext frc = g.getFontRenderContext(); TextLayout textLayout = new TextLayout(chart.getTitle(), chart.getStyler().getChartTitleFont(), frc); double chartTitleBoxWidth = chart.getPlot().getBounds().getWidth(); double chartTitleBoxHeight = textBounds.getHeight() + 2 * chart.getStyler().getChartTitlePadding(); + (chart.getPlot().getBounds().getWidth() - textBounds.getWidth()) / 2.0; yOffset = chart.getStyler().getChartPadding()
: 0); } else { double xWidth = yAxis.getAxisTitle().getBounds().getWidth(); xOffset = yAxis.getAxisTitle().getBounds().getX() + xWidth; && flippedTickLocation > yOffset FontRenderContext frc = g.getFontRenderContext(); TextLayout axisLabelTextLayout = new TextLayout(tickLabel, styler.getAxisTickLabelsFont(), frc); Rectangle2D tickLabelBounds = axisLabelTextLayout.getBounds(); double boundWidth = tickLabelBounds.getWidth(); if (boundWidth > maxTickLabelWidth) { maxTickLabelWidth = boundWidth; double boundWidth = tickLabelBounds.getWidth(); double xPos; switch (styler.getYAxisLabelAlignment()) { FontRenderContext frc = g.getFontRenderContext(); TextLayout textLayout = new TextLayout(tickLabel, styler.getAxisTickLabelsFont(), frc); FontRenderContext frc = g.getFontRenderContext(); TextLayout textLayout = new TextLayout(tickLabel, styler.getAxisTickLabelsFont(), frc);
private boolean fontFits(Font font, double width, Graphics2D g2) { String baseChar = "G"; Rectangle2D charRect = font.getStringBounds(baseChar, g2.getFontRenderContext()); if (charRect.getWidth() > width) return false; else return true; }
/** * Patterned off GraphPane.drawMessageHelper, draws a string centred in the given box. */ public static void drawMessage(Graphics2D g2, String message, Rectangle2D box){ FontMetrics metrics = g2.getFontMetrics(); Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext()); float x = (float)(box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0); float y = (float)(box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0); g2.drawString(message, x, y); } }
private static Rectangle2D computeBoundsUsingNormalizedFont( String string, Graphics2D g) { Font normalizedFont = g.getFont().deriveFont(1f); Rectangle2D bounds = normalizedFont.getStringBounds(string, g.getFontRenderContext()); float scale = g.getFont().getSize2D(); return new Rectangle2D.Double(bounds.getX() * scale, bounds.getY() * scale, bounds.getWidth() * scale, bounds.getHeight() * scale); }
/** * Draws a string centred in the given box. */ public static void drawCentred(Graphics2D g2, String message, Rectangle2D box) { FontMetrics metrics = g2.getFontMetrics(); Rectangle2D stringBounds = g2.getFont().getStringBounds(message, g2.getFontRenderContext()); float x = (float) (box.getX() + (box.getWidth() - stringBounds.getWidth()) / 2.0); float y = (float) (box.getY() + (box.getHeight() + metrics.getAscent() - metrics.getDescent()) / 2.0); g2.drawString(message, x, y); }
protected int getWidth(String title) { if (g2d == null) return title.length()*8 + 30; Font font = g2d.getFont(); int width = (int) font.getStringBounds(title, g2d.getFontRenderContext()).getWidth(); return width + 30; }
public static Font calculateMaxFontForString(Graphics2D graphics2D, Font font, int maxWidth, double maxHeight, String text) { float fontSize = font.getSize(); FontRenderContext fontRenderContext = graphics2D.getFontRenderContext(); Rectangle2D bounds = font.getStringBounds(text, fontRenderContext); while (bounds.getWidth() > maxWidth || bounds.getHeight()>maxHeight) { font = font.deriveFont((fontSize -= 2)); bounds = font.getStringBounds(text, fontRenderContext); } return font; } }
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()); }
private void drawMessageHelper(Graphics2D g2, String message, Font font, int w, int h, int offset) { g2.setFont(font); FontMetrics metrics = g2.getFontMetrics(); Rectangle2D stringBounds = font.getStringBounds(message, g2.getFontRenderContext()); int preferredWidth = (int) stringBounds.getWidth() + metrics.getHeight(); int preferredHeight = (int) stringBounds.getHeight() + metrics.getHeight(); w = Math.min(preferredWidth, w); h = Math.min(preferredHeight, h); int x = (getWidth() - (int) stringBounds.getWidth()) / 2; int y = (getHeight() / 2) + ((metrics.getAscent() - metrics.getDescent()) / 2) + offset; g2.drawString(message, x, y); }
private static Font addTracking(Font baseFont, double maxTextWidth, String text, Graphics2D g2d) { FontRenderContext frc = g2d.getFontRenderContext(); double originalWidth = baseFont.getStringBounds(text, frc).getWidth(); double extraSpace = maxTextWidth - originalWidth; double extraSpacePerGap = extraSpace / (text.length() - 1); double tracking = extraSpacePerGap / baseFont.getSize2D(); return baseFont.deriveFont(Collections.singletonMap(TextAttribute.TRACKING, tracking)); } }
/** Returns a rectangle enclosing the pixels affected by drawString * assuming it is drawn at (x=0, y=0). As drawString draws above the drawing location, * the y coordinate of the rectangle is negative. */ public Rectangle getStringBounds(String s) { setupFontMetrics(); GlyphVector gv = font.createGlyphVector(fmGraphics.getFontRenderContext(), s); Rectangle2D rect = gv.getPixelBounds(null, 0.f, -fontMetrics.getDescent()); return new Rectangle((int)rect.getX(), (int)rect.getY(), (int)rect.getWidth(), (int)rect.getHeight()); }
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);