Mega Code Archive

 
Categories / Java Tutorial / 2D Graphics
 

Center text

import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import javax.swing.JFrame; public class CenterTextRectangle extends JFrame {   public static void main(String[] a) {     CenterTextRectangle f = new CenterTextRectangle();     f.setSize(300, 300);     f.setVisible(true);   }   final Font f = new Font("SansSerif", Font.BOLD, 18);   public void paint(Graphics g) {     Dimension d = this.getSize();     g.setColor(Color.white);     g.fillRect(0, 0, d.width, d.height);     g.setColor(Color.black);     g.setFont(f);     drawCenteredString("This is centered.", d.width, d.height, g);     g.drawRect(0, 0, d.width - 1, d.height - 1);   }   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);   } }