Java Code Examples for javax.swing.SwingUtilities#computeStringWidth()

The following examples show how to use javax.swing.SwingUtilities#computeStringWidth() . These examples are extracted from open source projects. 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
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 2
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 3
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 4
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 5
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 6
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 7
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 8
Source Project: hottub   File: KerningLeak.java    License: GNU General Public License v2.0 6 votes vote down vote up
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 9
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 10
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 11
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 12
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 13
private static void leak() {
    Map<TextAttribute, Object> textAttributes = new HashMap<>();
    textAttributes.put(TextAttribute.FAMILY, "Sans Serif");
    textAttributes.put(TextAttribute.SIZE, 12);
    textAttributes.put(TextAttribute.KERNING, TextAttribute.KERNING_ON);
    Font font = Font.getFont(textAttributes);
    JLabel label = new JLabel();
    int dummy = 0;
    for (int i = 0; i < 500; i++) {
        if (i % 10 == 0) System.out.println("Starting iter " + (i+1));
        for (int j = 0; j <1000; j++) {
            FontMetrics fm = label.getFontMetrics(font);
            dummy += SwingUtilities.computeStringWidth(fm, Integer.toString(j));
        }
    }
    System.out.println("done " + dummy);
}
 
Example 14
/**
 * Draws an item label.
 * 
 * @param g2 the graphics device.
 * @param orientation the orientation.
 * @param dataset the dataset.
 * @param series the series index (zero-based).
 * @param item the item index (zero-based).
 * @param x the x coordinate (in Java2D space).
 * @param y the y coordinate (in Java2D space).
 * @param negative indicates a negative value (which affects the item label position).
 */
protected void drawItemLabel(Graphics2D g2, PlotOrientation orientation, XYDataset dataset,
    int series, int item, double x, double y, boolean negative) {

  XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
  Font labelFont = getItemLabelFont(series, item);
  g2.setFont(labelFont);
  String label = generator.generateLabel(dataset, series, item);

  if ((label == null) || (label.length() == 0))
    return;

  // get the label position..
  ItemLabelPosition position = null;
  if (!negative) {
    position = getPositiveItemLabelPosition(series, item);
  } else {
    position = getNegativeItemLabelPosition(series, item);
  }

  // work out the label anchor point...
  Point2D anchorPoint =
      calculateLabelAnchorPoint(position.getItemLabelAnchor(), x, y, orientation);

  FontMetrics metrics = g2.getFontMetrics(labelFont);
  int width = SwingUtilities.computeStringWidth(metrics, label) + 2;
  int height = metrics.getHeight();

  int X = (int) (anchorPoint.getX() - (width / 2));
  int Y = (int) (anchorPoint.getY() - (height));

  g2.setPaint(searchColor);
  g2.fillRect(X, Y, width, height);

  super.drawItemLabel(g2, orientation, dataset, series, item, x, y, negative);

}
 
Example 15
/**
 * Draws an item label.
 * 
 * @param g2 the graphics device.
 * @param orientation the orientation.
 * @param dataset the dataset.
 * @param series the series index (zero-based).
 * @param item the item index (zero-based).
 * @param x the x coordinate (in Java2D space).
 * @param y the y coordinate (in Java2D space).
 * @param negative indicates a negative value (which affects the item label position).
 */
protected void drawItemLabel(Graphics2D g2, PlotOrientation orientation, XYDataset dataset,
    int series, int item, double x, double y, boolean negative) {

  XYItemLabelGenerator generator = getItemLabelGenerator(series, item);
  Font labelFont = getItemLabelFont(series, item);
  g2.setFont(labelFont);
  String label = generator.generateLabel(dataset, series, item);

  if ((label == null) || (label.length() == 0))
    return;

  // get the label position..
  ItemLabelPosition position = null;
  if (!negative) {
    position = getPositiveItemLabelPosition(series, item);
  } else {
    position = getNegativeItemLabelPosition(series, item);
  }

  // work out the label anchor point...
  Point2D anchorPoint =
      calculateLabelAnchorPoint(position.getItemLabelAnchor(), x, y, orientation);

  FontMetrics metrics = g2.getFontMetrics(labelFont);
  int width = SwingUtilities.computeStringWidth(metrics, label) + 2;
  int height = metrics.getHeight();

  int X = (int) (anchorPoint.getX() - (width / 2));
  int Y = (int) (anchorPoint.getY() - (height));

  g2.setPaint(searchColor);
  g2.fillRect(X, Y, width, height);

  super.drawItemLabel(g2, orientation, dataset, series, item, x, y, negative);

}
 
