當前位置: 首頁>>代碼示例>>Java>>正文


Java Graphics.getFontMetrics方法代碼示例

本文整理匯總了Java中java.awt.Graphics.getFontMetrics方法的典型用法代碼示例。如果您正苦於以下問題:Java Graphics.getFontMetrics方法的具體用法?Java Graphics.getFontMetrics怎麽用?Java Graphics.getFontMetrics使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.awt.Graphics的用法示例。


在下文中一共展示了Graphics.getFontMetrics方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: drawString

import java.awt.Graphics; //導入方法依賴的package包/類
public void drawString(Graphics g, String string, int x, int y, int width) {
    FontMetrics fontMetrics = g.getFontMetrics();
    int lineHeight = fontMetrics.getHeight();

    int curX = x;
    int curY = y;

    String[] words = string.split(" ");

    for (String word : words) {
        // Find out thw width of the word.
        int wordWidth = fontMetrics.stringWidth(word + " ");

        // If text exceeds the width, then move to next line.
        if (curX + wordWidth >= x + width) {
            curY += lineHeight;
            curX = x;
        }

        g.drawString(word, curX, curY);

        // Move over to the right for next word.
        curX += wordWidth;
    }
}
 
開發者ID:gengwg,項目名稱:java_mooc_fi,代碼行數:26,代碼來源:HangmanFigure.java

示例2: computePreferredSize

import java.awt.Graphics; //導入方法依賴的package包/類
private void computePreferredSize() {
	int inputs = table.getInputColumnCount();
	int outputs = table.getOutputColumnCount();
	if (inputs == 0 && outputs == 0) {
		setPreferredSize(new Dimension(0, 0));
		return;
	}

	Graphics g = getGraphics();
	if (g == null) {
		cellHeight = 16;
		cellWidth = 24;
	} else {
		FontMetrics fm = g.getFontMetrics(HEAD_FONT);
		cellHeight = fm.getHeight();
		cellWidth = 24;
		if (inputs == 0 || outputs == 0) {
			cellWidth = Math.max(cellWidth, fm.stringWidth(Strings.get("tableNullHeader")));
		}
		for (int i = 0; i < inputs + outputs; i++) {
			String header = i < inputs ? table.getInputHeader(i) : table.getOutputHeader(i - inputs);
			cellWidth = Math.max(cellWidth, fm.stringWidth(header));
		}
	}

	if (inputs == 0)
		inputs = 1;
	if (outputs == 0)
		outputs = 1;
	tableWidth = (cellWidth + COLUMN_SEP) * (inputs + outputs) - COLUMN_SEP;
	tableHeight = cellHeight * (1 + table.getRowCount()) + HEADER_SEP;
	setPreferredSize(new Dimension(tableWidth, tableHeight));
	revalidate();
	repaint();
}
 
開發者ID:LogisimIt,項目名稱:Logisim,代碼行數:36,代碼來源:TableTab.java

示例3: getWidth

