2

Display a text with a border

  1. // <applet code="Text3D" width="300" height="100">
  2. // <param name="text" value="Java is so much fun!"/>
  3. // </applet>
  4.  
  5. import java.applet.Applet;
  6. import java.awt.*;
  7.  
  8. public class Text3D extends Applet {
  9.     protected String text = "?";
  10.  
  11.     public void init() {
  12.         String p = getParameter("text");
  13.         if (p != null)
  14.             text = p;
  15.         setBackground(Color.CYAN);
  16.         setFont(new Font("Serif", Font.BOLD, 20));
  17.     }
  18.  
  19.     public void paint(Graphics g) {
  20.         Dimension size = getSize();
  21.         FontMetrics fm = g.getFontMetrics();
  22.  
  23.         int w = fm.stringWidth(text);
  24.         int h = fm.getHeight();
  25.         int x = (size.width - w) / 2;
  26.         int y = (size.height - h) / 2;
  27.  
  28.         g.setColor(Color.PINK);
  29.         g.fill3DRect(x - 8, y - 8, w + 16, h + 16, true);
  30.         g.draw3DRect(x - 8, y - 8, w + 16, h + 16, true);
  31.         g.draw3DRect(x - 6, y - 6, w + 12, h + 12, false);
  32.  
  33.         // MUST write on baseline
  34.         g.setColor(Color.GRAY);
  35.         g.drawString(text, x, y + fm.getAscent());
  36.     }
  37. }
$ javac Text3D.java 
$ appletviewer Text3D.java

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].