List of usage examples for java.awt FontMetrics stringWidth
public int stringWidth(String str)
From source file:Main.java
/** Draw 'str' centered at 'p'. */ public static void drawCenteredText(Graphics g, Point p, String str) { FontMetrics fm = g.getFontMetrics(); LineMetrics lm = fm.getLineMetrics(str, g); // Go to 'p', then add a/2 to get to the baseline. // I ignore the descent because it looks better to center without // regard to descenders. int baseY = p.y + (int) (lm.getAscent() / 2); int baseX = p.x - fm.stringWidth(str) / 2; g.drawString(str, baseX, baseY);//from w w w .j av a 2 s.c o m }
From source file:net.sf.webphotos.tools.Thumbnail.java
private static Image estampar(Image im) { try {//from w w w .j a v a2 s .c o m Image temp = new ImageIcon(im).getImage(); BufferedImage buf = new BufferedImage(temp.getWidth(null), temp.getHeight(null), BufferedImage.TYPE_INT_RGB); Graphics2D g2 = (Graphics2D) buf.getGraphics(); g2.drawImage(temp, 0, 0, null); g2.setBackground(Color.BLUE); Dimension dimensaoFoto = new Dimension(im.getWidth(null), im.getHeight(null)); // aplicar texto if (texto != null) { Util.out.println("aplicando texto " + texto); Font fonte = new Font(txFamilia, txEstilo, txTamanho); g2.setFont(fonte); FontMetrics fm = g2.getFontMetrics(fonte); Dimension dimensaoTexto = new Dimension(fm.stringWidth(texto), fm.getHeight()); Point posTexto = calculaPosicao(dimensaoFoto, dimensaoTexto, txMargem, txPosicao); g2.setPaint(txCorFundo); g2.drawString(texto, (int) posTexto.getX() + 1, (int) posTexto.getY() + 1 + fm.getHeight()); g2.setPaint(txCorFrente); g2.drawString(texto, (int) posTexto.getX(), (int) posTexto.getY() + fm.getHeight()); } // aplicar marca dagua if (marcadagua != null) { Image marca = new ImageIcon(marcadagua).getImage(); int rule = AlphaComposite.SRC_OVER; float alpha = (float) mdTransparencia / 100; Point pos = calculaPosicao(dimensaoFoto, new Dimension(marca.getWidth(null), marca.getHeight(null)), mdMargem, mdPosicao); g2.setComposite(AlphaComposite.getInstance(rule, alpha)); g2.drawImage(marca, (int) pos.getX(), (int) pos.getY(), null); } g2.dispose(); //return (Image) buf; return Toolkit.getDefaultToolkit().createImage(buf.getSource()); } catch (Exception e) { Util.err.println("[Thumbnail.estampar]/ERRO: Inesperado - " + e.getMessage()); e.printStackTrace(Util.err); return im; } }
From source file:pt.webdetails.cgg.scripts.BaseScope.java
public static Object getTextLenCGG(Context cx, Scriptable thisObj, Object[] args, Function funObj) { String text = Context.toString(args[0]); String fontFamily = Context.toString(args[1]); String fontSize = Context.toString(args[2]).trim(); String fontStyle = "normal"; String fontWeight = "normal"; if (args.length > 3) { fontStyle = Context.toString(args[3]); if (args.length > 4) { fontWeight = Context.toString(args[4]); }//from w w w . j ava 2 s . c o m } Font ffont = getFont(fontFamily, fontSize, fontStyle, fontWeight); JLabel label = new JLabel(); FontMetrics fMetric = label.getFontMetrics(ffont); int width = fMetric.stringWidth(text); return Context.toNumber(width); }
From source file:org.foxbpm.engine.impl.diagramview.svg.SVGUtils.java
/** * ??????/*from www.ja va2 s . co m*/ * * @param font * ? * @param text * * @return */ public final static int getTextWidth(Font font, String text) { JLabel label = new JLabel(text); label.setFont(font); FontMetrics metrics = label.getFontMetrics(label.getFont()); return metrics.stringWidth(label.getText()); }
From source file:pcgen.gui2.tools.Utility.java
/** * Trim a string from the left to fit within the specified width. * * @param fm The font the text will be rendered in. * @param str The string to trimmed./*from www . j ava 2 s . c o m*/ * @param maxWidth The maximum width that the string is allowed to be. * @return String The trimmed string. */ public static String shortenString(FontMetrics fm, String str, int maxWidth) { for (int i = str.length(); i > 0; i -= 5) { String shortedString = "..." + str.substring(str.length() - i); int width = fm.stringWidth(shortedString); //System.out.println("testing '"+foo+"' = "+width); if (width < maxWidth) { return shortedString; } } return ""; }
From source file:com.piaoyou.util.ImageUtil.java
public static BufferedImage getProductionImage(String productVersion, String productRealeaseDate) { String str = productVersion + " on " + productRealeaseDate; int IMAGE_WIDTH = 250; int IMAGE_HEIGHT = 30; BufferedImage bufferedImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = bufferedImage.createGraphics(); g.setBackground(Color.blue);//from ww w . j av a 2 s. c om g.setColor(Color.white); g.draw3DRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, false); FontMetrics fontMetrics = g.getFontMetrics(); int strWidth = fontMetrics.stringWidth(str); int strHeight = fontMetrics.getAscent() + fontMetrics.getDescent(); g.drawString(str, (IMAGE_WIDTH - strWidth) / 2, IMAGE_HEIGHT - ((IMAGE_HEIGHT - strHeight) / 2) - fontMetrics.getDescent()); g.dispose(); // free resource return bufferedImage; }
From source file:weka.core.ChartUtils.java
/** * Render a combined histogram and pie chart from summary data * /* w ww . j av a 2s. c o m*/ * @param width the width of the resulting image * @param height the height of the resulting image * @param values the values for the chart * @param freqs the corresponding frequencies * @param additionalArgs optional arguments to the renderer (may be null) * @return a buffered image * @throws Exception if a problem occurs */ public static BufferedImage renderCombinedPieAndHistogramFromSummaryData(int width, int height, List<String> values, List<Double> freqs, List<String> additionalArgs) throws Exception { String plotTitle = "Combined Chart"; String userTitle = getOption(additionalArgs, "-title"); plotTitle = (userTitle != null) ? userTitle : plotTitle; List<String> opts = new ArrayList<String>(); opts.add("-title=distribution"); BufferedImage pie = renderPieChartFromSummaryData(width / 2, height, values, freqs, false, true, opts); opts.clear(); opts.add("-title=histogram"); BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, values, freqs, opts); BufferedImage img = new BufferedImage(width, height + 20, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); g2d.setFont(new Font("SansSerif", Font.BOLD, 12)); g2d.setColor(Color.lightGray); g2d.fillRect(0, 0, width, height + 20); g2d.setColor(Color.black); FontMetrics fm = g2d.getFontMetrics(); int fh = fm.getHeight(); int sw = fm.stringWidth(plotTitle); g2d.drawImage(pie, 0, 20, null); g2d.drawImage(hist, width / 2 + 1, 20, null); g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2); g2d.dispose(); return img; }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * Draws the given <code>String</code>, centered at <code>(x,y)</code>, using * the given graphics context.//from w w w. j a va2 s .c o m * * @param s the string * @param g the graphics context to draw in * @param x the x coordinate for center of the <code>String</code> * @param y the y coordinate for center of the <code>String</code> */ public static void drawCenteredString(String s, Graphics g, int x, int y) { ((Graphics2D) g).addRenderingHints(hints); FontMetrics fm = g.getFontMetrics(); int ht = fm.getAscent() + fm.getDescent(); int width = fm.stringWidth(s); g.drawString(s, x - width / 2, y + (fm.getAscent() - ht / 2)); }
From source file:weka.core.ChartUtils.java
/** * Render a combined histogram and box plot chart from summary data * /*from w w w.j a v a2 s . c om*/ * @param width the width of the resulting image * @param height the height of the resulting image * @param bins the values for the chart * @param freqs the corresponding frequencies * @param summary the summary stats for the box plot * @param outliers an optional list of outliers for the box plot * @param additionalArgs optional arguments to the renderer (may be null) * @return a buffered image * @throws Exception if a problem occurs */ public static BufferedImage renderCombinedBoxPlotAndHistogramFromSummaryData(int width, int height, List<String> bins, List<Double> freqs, List<Double> summary, List<Double> outliers, List<String> additionalArgs) throws Exception { String plotTitle = "Combined Chart"; String userTitle = getOption(additionalArgs, "-title"); plotTitle = (userTitle != null) ? userTitle : plotTitle; List<String> opts = new ArrayList<String>(); opts.add("-title=histogram"); BufferedImage hist = renderHistogramFromSummaryData(width / 2, height, bins, freqs, opts); opts.clear(); opts.add("-title=box plot"); BufferedImage box = null; try { box = renderBoxPlotFromSummaryData(width / 2, height, summary, outliers, opts); } catch (Exception ex) { // if we can't generate the box plot (i.e. probably because // data is 100% missing) then just return the histogram } if (box == null) { width /= 2; } BufferedImage img = new BufferedImage(width, height + 20, BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP); g2d.setFont(new Font("SansSerif", Font.BOLD, 12)); g2d.setColor(Color.lightGray); g2d.fillRect(0, 0, width, height + 20); g2d.setColor(Color.black); FontMetrics fm = g2d.getFontMetrics(); int fh = fm.getHeight(); int sw = fm.stringWidth(plotTitle); if (box != null) { g2d.drawImage(box, 0, 20, null); g2d.drawImage(hist, width / 2 + 1, 20, null); } else { g2d.drawImage(hist, 0, 20, null); } g2d.drawString(plotTitle, width / 2 - sw / 2, fh + 2); g2d.dispose(); return img; }
From source file:ala.soils2sat.DrawingUtils.java
private static List<String> wrapString(String s, FontMetrics fm, int width) { List<String> lines = new ArrayList<String>(); StringBuilder line = new StringBuilder(); for (int i = 0; i < s.length(); ++i) { char ch = s.charAt(i); String test = line.toString() + ch; if (fm.stringWidth(test) > width) { if (test.length() > 1) { // Backtrack to look for a space... boolean breakFound = false; if (ch != ' ') { for (int j = line.length() - 1; j > 0; --j) { if (line.charAt(j) == ' ') { lines.add(line.substring(0, j)); line = new StringBuilder(line.substring(j + 1)); breakFound = true; break; }// www . java2s. com } } if (!breakFound) { lines.add(line.toString()); line = new StringBuilder(); } line.append(ch); } else { lines.add(test); line = new StringBuilder(); } } else { line.append(ch); } } if (line.length() > 0) { lines.add(line.toString()); } return lines; }