javax.swing.SwingUtilities#computeStringWidth ( )源码实例Demo

下面列出了javax.swing.SwingUtilities#computeStringWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: dragonwell8_jdk   文件: KerningLeak.java
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);
}
 
源代码2 项目: TencentKona-8   文件: KerningLeak.java
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);
}
 
源代码3 项目: jdk8u60   文件: KerningLeak.java
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);
}
 
源代码4 项目: openjdk-jdk8u   文件: KerningLeak.java
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);
}
 
源代码5 项目: openjdk-jdk8u-backup   文件: KerningLeak.java
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);
}
 
源代码6 项目: openjdk-jdk9   文件: KerningLeak.java
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);
}
 
源代码7 项目: jdk8u-jdk   文件: KerningLeak.java
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);
}
 
源代码8 项目: hottub   文件: KerningLeak.java
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);
}
 
源代码9 项目: openjdk-8-source   文件: KerningLeak.java
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);
}
 
源代码10 项目: openjdk-8   文件: KerningLeak.java
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);
}
 
源代码11 项目: jdk8u_jdk   文件: KerningLeak.java
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);
}
 
源代码12 项目: jdk8u-jdk   文件: KerningLeak.java
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);
}
 
源代码13 项目: jdk8u-dev-jdk   文件: KerningLeak.java
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);
}
 
源代码14 项目: mzmine3   文件: ScatterPlotRenderer.java
/**
 * 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);

}
 
源代码15 项目: mzmine2   文件: ScatterPlotRenderer.java
/**
 * 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);

}
 
源代码16 项目: chipster   文件: ImportPreviewTable.java
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);
	}
}
 
源代码17 项目: FlatLaf   文件: FlatMenuItemRenderer.java
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 );
}
 
源代码18 项目: FlatLaf   文件: FlatMenuItemRenderer.java
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() ) );
}
 
源代码19 项目: chipster   文件: Text.java
/**
  * 
  * @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);        			        	
     }
 }