Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Create simple applet, print and align text using FontMatrics

  1. /* Using Applet Dimension */
  2. /* Syntax
  3.  Dimension(Dimension d)
  4.  Dimension(int w, int h)
  5. */
  6. import java.applet.Applet;
  7. import java.awt.Graphics;
  8. import java.awt.*;
  9. import java.awt.Color;
  10. /*
  11.  <applet code = "Applet06" width = 500 height = 300>
  12.  </applet>
  13. */
  14. public class Applet06 extends Applet
  15. {
  16.  public void paint(Graphics g)
  17.  {
  18.   int x,y;
  19.   String s = "Hello World...";
  20.   Dimension d = getSize();
  21.   Font f = new Font("Arial",Font.BOLD,24);
  22.   g.setFont(f);
  23.   FontMetrics fm = g.getFontMetrics();
  24.   x = d.width/2 - fm.stringWidth(s)/2;
  25.   y = d.height/2 - fm.getHeight();
  26.   g.drawString(s,x,y);
  27.  }
  28. }
No votes yet
Share this

Post new comment

7 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.