2
Display a text with a border
- // <applet code="Text3D" width="300" height="100">
- // <param name="text" value="Java is so much fun!"/>
- // </applet>
- import java.applet.Applet;
- import java.awt.*;
- public class Text3D extends Applet {
- protected String text = "?";
- public void init() {
- String p = getParameter("text");
- if (p != null)
- text = p;
- setBackground(Color.CYAN);
- setFont(new Font("Serif", Font.BOLD, 20));
- }
- public void paint(Graphics g) {
- Dimension size = getSize();
- FontMetrics fm = g.getFontMetrics();
- int w = fm.stringWidth(text);
- int h = fm.getHeight();
- int x = (size.width - w) / 2;
- int y = (size.height - h) / 2;
- g.setColor(Color.PINK);
- g.fill3DRect(x - 8, y - 8, w + 16, h + 16, true);
- g.draw3DRect(x - 8, y - 8, w + 16, h + 16, true);
- g.draw3DRect(x - 6, y - 6, w + 12, h + 12, false);
- // MUST write on baseline
- g.setColor(Color.GRAY);
- g.drawString(text, x, y + fm.getAscent());
- }
- }
$ javac Text3D.java
$ appletviewer Text3D.java
Comments