Example 16
Source Project: chipster   File: ImportPreviewTable.java    License: MIT License 5 votes vote down vote up
private void updateRowNumberColumnWidth() {	
	if(this.getRowCount()>0 && this.getColumnCount()>0){
		String str = this.getValueAt(this.getRowCount()-1, 0).toString();
		FontMetrics fm = this.getFontMetrics(this.getFont());

		// The number does not fit without a bit extra space (+8)
		int width = SwingUtilities.computeStringWidth(fm, str) + 8;

		this.getColumnModel().getColumn(0).setMaxWidth(width);
		this.getColumnModel().getColumn(0).setMinWidth(width);
		logger.debug("Set row number column width to " + width);
	}
}
 
Example 17
Source Project: FlatLaf   File: FlatMenuItemRenderer.java    License: Apache License 2.0 4 votes vote down vote up
protected Dimension getPreferredMenuItemSize() {
	int width = 0;
	int height = 0;
	boolean isTopLevelMenu = isTopLevelMenu( menuItem );

	Rectangle viewRect = new Rectangle( 0, 0, Integer.MAX_VALUE, Integer.MAX_VALUE );
	Rectangle iconRect = new Rectangle();
	Rectangle textRect = new Rectangle();

	// layout icon and text
	SwingUtilities.layoutCompoundLabel( menuItem,
		menuItem.getFontMetrics( menuItem.getFont() ), menuItem.getText(), getIconForLayout(),
		menuItem.getVerticalAlignment(), menuItem.getHorizontalAlignment(),
		menuItem.getVerticalTextPosition(), menuItem.getHorizontalTextPosition(),
		viewRect, iconRect, textRect, scale( menuItem.getIconTextGap() ) );

	// union icon and text rectangles
	Rectangle labelRect = iconRect.union( textRect );
	width += labelRect.width;
	height = Math.max( labelRect.height, height );

	// accelerator size
	String accelText = getAcceleratorText();
	if( accelText != null ) {
		// gap between text and accelerator
		width += scale( !isTopLevelMenu ? textAcceleratorGap : menuItem.getIconTextGap() );

		FontMetrics accelFm = menuItem.getFontMetrics( acceleratorFont );
		width += SwingUtilities.computeStringWidth( accelFm, accelText );
		height = Math.max( accelFm.getHeight(), height );
	}

	// arrow size
	if( !isTopLevelMenu && arrowIcon != null ) {
		// gap between text and arrow
		if( accelText == null )
			width += scale( textNoAcceleratorGap );

		// gap between accelerator and arrow
		width += scale( acceleratorArrowGap );

		width += arrowIcon.getIconWidth();
		height = Math.max( arrowIcon.getIconHeight(), height );
	}

	// add insets
	Insets insets = menuItem.getInsets();
	width += insets.left + insets.right;
	height += insets.top + insets.bottom;

	// minimum width
	if( !isTopLevelMenu ) {
		int minimumWidth = FlatUIUtils.minimumWidth( menuItem, this.minimumWidth );
		width = Math.max( width, scale( minimumWidth ) );
	}

	return new Dimension( width, height );
}
 
