private Rectangle2D getUsed(Graphics g, final String label) { return g.getFontMetrics().getStringBounds(label, g); }
Graphics2D g2d = (Graphics2D) g; FontMetrics fm = g2d.getFontMetrics(); Rectangle2D r = fm.getStringBounds(stringTime, g2d); int x = (this.getWidth() - (int) r.getWidth()) / 2; int y = (this.getHeight() - (int) r.getHeight()) / 2 + fm.getAscent(); g.drawString(stringTime, x, y);
private Rectangle getGlyphBounds (GlyphVector vector, int index, int codePoint) { Rectangle bounds; bounds = vector.getGlyphPixelBounds(index, GlyphPage.renderContext, 0, 0); if (renderType == RenderType.Native) { if (bounds.width == 0 || bounds.height == 0) bounds = new Rectangle(); else bounds = metrics.getStringBounds("" + (char)codePoint, GlyphPage.scratchGraphics).getBounds(); } if (codePoint == ' ') bounds.width = spaceWidth; return bounds; }
private Rectangle getGlyphBounds (GlyphVector vector, int index, int codePoint) { Rectangle bounds; bounds = vector.getGlyphPixelBounds(index, GlyphPage.renderContext, 0, 0); if (renderType == RenderType.Native) { if (bounds.width == 0 || bounds.height == 0) bounds = new Rectangle(); else bounds = metrics.getStringBounds("" + (char)codePoint, GlyphPage.scratchGraphics).getBounds(); } if (codePoint == ' ') bounds.width = spaceWidth; return bounds; }
private void draw (Graphics g, Array<TextureRegion> regions, Color color, boolean drawIndex) { int i=0; for(TextureRegion region : regions){ int x = region.getRegionX(), y = region.getRegionY(), h = region.getRegionHeight(); if(drawIndex){ String indexString = ""+i; Rectangle bounds = g.getFontMetrics().getStringBounds(indexString, g).getBounds(); g.setColor(indexBackgroundColor); g.fillRect(x, y+h-bounds.height, bounds.width, bounds.height); g.setColor(indexColor); g.drawString(indexString, x, y+h); ++i; } g.setColor(color); g.drawRect(x, y, region.getRegionWidth(), h); } } }
public static void forceOpenJdkResourceLoad() { if (isOpenJdkRunning()) { // see https://github.com/plantuml/plantuml/issues/123 Log.info("Forcing resource load on OpenJdk"); final BufferedImage imDummy = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB); final Graphics2D gg = imDummy.createGraphics(); final String text = "Alice"; final Font font = new Font("SansSerif", Font.PLAIN, 12); final FontMetrics fm = gg.getFontMetrics(font); final Rectangle2D rect = fm.getStringBounds(text, gg); } }
private void draw (Graphics g, Array<TextureRegion> regions, Color color, boolean drawIndex) { int i=0; for(TextureRegion region : regions){ int x = region.getRegionX(), y = region.getRegionY(), h = region.getRegionHeight(); if(drawIndex){ String indexString = ""+i; Rectangle bounds = g.getFontMetrics().getStringBounds(indexString, g).getBounds(); g.setColor(indexBackgroundColor); g.fillRect(x, y+h-bounds.height, bounds.width, bounds.height); g.setColor(indexColor); g.drawString(indexString, x, y+h); ++i; } g.setColor(color); g.drawRect(x, y, region.getRegionWidth(), h); } } }
private Dimension2DDouble getJavaDimension(UFont font, String text) { final Font javaFont = font.getFont(); final FontMetrics fm = gg.getFontMetrics(javaFont); final Rectangle2D rect = fm.getStringBounds(text, gg); return new Dimension2DDouble(rect.getWidth(), rect.getHeight()); }
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); }
@Signature public double[] textSize(String string, DrawOptions options) { Rectangle2D bounds = gc.getFontMetrics(options.getFont() == null ? getFont() : options.getFont()) .getStringBounds(string, gc); return new double[] { bounds.getWidth(), bounds.getHeight() }; }
/** * Calculates text position and centers on minimap depending on string length. * * @param client the game client * @param graphics the game graphics * @param localLocation local location of the tile * @param text string for width measurement * @return a {@link Point} on screen corresponding to the given * localLocation. */ public static Point getCanvasTextMiniMapLocation( @Nonnull Client client, @Nonnull Graphics2D graphics, @Nonnull LocalPoint localLocation, @Nonnull String text) { Point p = Perspective.localToMinimap(client, localLocation); if (p == null) { return null; } FontMetrics fm = graphics.getFontMetrics(); Rectangle2D bounds = fm.getStringBounds(text, graphics); int xOffset = p.getX() - (int) (bounds.getWidth() / 2); int yOffset = p.getY() - (int) (bounds.getHeight() / 2) + fm.getAscent(); return new Point(xOffset, yOffset); }
private void renderProgressWidget(Graphics2D graphics) { Widget bar = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_BAR).getChild(0); Rectangle2D bounds = bar.getBounds().getBounds2D(); Widget prgs = client.getWidget(WidgetInfo.PEST_CONTROL_ACTIVITY_PROGRESS).getChild(0); int perc = (int) ((prgs.getBounds().getWidth() / bounds.getWidth()) * 100); Color color = Color.GREEN; if (perc < 25) { color = Color.RED; } String text = String.valueOf(perc) + "%"; FontMetrics fm = graphics.getFontMetrics(); Rectangle2D textBounds = fm.getStringBounds(text, graphics); int x = (int) (bounds.getX() - textBounds.getWidth()); int y = (int) (bounds.getY() + fm.getHeight() - 2); graphics.setColor(Color.BLACK); graphics.drawString(text, x + 1, y + 1); graphics.setColor(color); graphics.drawString(text, x, y); }
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); } }
Rectangle2D bounds = fm.getStringBounds(text, graphics); int xOffset = p.getX() - (int) (bounds.getWidth() / 2);
Rectangle2D rect = fm.getStringBounds(str, g);
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); }
String regionText = String.valueOf(regionId); FontMetrics fm = graphics.getFontMetrics(); Rectangle2D textBounds = fm.getStringBounds(regionText, graphics); int labelWidth = (int) textBounds.getWidth() + 2 * LABEL_PADDING; int labelHeight = (int) textBounds.getHeight() + 2 * LABEL_PADDING;
font = font.deriveFont(fontTransform); _graphics.setFont(font); Rectangle2D bounds = _graphics.getFontMetrics().getStringBounds(str, _graphics);
Rectangle2D bounds = fm.getStringBounds(book.getShortName(), g); height = (int) bounds.getHeight() + book.getIcon().getHeight() + 6; Point textLoc = new Point( Rectangle2D bounds = fm.getStringBounds(str, g); Point textLoc = new Point((int) (screenBookcase.getX() - (bounds.getWidth() / 2)), screenBookcase.getY() - (height / 2)); OverlayUtil.renderTextLocation(g, textLoc, str, Color.WHITE);
// Find the size of string s in font f in the current Graphics context g. FontMetrics fm = g.getFontMetrics(f); java.awt.geom.Rectangle2D rect = fm.getStringBounds(s, g); int textHeight = (int)(rect.getHeight()); int textWidth = (int)(rect.getWidth()); int panelHeight= this.getHeight(); int panelWidth = this.getWidth(); // Center text horizontally and vertically int x = (panelWidth - textWidth) / 2; int y = (panelHeight - textHeight) / 2 + fm.getAscent(); g.drawString(s, x, y); // Draw the string.