Java Code Examples for java.awt.FontMetrics.getStringBounds()

The following are Jave code examples for showing how to use getStringBounds() of the java.awt.FontMetrics class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: incubator-netbeans   File: SummaryCellRenderer.java   View Source Code Vote up 6 votes
@SuppressWarnings("empty-statement")
private int resizePane (String text, JList list, int lastWidth) {
    if (text == null) {
        text = ""; //NOI18N
    }
    int width = summaryView.getMaster().getComponent().getWidth();
    if (width > 0 && width != lastWidth) {
        String[] rows = text.split("\n"); //NOI18N
        FontMetrics fm = list.getFontMetrics(list.getFont());
        int lines = 0;
        for (String row : rows) {
            Rectangle2D rect = fm.getStringBounds(row, revisionCell.getGraphics());
            lines += (int) (rect.getWidth() / (width - 80) + 1);
        }
        int ph = fm.getHeight() * (lines + 1) + 4;
        revisionCell.setPreferredSize(new Dimension(width - 50 - ICON_COLLAPSED.getIconWidth(), ph));
        setPreferredSize(revisionCell.getPreferredSize());
    }
    return width;
}
 
Example 2
Project: geomapapp   File: PoleMapServer.java   View Source Code Vote up 6 votes
private static BufferedImage drawTileName(BufferedImage image, String name) {
	BufferedImage i2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
	Graphics2D g2 = i2.createGraphics();
	g2.drawImage(image, null, 0, 0);
	g2.setColor(Color.red);
	g2.setStroke(new BasicStroke(4));
	g2.drawRect(8, 8, image.getWidth() - 8, image.getHeight() - 8);

	g2.setColor(Color.RED);
	g2.setFont(g2.getFont().deriveFont(Font.BOLD).deriveFont(13f));
	FontMetrics fm = g2.getFontMetrics();
	String[] parts = name.split("/");
	for (int i = 0; i < parts.length; i++) {
		String s = i == parts.length - 1 ? parts[i] : parts[i] + "/"; 
		int x = 40 + i * 5;
		int y = 40 + i * (fm.getHeight() + 5);
		Rectangle2D rect = fm.getStringBounds(s, g2);
		g2.setColor(Color.white);
		g2.fillRect(x, y - fm.getAscent(), (int)rect.getWidth(), fm.getHeight());
		g2.setColor(Color.red);
		g2.drawString(s, x, y);
	}
	return i2;
}
 
Example 3
Project: geomapapp   File: MMapServer.java   View Source Code Vote up 6 votes
private static BufferedImage drawTileName(BufferedImage image, String name) {
	BufferedImage i2 = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
	Graphics2D g2 = i2.createGraphics();
	g2.drawImage(image, null, 0, 0);
	g2.setColor(Color.red);
	g2.setStroke(new BasicStroke(4));
	g2.drawRect(8, 8, image.getWidth() - 8, image.getHeight() - 8);

	g2.setColor(Color.RED);
	g2.setFont(g2.getFont().deriveFont(Font.BOLD).deriveFont(13f));
	FontMetrics fm = g2.getFontMetrics();
	String[] parts = name.split("/");
	for (int i = 0; i < parts.length; i++) {
		String s = i == parts.length - 1 ? parts[i] : parts[i] + "/";
		int x = 40 + i * 5;
		int y = 40 + i * (fm.getHeight() + 5);
		Rectangle2D rect = fm.getStringBounds(s, g2);
		g2.setColor(Color.white);
		g2.fillRect(x, y - fm.getAscent(), (int)rect.getWidth(), fm.getHeight());
		g2.setColor(Color.red);
		g2.drawString(s, x, y);
	}
	return i2;
}
 
Example 4
Project: ScreenCut   File: ScreenCut.java   View Source Code Vote up 5 votes
/**
 * 绘制文字
 */
private void drawString(Point st, String str, Color c, Graphics g, int size) {
    g.setFont(new Font("宋体", Font.PLAIN, size));
    g.setColor(c);
    FontMetrics fm = g.getFontMetrics();
    Rectangle2D r2d = fm.getStringBounds(str, g);
    g.drawString(str, st.x, st.y + (int) r2d.getHeight());
}
 
Example 5
Project: incubator-netbeans   File: LineNumbersActionsBar.java   View Source Code Vote up 5 votes
private boolean checkLinesWidth(Graphics g) {
    FontMetrics fm = g.getFontMetrics(getLinesFont());
    Rectangle2D rect = fm.getStringBounds(Integer.toString(linesCount), g);
    linesWidth = (int) rect.getWidth() + LINES_BORDER_WIDTH * 2;
    if (linesWidth != oldLinesWidth) {
        oldLinesWidth = linesWidth;
        revalidate();
        repaint();
        return true;
    }
    return false;
}
 
Example 6
Project: jmt   File: FastGraph.java   View Source Code Vote up 5 votes
/**
 * Draws the selected value
 * @param selectedValue the selected value to draw
 * @param g the graphics object
 * @param width the total chart width
 * @param height the total chart height
 * @param maxx the maximum X value (simulation time)
 * @param maxy the maximum Y value
 */
