List of usage examples for java.awt FontMetrics stringWidth
public int stringWidth(String str)
From source file:Main.java
@Override protected void paintComponent(Graphics g) { super.paintComponent(g); FontMetrics fm = g.getFontMetrics(font); wordWidth = fm.stringWidth(word); wordHeight = fm.getAscent();//ww w .j a v a2s . c o m g.setFont(new Font("impact", Font.PLAIN, 28)); g.setColor(Color.BLUE); g.drawString(word, x, y); }
From source file:TextBox3D.java
public synchronized void paint(Graphics g) { FontMetrics fm = g.getFontMetrics(); Dimension size = getSize();//from w ww. j ava2s .co 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:Main.java
public void render(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.WHITE);//from w w w . j ava2 s. c o m Font font = new Font("Verdana", Font.PLAIN, 20); g2d.setFont(font); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); FontMetrics fm = g2d.getFontMetrics(); String option = "This is a test"; int x = (getWidth() - fm.stringWidth(option)) / 2; int y = ((getHeight() - fm.getHeight()) / 2); g2d.drawString(option, x, y + fm.getAscent()); g2d.drawRect((int) x - 20, (int) y - 10, (int) fm.stringWidth(option) + 40, (int) fm.getHeight() + 20); }
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 w w .java2 s. co m*/ 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); }
From source file:Center.java
License:asdf
public void addNotify() { super.addNotify(); int maxWidth = 0; FontMetrics fm = getFontMetrics(getFont()); for (int i = 0; i < text.length; i++) { maxWidth = Math.max(maxWidth, fm.stringWidth(text[i])); }// w w w .j a v a 2s. co m Insets inset = getInsets(); dim = new Dimension(maxWidth + inset.left + inset.right, text.length * fm.getHeight() + inset.top + inset.bottom); setSize(dim); }
From source file:MainClass.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;// ww w . j a v a2s. com 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); }
From source file:Center.java
License:asdf
public void paint(Graphics g) { g.translate(100, 100);/* w ww. j a va2 s . com*/ 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) { int fontSize = 20; Font font = new Font("TimesRoman", Font.PLAIN, fontSize); g.setFont(font);// www . j av a2s . com FontMetrics fm = g.getFontMetrics(font); String s = "www.java2s.com"; int stringWidth = fm.stringWidth(s); int w = 200; 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); }
From source file:MyCanvas.java
public void paintComponent(Graphics g) { super.paintComponent(g); // draw entire component white g.setColor(Color.white);/*from w w w .jav a 2s.c om*/ g.fillRect(0, 0, getWidth(), getHeight()); // yellow circle g.setColor(Color.yellow); g.fillOval(0, 0, 240, 240); // magenta circle g.setColor(Color.magenta); g.fillOval(160, 160, 240, 240); // paint the icon below blue sqaure int w = java2sLogo.getIconWidth(); int h = java2sLogo.getIconHeight(); java2sLogo.paintIcon(this, g, 280 - (w / 2), 120 - (h / 2)); // paint the icon below red sqaure java2sLogo.paintIcon(this, g, 120 - (w / 2), 280 - (h / 2)); // transparent red square g.setColor(m_tRed); g.fillRect(60, 220, 120, 120); // transparent green circle g.setColor(m_tGreen); g.fillOval(140, 140, 120, 120); // transparent blue square g.setColor(m_tBlue); g.fillRect(220, 60, 120, 120); g.setColor(Color.black); g.setFont(monoFont); FontMetrics fm = g.getFontMetrics(); w = fm.stringWidth("Java Source"); h = fm.getAscent(); g.drawString("Java Source", 120 - (w / 2), 120 + (h / 4)); g.setFont(sanSerifFont); fm = g.getFontMetrics(); w = fm.stringWidth("and"); h = fm.getAscent(); g.drawString("and", 200 - (w / 2), 200 + (h / 4)); g.setFont(serifFont); fm = g.getFontMetrics(); w = fm.stringWidth("Support."); h = fm.getAscent(); g.drawString("Support.", 280 - (w / 2), 280 + (h / 4)); }
From source file:ImageLabel.java
public Dimension getPreferredSize() { FontMetrics metrics = img.getGraphics().getFontMetrics(font); int width = metrics.stringWidth(text) * 2; int height = metrics.getHeight() * 2; return new Dimension(width, height); }