Java Code Examples for java.awt.Canvas#getFontMetrics()

The following examples show how to use java.awt.Canvas#getFontMetrics() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WideComboBox.java    From openAGV with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance with the given content and adapts the
 * popupWitdth to the longest item in items using it's string value.
 *
 * @param items The values in the new combo box.
 */
public WideComboBox(final String[] items) {
  super(items);
  int tmpPopupWidth = 1;
  Canvas c = new Canvas();
  FontMetrics fontMetrics = c.getFontMetrics(this.getFont());
  for (String item : items) {
    tmpPopupWidth = Integer.max(fontMetrics.stringWidth(item), tmpPopupWidth);
  }
  tmpPopupWidth += 20;
  this.popupWidth = Optional.of(tmpPopupWidth);
}
 
Example 2
Source File: WideComboBox.java    From openAGV with Apache License 2.0 5 votes vote down vote up
@Override
public void doLayout() {
  try {
    layingOut = true;
    int tmpPopupWidth = 1;
    Canvas c = new Canvas();
    FontMetrics fontMetrics = c.getFontMetrics(this.getFont());
    for (int i = 0; i < super.getItemCount(); i++) {
      String item = super.getItemAt(i);
      tmpPopupWidth = Integer.max(fontMetrics.stringWidth(item), tmpPopupWidth);
    }
    tmpPopupWidth += 15;
    this.popupWidth = Optional.of(tmpPopupWidth);
    super.doLayout();
  }
  finally {
    layingOut = false;
  }
}
 
Example 3
Source File: AbstractWordXmlWriter.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * function emulate the overflow hidden behavior on table cell
 * 
 * @param text
 *            String to check
 * @param style
 *            style of the text
 * @param fontFamily
 *            fond of the text
 * @param cellWidth
 *            the width of the container in points
 * @return String with truncated words that surpasses the cell width
 */
public String cropOverflowString( String text, IStyle style,
		String fontFamily, int cellWidth )
{// TODO: retrieve font type and replace plain with corresponding
	Font font = new Font( fontFamily, Font.PLAIN,
			WordUtil.parseFontSize( PropertyUtil.getDimensionValue( style
					.getProperty( StyleConstants.STYLE_FONT_SIZE ) ) ) );
	Canvas c = new Canvas( );
	FontMetrics fm = c.getFontMetrics( font );
	// conversion from point to advancement point from sample linear
	// regression:
	int cellWidthInPointAdv = ( cellWidth * (int) WordUtil.PT_TWIPS - 27 )
			/ 11;
	StringBuilder sb = new StringBuilder( text.length( ) + 1 );
	int wordEnd = INDEX_NOTFOUND;
	do
	{
		wordEnd = text.indexOf( SPACE );
		if ( wordEnd != INDEX_NOTFOUND ) // space found
		{
			String word = text.substring( 0, wordEnd );
			word = cropOverflowWord( word, fm, cellWidthInPointAdv );
			sb.append( word );
			sb.append( SPACE );
			text = text.substring( wordEnd + 1 );
		}
	} while ( wordEnd != INDEX_NOTFOUND && !EMPTY_STRING.equals( text ) );
	sb.append( cropOverflowWord( text, fm, cellWidthInPointAdv ) );
	return sb.toString( );
}
 
Example 4
Source File: StatusImage.java    From jenkins-status-badges-plugin with MIT License 5 votes vote down vote up
public int measureText( String text )
    throws FontFormatException, IOException
{
    URL fontURL =
        new URL( Jenkins.getInstance().pluginManager.getPlugin( "status-badges" ).baseResourceURL,
                 "fonts/verdana.ttf" );
    InputStream fontStream = fontURL.openStream();
    Font defaultFont = Font.createFont( Font.TRUETYPE_FONT, fontStream );
    defaultFont = defaultFont.deriveFont( 11f );
    Canvas canvas = new Canvas();
    FontMetrics fontMetrics = canvas.getFontMetrics( defaultFont );
    return fontMetrics.stringWidth( text );
}
 
Example 5
Source File: HTNDomainVisualizer.java    From microrts with GNU General Public License v3.0 4 votes vote down vote up
public HTNDomainVisualizer() {
    font = new Font("Arial", Font.PLAIN, 10);
    Canvas c = new Canvas();
    fm = c.getFontMetrics(font);
}
 
Example 6
Source File: HTNDomainVisualizerVertical.java    From microrts with GNU General Public License v3.0 4 votes vote down vote up
public HTNDomainVisualizerVertical() {
    font = new Font("Arial", Font.PLAIN, 16);
    Canvas c = new Canvas();
    fm = c.getFontMetrics(font);
}