private void drawTextInImg(BufferedImage baseImage, String textToWrite, int x, int y) { Graphics2D g2D = (Graphics2D) baseImage.getGraphics(); g2D.setColor(new Color(167, 136, 69)); //TODO 注意,这里的字体必须安装在服务器上 g2D.setFont(new Font("Microsoft YaHei", Font.PLAIN, 28)); g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2D.drawString(textToWrite, x, y); g2D.dispose(); }
public int getAscent(Font font){ fakeGraphics.setFont(font); FontMetrics metrics = fakeGraphics.getFontMetrics(); if(DEBUG) System.out.println("Ascent: "+metrics.getAscent()); return metrics.getAscent(); }
private void drawTextInImgCenter(BufferedImage baseImage, String textToWrite, int y) { Graphics2D g2D = (Graphics2D) baseImage.getGraphics(); g2D.setColor(new Color(167, 136, 69)); String fontName = "Microsoft YaHei"; Font f = new Font(fontName, Font.PLAIN, 28); g2D.setFont(f); g2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); // 计算文字长度,计算居中的x点坐标 FontMetrics fm = g2D.getFontMetrics(f); int textWidth = fm.stringWidth(textToWrite); int widthX = (baseImage.getWidth() - textWidth) / 2; // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。 g2D.drawString(textToWrite, widthX, y); // 释放对象 g2D.dispose(); }
fontColor = _defaultColor; _graphics.setColor(fontColor); fontTransform.rotate(-ta.getOrientationAngle() * Math.PI / 180); font = font.deriveFont(fontTransform); _graphics.setFont(font); Rectangle2D bounds = _graphics.getFontMetrics().getStringBounds(str, _graphics); 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(); _graphics.drawString(str, (float) x, (float) y); _graphics.setColor(currentColor);
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); g.setColor(styler.getToolTipBackgroundColor()); g.fill(rectangle); g.setColor(styler.getToolTipBorderColor()); g.draw(rectangle); g.setColor(styler.getChartFontColor()); g.setFont(styler.getToolTipFont()); AffineTransform orig = g.getTransform(); AffineTransform at = new AffineTransform();
void drawString(String text, int x, int y, Font font, Paint paint) { gd.setFont(font); gd.setPaint(paint); gd.drawString(text, x, y); }
/** * Write the given text string in the current font, left-aligned at (<em>x</em>, <em>y</em>). * @param x the <em>x</em>-coordinate of the text * @param y the <em>y</em>-coordinate of the text * @param text the text */ public static void textLeft(double x, double y, String text) { if (text == null) throw new IllegalArgumentException(); offscreen.setFont(font); FontMetrics metrics = offscreen.getFontMetrics(); double xs = scaleX(x); double ys = scaleY(y); int hs = metrics.getDescent(); offscreen.drawString(text, (float) xs, (float) (ys + hs)); draw(); }
g2.setFont(getTickLabelFont(tick.getCategory())); g2.setPaint(getTickLabelPaint(tick.getCategory())); 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);
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 visible.ensureVisible(x + dimBack.getWidth(), y + 1.5); g2d.setFont(font.getFont()); g2d.setColor(mapper.getMappedColor(fontConfiguration.getColor())); final TextLayout t = new TextLayout(shape.getText(), font.getFont(), fontRenderContext); g2d.translate(x, y); final HtmlColor extended = fontConfiguration.getExtendedColor(); if (extended != null) { g2d.setColor(mapper.getMappedColor(extended)); final FontMetrics fm = g2d.getFontMetrics(font.getFont()); final int ypos = (int) (y - fm.getDescent() - 0.5); final HtmlColor extended = fontConfiguration.getExtendedColor();
protected void paintBackground(Graphics g) { Color main = FlamingoUtilities.getColor(Color.gray, "Label.disabledForeground").brighter(); Graphics2D g2d = (Graphics2D) g.create(); g2d.setPaint(new GradientPaint(0, 0, FlamingoUtilities.getLighterColor( main, 0.9), 0, this.richTooltipPanel.getHeight(), FlamingoUtilities.getLighterColor(main, 0.4))); g2d.fillRect(0, 0, this.richTooltipPanel.getWidth(), this.richTooltipPanel.getHeight()); g2d.setFont(FlamingoUtilities.getFont(this.richTooltipPanel, "Ribbon.font", "Button.font", "Panel.font")); g2d.dispose(); }
public static void printTree(Cluster a,String file) { int depth = getDepth(a); width = wunit*(depth+1); height = hunit*(depth+1); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image .createGraphics(); g.setColor(new Color(0,0,0)); g.setStroke(new BasicStroke(1)); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font font = new Font("宋体", Font.BOLD, 20); g.setFont(font); drawTree(a, g, width/2, 0 , 1); //释放对象 g.dispose(); // 保存文件 try { ImageIO.write(image, "png", new File(file)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
public SVGProcessDiagramCanvas(int width, int height, int minX, int minY) { super(width, height, minX, minY); DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation(); // Create an instance of org.w3c.dom.Document. String svgNS = "http://www.w3.org/2000/svg"; Document document = domImpl.createDocument(svgNS, "svg", null); this.g = new SVGGraphics2D( document ); ((SVGGraphics2D) this.g).setSVGCanvasSize( new Dimension( canvasWidth, canvasHeight)); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setPaint(Color.black); Font font = new Font("Arial", Font.BOLD, FONT_SIZE); g.setFont(font); }
public static byte[] createFor(String price) throws IOException { byte[] badgeBackground = Resources.toByteArray(Resources.getResource("assets/badge.png")); BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(badgeBackground)); Graphics2D graphics = bufferedImage.createGraphics(); graphics.setFont(new Font("OpenSans", Font.PLAIN, 34)); graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); graphics.drawString(price + " USD", 86, 45); graphics.dispose(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(bufferedImage, "png", baos); return baos.toByteArray(); }
private void setupFontHeight(Graphics2D graphics2D, int textContainerWidth, int maxHeightOfText) { float fontSize = requestedFont.getSize(); FontRenderContext fontRenderContext = graphics2D.getFontRenderContext(); Rectangle2D bounds = requestedFont.getStringBounds(maxSingleLineStringWidth(text), fontRenderContext); while (bounds.getWidth() > textContainerWidth || bounds.getHeight() > maxHeightOfText) { requestedFont = requestedFont.deriveFont((fontSize -= 2)); bounds = requestedFont.getStringBounds(maxSingleLineStringWidth(text), fontRenderContext); } graphics2D.setFont(requestedFont); }
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; }
private void shadowWrite(BufferedImage image, String... strings) { Graphics2D g2d = image.createGraphics(); g2d.setFont(new Font("Sans", Font.PLAIN, 25)); FontMetrics fm = g2d.getFontMetrics(); int dy = fm.getHeight(); int xsize = 0; for (String s : strings) { int w = fm.stringWidth(s); if (w > xsize) xsize = w; } int y = 5; int x = 5; //g2d.fillRect(x, y, xsize, dy * strings.length + fm.getDescent()); y += dy; for (String s : strings) { g2d.setPaint(Color.black); g2d.drawString(s, x+1, y+1); g2d.setPaint(Color.white); g2d.drawString(s, x, y); y += dy; } g2d.dispose(); }
public static void drawLabel( Point2D_F64 locationPixel , String label, Graphics2D g2) { // Draw the ID number approximately in the center FontMetrics metrics = g2.getFontMetrics(font); Rectangle2D r = metrics.getStringBounds(label,null); g2.setColor(Color.ORANGE); g2.setFont(font); g2.drawString(label,(float)(locationPixel.x-r.getWidth()/2),(float)(locationPixel.y+r.getHeight()/2)); }
pieBounds.getX() + pieBounds.getWidth() / 2 - annotationRectangle.getWidth() / 2; double yCenter = pieBounds.getY() + pieBounds.getHeight() / 2 + annotationRectangle.getHeight() / 2; double angle = (arcAngle + startAngle) - arcAngle / 2; double xOffset = xCenter + Math.cos(Math.toRadians(angle)) * (pieBounds.getWidth() / 2 * pieStyler.getAnnotationDistance()); double yOffset = yCenter - Math.sin(Math.toRadians(angle)) * (pieBounds.getHeight() / 2 * pieStyler.getAnnotationDistance()); g.setColor(pieStyler.getChartFontColor()); g.setFont(pieStyler.getAnnotationsFont()); AffineTransform orig = g.getTransform(); AffineTransform at = new AffineTransform();