List of usage examples for java.awt Graphics getFontMetrics
public abstract FontMetrics getFontMetrics(Font f);
From source file:Main.java
public static int getStringWidth(Graphics g, Font font, String text) { FontMetrics fm = g.getFontMetrics(font); return (int) fm.getStringBounds(text, g).getWidth(); }
From source file:Main.java
public static void drawCenteredString(Graphics g, String str, int x, int y) { FontMetrics metrics = g.getFontMetrics(g.getFont()); Rectangle2D rect = metrics.getStringBounds(str, g); int w = (int) (rect.getWidth()); int h = (int) (rect.getHeight()); g.drawString(str, x - w / 2, y + h / 2); }
From source file:Main.java
/** * Draw a string centered within a rectangle. The string is drawn using the graphics context's * current font and color.//from ww w .j av a2 s.c om * * @param g the graphics context. * @param str the string. * @param x the bounding x position. * @param y the bounding y position. * @param width the bounding width. * @param height the bounding height. */ public static void drawStringCentered(Graphics g, String str, int x, int y, int width, int height) { FontMetrics fm = g.getFontMetrics(g.getFont()); int xpos = x + ((width - fm.stringWidth(str)) / 2); int ypos = y + ((height + fm.getAscent()) / 2); g.drawString(str, xpos, ypos); }
From source file:Main.java
/** * Creates (in a rather clumsy way) the font metrics for a given {@link Font}. * /* w w w . j a v a 2 s . com*/ * @param aFont * the font instance to create the font metrics for, cannot be * <code>null</code>. * @return a font metrics, never <code>null</code>. */ private static FontMetrics createFontMetrics(final Font aFont) { BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); Graphics canvas = img.getGraphics(); try { return canvas.getFontMetrics(aFont); } finally { canvas.dispose(); canvas = null; img = null; } }
From source file:Main.java
/** * computes the width of a string drawn on a canvas * * @param g the graphics object where to draw * @param f the font used to draw/* w ww.j a v a 2 s .c o m*/ * @param strValue the string to draw * @return */ public static int getStringWidth(Graphics g, Font f, String strValue) { // gets the metrics of the font FontMetrics fm = g.getFontMetrics(f); // and returns its width return (int) fm.getStringBounds(strValue, g).getWidth(); }
From source file:Main.java
/** * Returns a string abbreviated according to the length of the available space * in a component.//from w ww. ja va 2 s .co m * @param str A string which may need abbreviating. * @param component The component the string will be rendered in. * @return a string abbreviated according to the length of the available space */ public static String abbreviate(String str, JComponent component) { String result = ""; if (component != null) { Graphics g = component.getGraphics(); FontMetrics fm = g.getFontMetrics(component.getFont()); int stringSize = SwingUtilities.computeStringWidth(fm, str); final int border = 48; int availableWidth = component.getWidth() - border; if (stringSize > availableWidth) { final int avCharWidth = fm.charWidth('x'); final int alwaysChop = 5; final int charsToChop = alwaysChop + ((stringSize - availableWidth) / avCharWidth); final int leftPos = (str.length() - charsToChop) / 2; final int maxLength = str.length() - charsToChop; final int left = leftPos > 0 ? leftPos : 0; final int len = maxLength > left ? maxLength : left + 1; result = abbreviate(str, left, len); } else { result = str; } } return result; }
From source file:Main.java
public static FontMetrics getFontMetrics(JComponent c, Graphics g) { Font font = g.getFont();/*from ww w . ja v a2 s . c o m*/ if (c != null) { return c.getFontMetrics(g.getFont()); } return g.getFontMetrics(font); }
From source file:Main.java
public static int getStringHeight(Graphics g, Font font, String text) { if (font == null || g == null || text == null) { return -1; }/*from w ww . ja v a 2 s. c om*/ FontMetrics fm = g.getFontMetrics(font); if (!text.contains(("\\n"))) { return (int) fm.getStringBounds(text, g).getHeight(); } return Arrays.stream(text.split("\\\\n")).mapToInt(row -> (int) fm.getStringBounds(text, g).getHeight()) .reduce(Integer::sum).getAsInt(); }
From source file:Main.java
/** * Returns the FontMetrics for the specified Font. This method is used when * a Graphics is available, typically when painting. If a Graphics is not * available the JComponent method of the same name should be used. * <p/>/*ww w. java 2s .co m*/ * This does not necessarily return the FontMetrics from the Graphics. * * @param c * JComponent requesting FontMetrics, may be null * @param g * Graphics Graphics * @param font * Font to get FontMetrics for */ public static FontMetrics getFontMetrics(final JComponent c, final Graphics g, final Font font) { if (c != null) { return c.getFontMetrics(font); } else { return g.getFontMetrics(font); } }
From source file:ala.soils2sat.DrawingUtils.java
public static Rectangle drawString(Graphics g, Font font, String text, int x, int y, int width, int height, int align, boolean wrap) { g.setFont(font);/*from w w w .ja v a2 s .c om*/ FontMetrics fm = g.getFontMetrics(font); setPreferredAliasingMode(g); Rectangle ret = new Rectangle(0, 0, 0, 0); if (text == null) { return ret; } String[] alines = text.split("\\n"); ArrayList<String> lines = new ArrayList<String>(); for (String s : alines) { if (wrap && fm.stringWidth(s) > width) { // need to split this up into multiple lines... List<String> splitLines = wrapString(s, fm, width); lines.addAll(splitLines); } else { lines.add(s); } } int numlines = lines.size(); while (fm.getHeight() * numlines > height) { numlines--; } if (numlines > 0) { int maxwidth = 0; int minxoffset = y + width; int totalheight = (numlines * fm.getHeight()); int linestart = ((height / 2) - (totalheight / 2)); if (!wrap) { ret.y = y + linestart; } else { ret.y = y; linestart = 0; } for (int idx = 0; idx < numlines; ++idx) { String line = lines.get(idx); int stringWidth = fm.stringWidth(line); // the width of the label depends on the font : // if the width of the label is larger than the item if (stringWidth > 0 && width < stringWidth) { // We have to truncate the label line = clipString(null, fm, line, width); stringWidth = fm.stringWidth(line); } int xoffset = 0; int yoffset = linestart + fm.getHeight() - fm.getDescent(); if (align == TEXT_ALIGN_RIGHT) { xoffset = (width - stringWidth); } else if (align == TEXT_ALIGN_CENTER) { xoffset = (int) Math.round((double) (width - stringWidth) / (double) 2); } if (xoffset < minxoffset) { minxoffset = xoffset; } g.drawString(line, x + xoffset, y + yoffset); if (stringWidth > maxwidth) { maxwidth = stringWidth; } linestart += fm.getHeight(); } ret.width = maxwidth; ret.height = totalheight; ret.x = x + minxoffset; // Debug only... if (DEBUG) { Graphics2D g2d = (Graphics2D) g; g2d.setStroke(new BasicStroke(1)); g.setColor(Color.blue); g.drawRect(ret.x, ret.y, ret.width, ret.height); g.setColor(Color.green); g.drawRect(x, y, width, height); } return ret; } return ret; }