Example 18
Source Project: FlatLaf   File: FlatMenuItemRenderer.java    License: Apache License 2.0 4 votes vote down vote up
private void layout( Rectangle viewRect, Rectangle iconRect, Rectangle textRect,
	Rectangle accelRect, Rectangle arrowRect, Rectangle labelRect )
{
	boolean isTopLevelMenu = isTopLevelMenu( menuItem );

	// layout arrow
	if( !isTopLevelMenu && arrowIcon != null ) {
		arrowRect.width = arrowIcon.getIconWidth();
		arrowRect.height = arrowIcon.getIconHeight();
	} else
		arrowRect.setSize( 0, 0 );
	arrowRect.y = viewRect.y + centerOffset( viewRect.height, arrowRect.height );

	// layout accelerator
	String accelText = getAcceleratorText();
	if( accelText != null ) {
		FontMetrics accelFm = menuItem.getFontMetrics( acceleratorFont );
		accelRect.width = SwingUtilities.computeStringWidth( accelFm, accelText );
		accelRect.height = accelFm.getHeight();

		accelRect.y = viewRect.y + centerOffset( viewRect.height, accelRect.height );
	} else
		accelRect.setBounds( 0, 0, 0, 0 );

	// compute horizontal positions of accelerator and arrow
	int accelArrowGap = !isTopLevelMenu ? scale( acceleratorArrowGap ) : 0;
	if( menuItem.getComponentOrientation().isLeftToRight() ) {
		// left-to-right
		arrowRect.x = viewRect.x + viewRect.width - arrowRect.width;
		accelRect.x = arrowRect.x - accelArrowGap - accelRect.width;
	} else {
		// right-to-left
		arrowRect.x = viewRect.x;
		accelRect.x = arrowRect.x + accelArrowGap + arrowRect.width;
	}

	// width of accelerator, arrow and gap
	int accelArrowWidth = accelRect.width + arrowRect.width;
	if( accelText != null )
		accelArrowWidth += scale( !isTopLevelMenu ? textAcceleratorGap : menuItem.getIconTextGap() );
	if( !isTopLevelMenu && arrowIcon != null ) {
		if( accelText == null )
			accelArrowWidth += scale( textNoAcceleratorGap );
		accelArrowWidth += scale( acceleratorArrowGap );
	}

	// label rectangle is view rectangle subtracted by accelerator, arrow and gap
	labelRect.setBounds( viewRect );
	labelRect.width -= accelArrowWidth;
	if( !menuItem.getComponentOrientation().isLeftToRight() )
		labelRect.x += accelArrowWidth;

	// layout icon and text
	SwingUtilities.layoutCompoundLabel( menuItem,
		menuItem.getFontMetrics( menuItem.getFont() ), menuItem.getText(), getIconForLayout(),
		menuItem.getVerticalAlignment(), menuItem.getHorizontalAlignment(),
		menuItem.getVerticalTextPosition(), menuItem.getHorizontalTextPosition(),
		labelRect, iconRect, textRect, scale( menuItem.getIconTextGap() ) );
}
 
Example 19
Source Project: chipster   File: Text.java    License: MIT License 4 votes vote down vote up
/**
  * 
  * @param g 
  * @param width 
  * @param height 
  */
 public void draw(Graphics g, int width, int height, PaintMode notUsed) {    
     
     g.setColor(color);
     deviceCoords[0][0] = (int)((projectedCoords[0][0] + 0.5) * width);
     deviceCoords[0][1] = (int)((projectedCoords[0][1] + 0.5) * height);
     
     if (this.text != null) {
     	
     	//Calculate text dimensions
     	int textWidth = 0;
     	int textHeight = 10;
		
			textWidth += SwingUtilities.computeStringWidth(g.getFontMetrics(), text);
     	
     	int textCenterX = deviceCoords[0][0];
     	int textCenterY = deviceCoords[0][1];
     	
     	int x;
     	int y;
     	
     	if (textCenterX < width / 2) {
     		x = textCenterX - textWidth;        		
     	} else {
     		x = textCenterX;
     	}
     	        	
     	if (textCenterY < height / 2) {        	
     		y = textCenterY;        
     	} else {
     		y = textCenterY + textHeight;
     	}

g.setColor(textColor);

Font origFont = g.getFont();
if (emphasis) {
	g.setFont(g.getFont().deriveFont(Font.BOLD, 16));
}
g.drawString(text, x, y);
g.setFont(origFont);        			        	
     }
 }