import java.awt.Graphics; //導入方法依賴的package包/類
public int getWidth() {
    if (shortName == null || shortName.length() <= 1) {
        return Figure.SLOT_WIDTH;
    } else {
        BufferedImage image = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        g.setFont(figure.getDiagram().getSlotFont().deriveFont(Font.BOLD));
        FontMetrics metrics = g.getFontMetrics();
        return Math.max(Figure.SLOT_WIDTH, metrics.stringWidth(shortName) + 6);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:12,代碼來源:Slot.java

示例4: setExpression

import java.awt.Graphics; //導入方法依賴的package包/類
public void setExpression(Expression expr) {
	ExpressionData exprData = new ExpressionData(expr);
	Graphics g = getGraphics();
	FontMetrics fm = g == null ? null : g.getFontMetrics();
	renderData = new RenderData(exprData, getWidth(), fm);
	setPreferredSize(renderData.getPreferredSize());
	revalidate();
	repaint();
}
 
開發者ID:LogisimIt,項目名稱:Logisim,代碼行數:10,代碼來源:ExpressionView.java

示例5: paintText

import java.awt.Graphics; //導入方法依賴的package包/類
/**
 * Renders the text.
 */
private void paintText(Graphics g, int w, int h) {
    g.setFont(getFont());
    String text = getHeapSizeText();
    FontMetrics fm = g.getFontMetrics();
    int textWidth = fm.stringWidth(text);
    g.drawString(text, (w - maxTextWidth) / 2 + (maxTextWidth - textWidth), 
            h / 2 + fm.getAscent() / 2 - 2);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:12,代碼來源:HeapView.java

示例6: trialDrawY

import java.awt.Graphics; //導入方法依賴的package包/類
/**
 * probne rysowanie - policznie wymiarow w dół
 *
 * @param g - kontekst graficzny
 */
public void trialDrawY(Graphics g) {
    if (itemsToDraw == null) {
        return;
    }

    // policzenie wymiarow
    FontMetrics metrics = g.getFontMetrics();
    fontHeight = metrics.getHeight();
    int pos = fontHeight;
    int length = 0;
    for (String text : itemsToDraw) {
        length = Math.max(length, metrics.stringWidth(text));
        pos += fontHeight;
    }
    dimension.width = length + 4;
    dimension.height = pos - fontHeight + 4;

    // policzenie obrysu z dziecmi
    bound.width = 0;
    bound.height = 0;
    for (NodeDrawer node : nodes) {
        node.trialDrawY(g);
        bound.width += node.getBound().width + X_DISTANCE;
        bound.height = Math.max(node.getBound().height, bound.height);
    }
    bound.width = Math.max(dimension.width, bound.width - X_DISTANCE);
    bound.height = dimension.height + Y_DISTANCE + bound.height;

    // korekta na odstep
    if (nodes.isEmpty()) {
        bound.height -= Y_DISTANCE;
    }
}
 
開發者ID:CLARIN-PL,項目名稱:WordnetLoom,代碼行數:39,代碼來源:NodeDrawer.java

示例7: createPullButton

import java.awt.Graphics; //導入方法依賴的package包/類
/**
* Create "Pull" button.
* 
* @return the "Pull" button.
*/
private ToolbarButton createPullButton() {
  return new ToolbarButton(createPullAction(), false) {
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);

		String str = "";
		if (pullsBehind > 0) {
			str = "" + pullsBehind;
		}
		if (pullsBehind > 9) {
			pullButton.setHorizontalAlignment(SwingConstants.LEFT);
		} else {
			pullButton.setHorizontalAlignment(SwingConstants.CENTER);
		}
		g.setFont(g.getFont().deriveFont(Font.BOLD, 8.5f));
		FontMetrics fontMetrics = g.getFontMetrics(g.getFont());
		int stringWidth = fontMetrics.stringWidth(str);
		int stringHeight = fontMetrics.getHeight();
		g.setColor(new Color(255, 255, 255, 100));
		g.fillRect(pullButton.getWidth() - stringWidth, 0, stringWidth, stringHeight);
		g.setColor(Color.BLACK);
		g.drawString(str, pullButton.getWidth() - stringWidth,
				fontMetrics.getHeight() - fontMetrics.getDescent() - fontMetrics.getLeading());
	}
};
}
 
開發者ID:oxygenxml,項目名稱:oxygen-git-plugin,代碼行數:33,代碼來源:ToolbarPanel.java

示例8: getPreferredSize

import java.awt.Graphics; //導入方法依賴的package包/類
public Dimension getPreferredSize(JComponent c) {
    int prefHeight = 28;
    Graphics g = BasicScrollingTabDisplayerUI.getOffscreenGraphics();
    if (g != null) {
        FontMetrics fm = g.getFontMetrics(displayer.getFont());
        Insets ins = getTabAreaInsets();
        prefHeight = fm.getHeight() + ins.top + ins.bottom + (isGenericUI ? 5 : 6);
    }
    return new Dimension(displayer.getWidth(), prefHeight);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:11,代碼來源:WinClassicEditorTabDisplayerUI.java

示例9: getFontMetrics

import java.awt.Graphics; //導入方法依賴的package包/類
/** Get the font-metrics for the given font.
* @param font font for which the metrics is being retrieved.
* @param g graphics that is used to retrieve the metrics in case it's
*   not yet in the cache.
*/
public static synchronized FontMetrics getFontMetrics(Font f, Graphics g) {
    Object fm = font2FM.get(f);
    if (fm == null) {
        fm = g.getFontMetrics(f);
        font2FM.put(f, fm);
    }
    return (FontMetrics)fm;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:14,代碼來源:FontMetricsCache.java

示例10: getTextBounds

import java.awt.Graphics; //導入方法依賴的package包/類
static public Rectangle getTextBounds(Graphics g, String text, int x, int y, int halign, int valign) {
	if (g == null)
		return new Rectangle(x, y, 0, 0);
	FontMetrics mets = g.getFontMetrics();
	int width = mets.stringWidth(text);
	int ascent = mets.getAscent();
	int descent = mets.getDescent();
	int height = ascent + descent;

	Rectangle ret = new Rectangle(x, y, width, height);
	switch (halign) {
	case H_CENTER:
		ret.translate(-(width / 2), 0);
		break;
	case H_RIGHT:
		ret.translate(-width, 0);
		break;
	default:
		;
	}
	switch (valign) {
	case V_TOP:
		break;
	case V_CENTER:
		ret.translate(0, -(ascent / 2));
		break;
	case V_CENTER_OVERALL:
		ret.translate(0, -(height / 2));
		break;
	case V_BASELINE:
		ret.translate(0, -ascent);
		break;
	case V_BOTTOM:
		ret.translate(0, -height);
		break;
	default:
		;
	}
	return ret;
}
 
開發者ID:LogisimIt,項目名稱:Logisim,代碼行數:41,代碼來源:GraphicsUtil.java

示例11: paint

import java.awt.Graphics; //導入方法依賴的package包/類
@Override
public void paint(Graphics g) {
    int fontSize = g.getFont().getSize();
    int x = 0, y = fontSize, space;
    int red = (int) (50 * Math.random());
    int green = (int) (50 * Math.random());
    int blue = (int) (256 * Math.random());
    Dimension d = getSize();
    g.setColor(Color.black);
    FontMetrics fm = g.getFontMetrics();
    space = fm.stringWidth(" ");
    for (StringTokenizer t = new StringTokenizer(labelString);
            t.hasMoreTokens();) {
        String word = t.nextToken();
        int w = fm.stringWidth(word) + space;
        if (x + w > d.width) {
            x = 0;
            y += fontSize;  //move word to next line if it doesn't fit
        }
        if (Math.random() < 0.5) {
            g.setColor(new java.awt.Color((red + y * 30) % 256,
                    (green + x / 3) % 256, blue));
        } else {
            g.setColor(getBackground());
        }
        g.drawString(word, x, y);
        x += w;  //shift to the right to draw the next word
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:30,代碼來源:Blink.java

示例12: calcRowHeight

import java.awt.Graphics; //導入方法依賴的package包/類
/**
 * Calculate the height of rows based on the current font.  This is done
 * when the first paint occurs, to ensure that a valid Graphics object is
 * available.
 *
 * @since 1.25
 */
private void calcRowHeight(Graphics g) {
    Font f = getFont();
    FontMetrics fm = g.getFontMetrics(f);
    // As icons are displayed use maximum from font and icon height
    int rowHeight = Math.max(fm.getHeight(), 16) + 4;
    needCalcRowHeight = false;
    setRowHeight(rowHeight);
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:16,代碼來源:SwitcherTable.java

示例13: paint

import java.awt.Graphics; //導入方法依賴的package包/類
@Override public void paint(Graphics graphics) {
	graphics.setColor(Color.BLACK);
	graphics.setFont(graphics.getFont().deriveFont(8f));
	int top = size.height/4, height=graphics.getFontMetrics().getAscent()/2;
	graphics.drawRect(5, 0, size.width-10, size.height);
	graphics.drawString("A", 10, top+height);
	graphics.drawString("B", 10, top*3+height);
	graphics.drawString("S", size.width-15, top+height);
	graphics.drawString("C", size.width-15, top*3+height);
	graphics.setFont(graphics.getFont().deriveFont(Font.BOLD, 15f));
	FontMetrics metrics = graphics.getFontMetrics();
	graphics.drawString("HA", size.width/2-metrics.stringWidth("HA")/2,
			size.height/2+metrics.getAscent()/3);
	ContactUtilities.paintSolderingJoints(graphics, contacts);
}
 
開發者ID:kristian,項目名稱:JDigitalSimulator,代碼行數:16,代碼來源:HalfAdder.java

示例14: checkLinesWidth

import java.awt.Graphics; //導入方法依賴的package包/類
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;
}
 
開發者ID:apache,項目名稱:incubator-netbeans,代碼行數:13,代碼來源:LineNumbersActionsBar.java

示例15: getNumberWidthY

import java.awt.Graphics; //導入方法依賴的package包/類
/**
 * Calculates max number width for y-axis numeration.
 * 
 * @param g
 *            The {@link Graphics} object.
 * @return Maximum number width.
 */
private int getNumberWidthY(Graphics g) {
    FontMetrics fm = g.getFontMetrics();

    int dimension = 0;
    int yValueStart = chart.getyMin();
    int yValueEnd = chart.getyMax();
    int yValue = Math.max(Math.abs(yValueEnd), Math.abs(yValueStart));

    int width = fm.stringWidth(Integer.toString(yValue));
    dimension = Math.max(width, dimension);

    return dimension;
}
 
開發者ID:fgulan,項目名稱:java-course,代碼行數:21,代碼來源:BarChartComponent.java


注:本文中的java.awt.Graphics.getFontMetrics方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。