List of usage examples for java.awt Graphics getFontMetrics
public FontMetrics getFontMetrics()
From source file:com.headswilllol.basiclauncher.Launcher.java
private static int centerText(Graphics g, String text) { int stringLen = (int) g.getFontMetrics().getStringBounds(text, g).getWidth(); return width / 2 - stringLen / 2; }
From source file:CenterTextRectangle.java
public void drawCenteredString(String s, int w, int h, Graphics g) { FontMetrics fm = g.getFontMetrics(); int x = (w - fm.stringWidth(s)) / 2; int y = (fm.getAscent() + (h - (fm.getAscent() + fm.getDescent())) / 2); g.drawString(s, x, y);// w w w . j a v a 2 s .c o m }
From source file:VASSAL.counters.Labeler.java
public static void drawLabel(Graphics g, String text, int x, int y, Font f, int hAlign, int vAlign, Color fgColor, Color bgColor, Color borderColor) { g.setFont(f);// w ww . j a va 2 s.c o m final int width = g.getFontMetrics().stringWidth(text + " "); final int height = g.getFontMetrics().getHeight(); int x0 = x; int y0 = y; switch (hAlign) { case CENTER: x0 = x - width / 2; break; case LEFT: x0 = x - width; break; } switch (vAlign) { case CENTER: y0 = y - height / 2; break; case BOTTOM: y0 = y - height; break; } if (bgColor != null) { g.setColor(bgColor); g.fillRect(x0, y0, width, height); } if (borderColor != null) { g.setColor(borderColor); g.drawRect(x0, y0, width, height); } g.setColor(fgColor); ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.drawString(" " + text + " ", x0, y0 + g.getFontMetrics().getHeight() - g.getFontMetrics().getDescent()); }
From source file:TextBox3D.java
public synchronized void paint(Graphics g) { FontMetrics fm = g.getFontMetrics(); Dimension size = getSize();/*from w w w .j a v a 2 s.c o m*/ int x = (size.width - fm.stringWidth(text)) / 2; int y = (size.height - fm.getHeight()) / 2; g.setColor(SystemColor.control); g.fillRect(0, 0, size.width, size.height); g.setColor(SystemColor.controlShadow); g.drawLine(0, 0, 0, size.height - 1); g.drawLine(0, 0, size.width - 1, 0); g.setColor(SystemColor.controlDkShadow); g.drawLine(0, size.height - 1, size.width - 1, size.height - 1); g.drawLine(size.width - 1, 0, size.width - 1, size.height - 1); g.setColor(SystemColor.controlText); g.drawString(text, x, y); }
From source file:Center.java
License:asdf
public void paint(Graphics g) { g.translate(100, 100);//from www . j a v a2 s .c o m FontMetrics fm = g.getFontMetrics(); for (int i = 0; i < text.length; i++) { int x, y; x = (getWidth() - fm.stringWidth(text[i])) / 2; y = (i + 1) * fm.getHeight() - 1; g.drawString(text[i], x, y); } }
From source file:Main.java
public void paint(Graphics g) { g.setFont(new Font("SansSerif", Font.BOLD, 12)); FontMetrics fm = g.getFontMetrics(); g.drawString("Current font: " + g.getFont(), 10, 40); g.drawString("Ascent: " + fm.getAscent(), 10, 55); g.drawString("Descent: " + fm.getDescent(), 10, 70); g.drawString("Height: " + fm.getHeight(), 10, 85); g.drawString("Leading: " + fm.getLeading(), 10, 100); Font font = new Font("Serif", Font.ITALIC, 14); fm = g.getFontMetrics(font);//w w w . j a v a2s .c om g.setFont(font); g.drawString("Current font: " + font, 10, 130); g.drawString("Ascent: " + fm.getAscent(), 10, 145); g.drawString("Descent: " + fm.getDescent(), 10, 160); g.drawString("Height: " + fm.getHeight(), 10, 175); g.drawString("Leading: " + fm.getLeading(), 10, 190); }
From source file:Main.java
public void paint(Graphics g) { m_fm = g.getFontMetrics(); g.setColor(getBackground());// w ww. j a v a 2s .c om g.fillRect(0, 0, getWidth(), getHeight()); getBorder().paintBorder(this, g, 0, 0, getWidth(), getHeight()); g.setColor(getForeground()); g.setFont(getFont()); Insets insets = getInsets(); int x = insets.left; int y = insets.top + m_fm.getAscent(); StringTokenizer st = new StringTokenizer(getText(), "\t"); while (st.hasMoreTokens()) { String str = st.nextToken(); g.drawString(str, x, y); //insert distance for each tab x += m_fm.stringWidth(str) + 50; if (!st.hasMoreTokens()) break; } }
From source file:Main.java
private void drawString(Graphics g, String text, int x, int y) { for (String line : text.split("\n")) g.drawString(line, x, y += g.getFontMetrics().getHeight()); }
From source file:BasicDraw.java
public void paint(Graphics g) { Font font = new Font("Serif", Font.PLAIN, 12); g.setFont(font);//from ww w . ja v a 2s . c om g.drawString("a String", 10, 10); FontMetrics fontMetrics = g.getFontMetrics(); g.drawString("aString", 10, 10 + fontMetrics.getAscent()); }
From source file:Main.java
public void paint(Graphics g) { int fontSize = 20; g.setFont(new Font("TimesRoman", Font.PLAIN, fontSize)); FontMetrics fm = g.getFontMetrics(); String s = "www.java2s.com"; int stringWidth = fm.stringWidth(s); int w = 200;//from w ww . j av a 2s . c om int h = 200; int x = (w - stringWidth) / 2; int baseline = fm.getMaxAscent() + (h - (fm.getAscent() + fm.getMaxDecent())) / 2; int ascent = fm.getMaxAscent(); int descent = fm.getMaxDecent(); int fontHeight = fm.getMaxAscent() + fm.getMaxDecent(); g.setColor(Color.white); g.fillRect(x, baseline - ascent, stringWidth, fontHeight); g.setColor(Color.gray); g.drawLine(x, baseline, x + stringWidth, baseline); g.setColor(Color.red); g.drawLine(x, baseline + descent, x + stringWidth, baseline + descent); g.setColor(Color.blue); g.drawLine(x, baseline - ascent, x + stringWidth, baseline - ascent); g.setColor(Color.black); g.drawString(s, x, baseline); }