private void paintSelectedValue(MeasureValue selectedValue, Graphics g, int width, int height, double maxx, double maxy) {
	FontMetrics metric = g.getFontMetrics();
	String x_str = simulationTimeFormat.format(selectedValue.getSimTime());
	String m_str = formatNumber(selectedValue.getMeanValue());
	String i_str = formatNumber(selectedValue.getLastIntervalAvgValue());
	
	Dimension bounds = composeVerticalBounds(g, metric, POPUP_FULL_X_PREFIX + x_str, POPUP_FULL_X_PREFIX + m_str, POPUP_FULL_X_PREFIX + i_str);
	int selectedValueX = getX(selectedValue.getSimTime());
	int textX = (int)(selectedValueX - bounds.getWidth() / 2);
	// Fix value out of chart for label
	if (textX < 2) {
		textX = 2;
	} else if (textX + bounds.getWidth() + POPUP_MARGIN > width) {
		textX = width - (int)bounds.getWidth() - POPUP_MARGIN;
	}
	int textY = getY(maxy / 2) + (int)bounds.getHeight() / 2;
	
	g.setColor(COLOR_POPUP);
	g.drawLine(selectedValueX, getY(0), selectedValueX, getY(selectedValue.getLastIntervalAvgValue()));
	g.setColor(COLOR_POPUP_BG);
	g.fillRoundRect(textX - POPUP_MARGIN, textY - (int)bounds.getHeight(), (int)bounds.getWidth() + POPUP_MARGIN * 2, (int)bounds.getHeight() + POPUP_MARGIN, 4, 4);	
	g.setColor(COLOR_POPUP);
	g.drawRoundRect(textX - POPUP_MARGIN, textY - (int)bounds.getHeight(), (int)bounds.getWidth() + POPUP_MARGIN * 2, (int)bounds.getHeight() + POPUP_MARGIN, 4, 4);
	
	// Draw squares
	Rectangle2D prefixBounds = metric.getStringBounds(POPUP_X_PREFIX, g);
	g.setColor(COLOR_DRAW);
	g.fillRect(textX, textY - (int)prefixBounds.getHeight() - bounds.height / 3, (int)prefixBounds.getWidth(), (int)prefixBounds.getHeight());
	g.setColor(COLOR_LAST_INTERVAL);
	g.fillRect(textX, textY - (int)prefixBounds.getHeight(), (int)prefixBounds.getWidth(), (int)prefixBounds.getHeight());
	
	// Draws texts
	g.setColor(COLOR_AXIS);
	g.drawString(POPUP_FULL_X_PREFIX + x_str, textX, textY - bounds.height * 2 / 3);
	g.drawString(POPUP_MIDDLE + m_str, textX + (int)prefixBounds.getWidth(), textY - bounds.height / 3);
	g.drawString(POPUP_MIDDLE + i_str, textX + (int)prefixBounds.getWidth(), textY);
}
 
Example 7
Project: jmt   File: FastGraph.java   View Source Code Vote up 5 votes
private Dimension composeVerticalBounds(Graphics g, FontMetrics metric, String...strings) {
	double xBounds = 0.0, yBounds = 0.0;
	for (int i=0; i<strings.length; i++) {
		Rectangle2D bounds = metric.getStringBounds(strings[i], g);
		if (xBounds < bounds.getWidth()) {
			xBounds = bounds.getWidth();
		}
		yBounds = yBounds + bounds.getHeight();
		if (i > 0) {
			yBounds = yBounds + POPUP_VSPACING;
		}
	}
	return new Dimension((int)xBounds, (int)yBounds);
}
 
Example 8
Project: DicomViewer   File: SliderComponentForTimeSlider.java   View Source Code Vote up 5 votes
@Override
public Point getToolTipLocation(MouseEvent event) {
    final String txt = getToolTipText(event);
    if (txt != null) {
        FontMetrics fm = this.getFontMetrics(getFont());
        Rectangle2D r = fm.getStringBounds(txt, getGraphics());
        return new Point(event.getX() - (int)r.getWidth()/2, -22);
    }
    return null;
}
 
Example 9
Project: BaseClient   File: CFont.java   View Source Code Vote up 4 votes
protected BufferedImage generateFontImage(Font font, boolean antiAlias, boolean fractionalMetrics, CharData[] chars)
{
    int imgSize = (int) this.imgSize;
    BufferedImage bufferedImage = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
    g.setFont(font);
    g.setColor(new Color(255, 255, 255, 0));
    g.fillRect(0, 0, imgSize, imgSize);
    g.setColor(Color.WHITE);
    g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, fractionalMetrics ? RenderingHints.VALUE_FRACTIONALMETRICS_ON : RenderingHints.VALUE_FRACTIONALMETRICS_OFF);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, antiAlias ? RenderingHints.VALUE_TEXT_ANTIALIAS_ON : RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antiAlias ? RenderingHints.VALUE_ANTIALIAS_ON : RenderingHints.VALUE_ANTIALIAS_OFF);
    FontMetrics fontMetrics = g.getFontMetrics();
    int charHeight = 0;
    int positionX = 0;
    int positionY = 1;

    for (int i = 0; i < chars.length; i++)
    {
        char ch = (char) i;
        CharData charData = new CharData();
        Rectangle2D dimensions = fontMetrics.getStringBounds(String.valueOf(ch), g);
        charData.width = (dimensions.getBounds().width + 8);
        charData.height = dimensions.getBounds().height;

        if (positionX + charData.width >= imgSize)
        {
            positionX = 0;
            positionY += charHeight;
            charHeight = 0;
        }

        if (charData.height > charHeight)
        {
            charHeight = charData.height;
        }

        charData.storedX = positionX;
        charData.storedY = positionY;

        if (charData.height > this.fontHeight)
        {
            this.fontHeight = charData.height;
        }

        chars[i] = charData;
        g.drawString(String.valueOf(ch), positionX + 2, positionY + fontMetrics.getAscent());
        positionX += charData.width;
    }

    return bufferedImage;
}