final Color extended = mapper.getMappedColor(fontConfiguration.getExtendedColor()); if (extended != null) { g2d.setColor(extended); g2d.setBackground(extended); g2d.fill(new Rectangle2D.Double(x, y - dimBack.getHeight() + 1.5, dimBack.getWidth(), dimBack .getHeight())); g2d.setFont(font.getFont()); g2d.setColor(mapper.getMappedColor(fontConfiguration.getColor())); final TextLayout t = new TextLayout(shape.getText(), font.getFont(), fontRenderContext); g2d.translate(x, y); g2d.fill(t.getOutline(null)); g2d.translate(-x, -y);
FontRenderContext frc = graphics.getFontRenderContext(); Font font = graphics.getFont(); AffineTransform oldTr = graphics.getTransform(); graphics.translate(0, -bounds.height + graphics.getFontMetrics().getMaxCharBounds(graphics) .getHeight()); TextLayout layout = new TextLayout(line, font, frc); if (layout.isLeftToRight()) { layout.draw(graphics, 0, nextY); } else { layout.draw(graphics, (float) (bounds.width - layout.getAdvance()), nextY); nextY += layout.getAscent() + layout.getDescent() + layout.getLeading();
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);
/** * @return the leading height before/after a text line */ public float getLeading() { // fix invalid leadings (leading == 0) double l = layout.getLeading(); if (l == 0) { // see https://stackoverflow.com/questions/925147 // we use a 115% value instead of the 120% proposed one, as this seems to be closer to LO/OO l = (layout.getAscent()+layout.getDescent())*0.15; } return (float)l; }
public void draw(Graphics2D graphics){ if(str == null) { return; } double yBaseline = y + layout.getAscent(); Integer textMode = (Integer)graphics.getRenderingHint(Drawable.TEXT_RENDERING_MODE); if(textMode != null && textMode == Drawable.TEXT_AS_SHAPES){ layout.draw(graphics, (float)x, (float)yBaseline); } else { graphics.drawString(str.getIterator(), (float)x, (float)yBaseline ); } }
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 drawErrorText(String errStr, int w, Graphics2D g2) { g2.setColor(colorModel.getColor("alertColor")); g2.setFont(new Font(res.getString("g2_Font"), Font.BOLD, 20)); g2.drawString(res.getString("ERROR"), 5, 20); AttributedString as = new AttributedString(errStr); as.addAttribute(TextAttribute.FONT, font12, 0, errStr.length()); AttributedCharacterIterator aci = as.getIterator(); FontRenderContext frc = g2.getFontRenderContext(); LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc); float x = 5, y = 25; lbm.setPosition(0); while (lbm.getPosition() < errStr.length()) { TextLayout tl = lbm.nextLayout(w - x - 5); if (!tl.isLeftToRight()) { x = w - tl.getAdvance(); } tl.draw(g2, x, y += tl.getAscent()); y += tl.getDescent() + tl.getLeading(); } }
Font f = g.getFont(); g.setFont(f.deriveFont(sap.getFontSize())); g.setColor(Color.BLACK); Graphics2D graphics2D = (Graphics2D)g; GraphicsEnvironment.getLocalGraphicsEnvironment(); AttributedString messageAS = new AttributedString(message); messageAS.addAttribute(TextAttribute.FONT, g.getFont()); AttributedCharacterIterator messageIterator = messageAS.getIterator(); FontRenderContext messageFRC = graphics2D.getFontRenderContext(); LineBreakMeasurer messageLBM = new LineBreakMeasurer(messageIterator, messageFRC); while (messageLBM.getPosition() < messageIterator.getEndIndex()) { textLayout = messageLBM.nextLayout(wrappingWidth); y += textLayout.getAscent(); if ( object.getAlignment().equals(TextObject.LEFT)){ textLayout.draw(graphics2D, x , y); textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth()) , y); textLayout.draw(graphics2D, x + (float) (wrappingWidth - textLayout.getBounds().getWidth())/2 , y); y += textLayout.getDescent() + textLayout.getLeading(); x = sap.getxOrigin() + insets.left;
protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.RED); g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight()); g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); FontRenderContext context = g2d.getFontRenderContext(); Font font = new Font("Arial", Font.BOLD, 48); TextLayout txt = new TextLayout(text, font, context); Rectangle2D bounds = txt.getBounds(); int x = (int) ((getWidth() - (int) bounds.getWidth()) / 2); int y = (int) ((getHeight() - (bounds.getHeight() - txt.getDescent())) / 2); y += txt.getAscent() - txt.getDescent(); g2d.setFont(font); g2d.setColor(Color.BLACK); g2d.drawString(text, x, y); g2d.setColor(Color.BLUE); g2d.translate(x, y); g2d.draw(bounds);
public static TextLayout getTextLayout(String text, Font font) { // create temporary text image and get the text layout final int w = font.getSize() * text.length() + 20; final int h = font.getSize() + 20; BufferedImage buffImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB); Graphics2D g = buffImg.createGraphics(); g.setComposite(AlphaComposite.Src); g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); g.setFont(font); FontRenderContext ctx = g.getFontRenderContext(); TextLayout layout = new TextLayout(text, font, ctx); g.dispose(); return layout; }
FontRenderContext frc = g.getFontRenderContext(); AttributedCharacterIterator textIter = text.getIterator(); LineBreakMeasurer measure = new LineBreakMeasurer(textIter, frc); float yPos = y; float remain = h; AttributedCharacterIterator newLineCheck = text.getIterator(); while (measure.getPosition() < textIter.getEndIndex()) { newLineCheck.setIndex(measure.getPosition()); char ch; while ((ch = newLineCheck.next()) != CharacterIterator.DONE) { TextLayout layout = measure.nextLayout(availWidth, newLineCheck.getIndex(), false); float ascent = layout.getAscent(); yPos += ascent; remain -= ascent; g.setColor(Color.black); layout.draw(g, x + inset, yPos); yPos += layout.getDescent() + layout.getLeading() - 2; 4); g.setColor(new Color(0, 0, 0, 128)); g.fillRect(x + 2, y + advance + 1, w - 2, 1); g.setColor(Color.black);
public SystemFontMetrics(String fontName) { BufferedImage img = new BufferedImage(40, 40, BufferedImage.TYPE_4BYTE_ABGR); Graphics2D graphics = GraphicsEnvironment.getLocalGraphicsEnvironment().createGraphics(img); FontRenderContext fontRenderContext = graphics.getFontRenderContext(); this.font = new Font(fontName, Font.PLAIN, 1000); double maxHeight = 0; for (int i = 0; i < 255; i++) { TextLayout layout = new TextLayout(Character.toString((char)i), font, fontRenderContext); maxHeight = Math.max(maxHeight, layout.getBounds().getHeight()); super.widths[i] = (int)layout.getAdvance(); } super.maxCharHeight = (int)Math.round(maxHeight); }
public void paintIcon( Component c, Graphics g, int x, int y ){ g.setColor( c.getForeground() ); drawArrow( g, x+1, y+1 ); drawArrow( g, x+4, y+1 ); String text; if( size > 100 ){ text = "+"; } else{ text = String.valueOf( size ); } Font font = g.getFont(); GenericFontModifier modifier = new GenericFontModifier(); modifier.setSizeDelta( false ); modifier.setSize( 8 ); g.setFont( modifier.modify( font ) ); TextLayout layout = new TextLayout( text, g.getFont(), ((Graphics2D)g).getFontRenderContext() ); Rectangle2D bounds = layout.getBounds(); layout.draw( (Graphics2D)g, (float)(x + getIconWidth() - bounds.getWidth() - bounds.getX() - 1), (float)(y + getIconHeight() - bounds.getHeight() - bounds.getY() - 1 )); g.setFont( font ); }
public void drawCentralizedText(String text) { Graphics2D graphics = image.createGraphics(); try { graphics.setFont(font); graphics.setRenderingHint(KEY_TEXT_ANTIALIASING, VALUE_TEXT_ANTIALIAS_ON); FontRenderContext context = graphics.getFontRenderContext(); TextLayout layout = new TextLayout(text, font, context); int x = width / 2 - (int) (layout.getAdvance() / 2f); int y = height / 2 + (int) (layout.getAscent() / 2f) - 1; graphics.setColor(Color.DARK_GRAY.darker()); graphics.drawString(text, x-1, y-1); graphics.setColor(Color.WHITE); graphics.drawString(text, x, y); } finally { graphics.dispose(); } }
void paintSeriesText( Graphics2D g, Map<String, Rectangle2D> seriesTextBounds, int markerSize, double x, double starty) { g.setColor(chart.getStyler().getChartFontColor()); g.setFont(chart.getStyler().getLegendFont()); double multiLineOffset = 0.0; for (Map.Entry<String, Rectangle2D> entry : seriesTextBounds.entrySet()) { double height = entry.getValue().getHeight(); double centerOffsetY = (Math.max(markerSize, height) - height) / 2.0; FontRenderContext frc = g.getFontRenderContext(); TextLayout tl = new TextLayout(entry.getKey(), chart.getStyler().getLegendFont(), frc); Shape shape = tl.getOutline(null); AffineTransform orig = g.getTransform(); AffineTransform at = new AffineTransform(); at.translate(x, starty + height + centerOffsetY + multiLineOffset); g.transform(at); g.fill(shape); g.setTransform(orig); // // debug box // Rectangle2D boundsTemp = new Rectangle2D.Double(x, starty + centerOffsetY, // entry.getValue().getWidth(), height); // g.setColor(Color.blue); // g.draw(boundsTemp); multiLineOffset += height + MULTI_LINE_SPACE; } }
Graphics g = img.getGraphics(); Graphics2D graphics = (Graphics2D) g; graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); graphics.setFont(font); FontMetrics fm = graphics.getFontMetrics(); String character = Character.toString(characters[i]); g.setColor(Color.WHITE); g.fillRect(0, 0, characterSize.width, characterSize.height); g.setColor(Color.BLACK); Rectangle rect = new TextLayout(character, fm.getFont(), fm.getFontRenderContext()).getOutline(null).getBounds(); g.drawString(character, 0,
@Override public void drawText(String text, int x, int y, Paint paint) { if (text == null || text.trim().isEmpty()) { return; } if (paint.isTransparent()) { return; } AwtPaint awtPaint = AwtGraphicFactory.getPaint(paint); if (awtPaint.stroke == null) { this.graphics2D.setColor(awtPaint.color); this.graphics2D.setFont(awtPaint.font); this.graphics2D.drawString(text, x, y); } else { setColorAndStroke(awtPaint); TextLayout textLayout = new TextLayout(text, awtPaint.font, this.graphics2D.getFontRenderContext()); AffineTransform affineTransform = new AffineTransform(); affineTransform.translate(x, y); this.graphics2D.draw(textLayout.getOutline(affineTransform)); } }
LineBreakMeasurer measurer = new LineBreakMeasurer(charIterator, BreakIterator.getWordInstance(Locale.ENGLISH), fontContext); int lineCount = 0; while (measurer.getPosition() < charIterator.getEndIndex()) { measurer = new LineBreakMeasurer(charIterator, BreakIterator.getCharacterInstance(Locale.ENGLISH), fontContext); } else { measurer.setPosition(0); while (measurer.getPosition() < charIterator.getEndIndex()) { TextLayout textLayout = measurer.nextLayout(wrapWidth); float ascent = textLayout.getAscent(); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f)); int glowSize = key.glowSize; textLayout.draw(g2d, textX + glowSize / 2 + 1, textY + glowSize / 2 - 1); textLayout.draw(g2d, textX + glowSize / 2 + 1, textY + glowSize / 2 + 1); textLayout.draw(g2d, textX + glowSize / 2 - 1, textY + glowSize / 2 - 1); textLayout.draw(g2d, textX + glowSize / 2 - 1, textY + glowSize / 2 + 1); g2d.setColor(key.color); g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f)); textLayout.draw(g2d, textX + glowSize / 2, textY + glowSize / 2); textY += textLayout.getDescent() + textLayout.getLeading(); // Move down to top of next line.
Graphics2D g2 = (Graphics2D) g; g2.drawImage(image, 1, 1, this); g.setColor(Color.BLUE); g.drawLine(0, yOffset.getValue(), width.getValue(), yOffset.getValue()); g.drawLine(xOffset.getValue(), 0, xOffset.getValue(), height.getValue()); g.setColor(Color.ORANGE); g.drawRect(0, 0, width.getValue(), height.getValue()); FontRenderContext frc = g2.getFontRenderContext(); GlyphVector glyphVector = musicFont.createGlyphVector(frc, string); yOffset.getValue()); g.setColor(Color.RED); g2.draw(rect); TextLayout layout = new TextLayout(string, musicFont, frc); logger.debug( "getAdvance(): {} getVisibleAdvance(): {}", layout.getAdvance(), layout.getVisibleAdvance());
public void render(final String word, BufferedImage image) { Graphics2D g = image.createGraphics(); RenderingHints hints = new RenderingHints( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); hints.add(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY)); g.setRenderingHints(hints); AttributedString as = new AttributedString(word); as.addAttribute(TextAttribute.FONT, getRandomFont()); FontRenderContext frc = g.getFontRenderContext(); AttributedCharacterIterator aci = as.getIterator(); TextLayout tl = new TextLayout(aci, frc); int xBaseline = (int) Math.round(image.getWidth() * XOFFSET); int yBaseline = image.getHeight() - (int) Math.round(image.getHeight() * YOFFSET); Shape shape = tl.getOutline(AffineTransform.getTranslateInstance(xBaseline, yBaseline)); g.setColor(getRandomColor()); g.setStroke(new BasicStroke(_strokeWidth)); g.draw(shape); }