List of usage examples for java.awt FontMetrics stringWidth
public int stringWidth(String str)
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * @param fm/*from w ww . j a va 2 s . c o m*/ * @param text * @param availableWidth * @return */ public static String clipString(FontMetrics fm, String text, int availableWidth) { // first see if the string needs clipping at all if (text == null) { return ""; } if (fm.stringWidth(text) < availableWidth) { return text; } String dots = "..."; StringBuilder sb = new StringBuilder(text + dots); while (sb.length() - 4 > 0 && fm.stringWidth(sb.toString()) > availableWidth) { sb.deleteCharAt(sb.length() - 4); } return sb.toString(); }
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 ww . j av a 2 s . c o m*/ 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; }
From source file:com.limegroup.gnutella.gui.GUIUtils.java
/** * Gets the width of a given label./*from ww w . j a v a 2 s .com*/ */ public static int width(JLabel c) { FontMetrics fm = c.getFontMetrics(c.getFont()); return fm.stringWidth(c.getText()) + 3; }
From source file:com.limegroup.gnutella.gui.GUIUtils.java
/** * Returns a label with multiple lines that is sized according to * the string parameter./*ww w. ja va 2 s.c o m*/ * * @param msg the string that will be contained in the label. * * @return a MultiLineLabel sized according to the passed * in string. */ public static MultiLineLabel getSizedLabel(String msg) { Dimension dim = new Dimension(); MultiLineLabel label = new MultiLineLabel(msg); FontMetrics fm = label.getFontMetrics(label.getFont()); int width = fm.stringWidth(msg); dim.setSize(Integer.MAX_VALUE, width / 9); //what's this magic? label.setPreferredSize(dim); return label; }
From source file:util.ui.UiUtilities.java
/** * Gets the width of the specified String. * * @param str/* w w w.j a va 2 s .c om*/ * The String to get the width for. * @param font * The font being the base of the measure. * @return the width of the specified String. */ public static int getStringWidth(Font font, String str) { if (str == null) { return 0; } FontMetrics metrics = HELPER_LABEL.getFontMetrics(font); return metrics.stringWidth(str); }
From source file:CenterTextRectangle.java
public void drawCenteredString(String s, int w, int h, Graphics g) { FontMetrics fm = g.getFontMetrics(); int x = (w - fm.stringWidth(s)) / 2; int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2); g.drawString(s, x, y);/* ww w . j av a 2 s . c o m*/ }
From source file:BasicDraw.java
public void paint(Graphics g) { Graphics2D g2d = (Graphics2D) g; FontMetrics fontMetrics = g2d.getFontMetrics(); int width = fontMetrics.stringWidth("aString"); int height = fontMetrics.getHeight(); System.out.println(width);/*from ww w .java2 s .c o m*/ System.out.println(height); }
From source file:Main.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); String text = getWidth() + "x" + getHeight(); FontMetrics fm = g2d.getFontMetrics(); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g2d.drawString(text, x, y);/*from w w w . j ava2 s .c o m*/ g2d.dispose(); }
From source file:Main.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); String text = "I don't see the problem"; FontMetrics fm = g2d.getFontMetrics(); int x = (getWidth() - fm.stringWidth(text)) / 2; int y = ((getHeight() - fm.getHeight()) / 2) + fm.getDescent(); g2d.setTransform(AffineTransform.getRotateInstance(Math.toRadians(45), getWidth() / 2, getHeight() / 2)); g2d.drawString(text, x, y);/* ww w . j a v a 2 s .c o m*/ g2d.dispose(); }
From source file:Main.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.RED);// w w w . j ava 2 s .c om g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight()); g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); Font font = new Font("Arial", Font.BOLD, 48); g2d.setFont(font); FontMetrics fm = g2d.getFontMetrics(); int x = ((getWidth() - fm.stringWidth(text)) / 2); int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent(); g2d.setColor(Color.BLACK); g2d.drawString(text, x, y); g2d.dispose(); }