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 void drawText( String text, int x, int y ) { int height = gc.getFontMetrics().getHeight(); String[] lines = text.split( "\n" ); for ( String line : lines ) { gc.drawString( line, x + xOffset, y + height + yOffset ); y += height; } }
public static void renderTextLocation(Graphics2D graphics, Point txtLoc, String text, Color color) { if (Strings.isNullOrEmpty(text)) { return; } int x = txtLoc.getX(); int y = txtLoc.getY(); graphics.setColor(Color.BLACK); graphics.drawString(text, x + 1, y + 1); graphics.setColor(color); graphics.drawString(text, x, y); }
public BufferedImage createBufferedImage() { final BufferedImage im = new BufferedImage(widthCell * 15, heightCell * 15, BufferedImage.TYPE_INT_RGB); final Graphics2D g2d = im.createGraphics(); g2d.setColor(Color.WHITE); g2d.fillRect(0, 0, im.getWidth(), im.getHeight()); g2d.setColor(Color.BLACK); for (ANode n : board.getNodes()) { final int x = board.getCol(n) * widthCell; final int y = n.getRow() * heightCell; g2d.drawString(n.getCode(), x + 5, y + heightCell / 2 - 5); g2d.drawOval(x, y, widthCell / 2, heightCell / 2); } for (ALink link : board.getLinks()) { final ANode n1 = link.getNode1(); final ANode n2 = link.getNode2(); final int x1 = 10 + board.getCol(n1) * widthCell; final int y1 = 10 + n1.getRow() * heightCell; final int x2 = 10 + board.getCol(n2) * widthCell; final int y2 = 10 + n2.getRow() * heightCell; g2d.drawLine(x1, y1, x2, y2); } return im; }
@Signature public void text(float x, float y, String text, DrawOptions options) { if (options.getFill() != null) { gc.setColor(options.getFill()); gc.setPaint(options.getFill()); } if (options.getFont() != null) { gc.setFont(options.getFont()); } gc.drawString(text, x, y); }
public void draw(String text, BufferedImage canvas, FontFactory fontFactory, ColorFactory colorFactory) { Graphics2D g = (Graphics2D) canvas.getGraphics(); TextString ts = convertToCharacters(text, g, fontFactory, colorFactory); arrangeCharacters(canvas.getWidth(), canvas.getHeight(), ts); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); for (TextCharacter tc : ts.getCharacters()) { g.setColor(tc.getColor()); g.drawString(tc.iterator(), (float) tc.getX(), (float) tc.getY()); } }
g2d.setFont(font); FontMetrics fm = g2d.getFontMetrics(); int width = fm.stringWidth(text); int height = fm.getHeight(); g2d.dispose(); g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); g2d.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); fm = g2d.getFontMetrics(); g2d.setColor(Color.BLACK); g2d.drawString(text, 0, fm.getAscent()); g2d.dispose(); try {
double nodeWidth = fM.stringWidth(nodeStr); double nodeHeight = fM.getHeight(); double nodeAscent = fM.getAscent(); WidthResult wr = widthResult(t, fM); g2.drawString(nodeStr, (float) (nodeTab + start.getX()), (float) (start.getY() + nodeAscent)); if (t.isLeaf()) { return nodeWidth; childStartX += cWidth; if (i < t.children().length - 1) { childStartX += sisterSkip * fM.stringWidth(" ");
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 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(); }
/** * 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(); }
BufferedImage image = new BufferedImage(144, 32, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); g.setFont(new Font("Dialog", Font.PLAIN, 24)); Graphics2D graphics = (Graphics2D) g; graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); graphics.drawString("Hello World!", 6, 24); ImageIO.write(image, "png", new File("text.png")); for (int y = 0; y < 32; y++) { StringBuilder sb = new StringBuilder(); for (int x = 0; x < 144; x++) sb.append(image.getRGB(x, y) == -16777216 ? " " : image.getRGB(x, y) == -1 ? "#" : "*"); if (sb.toString().trim().isEmpty()) continue; System.out.println(sb); }
g.draw(path); g.fillArc(startX, y - CIRCLE_WIDTH / 2, CIRCLE_WIDTH, CIRCLE_WIDTH, 0, 360); g.drawString(label, startX + LABEL_OFFSET, y - LABEL_OFFSET); } else { int startX = x1; path.lineTo(endX + ARROW_WIDTH, y + ARROW_WIDTH); g.draw(path); int labelWidth = g.getFontMetrics().stringWidth(label); g.fillArc(startX - CIRCLE_WIDTH / 2, y - CIRCLE_WIDTH / 2, CIRCLE_WIDTH, CIRCLE_WIDTH, 0, 360); g.drawString(label, startX - LABEL_OFFSET - labelWidth, y - LABEL_OFFSET);
protected void paintComponent (Graphics graphics) { super.paintComponent(graphics); Graphics2D g = (Graphics2D)graphics; int width = getWidth(); int height = getHeight(); g.setColor(bgColor); g.fillRect(border, border, width - border * 2, height - border * 2); int maxKnobX = width - border - KNOB_WIDTH; int knobX = (int)((width - border * 2 - KNOB_WIDTH) * (value - sliderMin) / (sliderMax - sliderMin)) + border; g.setColor(knobColor); g.fillRect(Math.max(border, Math.min(maxKnobX, knobX)), 0, KNOB_WIDTH, height); float displayValue = (int)(value * 10) / 10f; String label = displayValue == (int)displayValue ? String.valueOf((int)displayValue) : String.valueOf(displayValue); FontMetrics metrics = g.getFontMetrics(); int labelWidth = metrics.stringWidth(label); g.setColor(Color.white); g.drawString(label, width / 2 - labelWidth / 2, height / 2 + metrics.getAscent() / 2); }
if (positionCursor != null) { int x = positionCursor.getX(this); g.setColor(positionCursor.getColor()); g.drawLine(x, positionCursor.getYMin(this), x, positionCursor.getYMax(this)); if (rangeCursor != null) { int x = rangeCursor.getX(this); g.setColor(rangeCursor.getColor()); g.drawLine(x, rangeCursor.getYMin(this), x, rangeCursor.getYMax(this)); AlphaComposite ac = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f); g.setComposite(ac); g.fillRect(positionCursor.getX(this), positionCursor.getYMin(this), rangeCursor.getX(this) - positionCursor.getX(this), rangeCursor.getYMax(this) - positionCursor.getYMin(this)); g.setColor(valueLabel.getColor()); g.drawString(valueLabel.getText(), valueLabel.getX(this), valueLabel.getY(this));
@Override public Image createImage(String code) { final BufferedImage image = new BufferedImage(this.width, this.height, BufferedImage.TYPE_INT_RGB); final Graphics2D g = ImageUtil.createGraphics(image, ObjectUtil.defaultIfNull(this.background, Color.WHITE)); // 画字符串 g.setFont(font); final int len = this.generator.getLength(); int charWidth = width / (len + 2); for (int i = 0; i < len; i++) { // 产生随机的颜色值,让输出的每个字符的颜色值都将不同。 g.setColor(ImageUtil.randomColor()); g.drawString(String.valueOf(code.charAt(i)), (i + 1) * charWidth, height - 4); } // g.drawString(code, 1, height - 4); shear(g, width, height, Color.white); drawThickLine(g, 0, RandomUtil.randomInt(height) + 1, width, RandomUtil.randomInt(height) + 1, this.interfereCount, ImageUtil.randomColor()); return image; }
public static void drawShadowText(Graphics2D g2, String s, int x, int y, Color shadowColor, int offset) { g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); Color c = g2.getColor(); g2.setColor(shadowColor); g2.drawString(s, x, y + offset); g2.setColor(c); g2.drawString(s, x, y); }
super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); String yLabel = ((int) ((getMinScore() + (getMaxScore() - getMinScore()) * ((i * 1.0) / numberYDivisions)) * 100)) / 100.0 + ""; FontMetrics metrics = g2.getFontMetrics(); int labelWidth = metrics.stringWidth(yLabel); g2.drawString(yLabel, x0 - labelWidth - 5, y0 + (metrics.getHeight() / 2) - 3); String xLabel = i + ""; FontMetrics metrics = g2.getFontMetrics(); int labelWidth = metrics.stringWidth(xLabel); g2.drawString(xLabel, x0 - labelWidth / 2, y0 + metrics.getHeight() + 3);
String s = "Hello, world!"; FontMetrics fm = g2d.getFontMetrics(); int x = img.getWidth() - fm.stringWidth(s) - 5; int y = fm.getHeight(); g2d.drawString(s, x, y); g2d.dispose(); return img;
private void renderInventory(Graphics2D graphics) { Widget inventoryWidget = client.getWidget(WidgetInfo.INVENTORY); if (inventoryWidget == null || inventoryWidget.isHidden()) { return; } for (WidgetItem item : inventoryWidget.getWidgetItems()) { Rectangle slotBounds = item.getCanvasBounds(); String idText = "" + item.getId(); FontMetrics fm = graphics.getFontMetrics(); Rectangle2D textBounds = fm.getStringBounds(idText, graphics); int textX = (int) (slotBounds.getX() + (slotBounds.getWidth() / 2) - (textBounds.getWidth() / 2)); int textY = (int) (slotBounds.getY() + (slotBounds.getHeight() / 2) + (textBounds.getHeight() / 2)); graphics.setColor(new Color(255, 255, 255, 65)); graphics.fill(slotBounds); graphics.setColor(Color.BLACK); graphics.drawString(idText, textX + 1, textY + 1); graphics.setColor(YELLOW); graphics.drawString(idText, textX, textY); } }