The Java Program: Text.java
1 // Text.java -- a simple Java applet
2
3 /*
4 Example of how to use this applet in an HTML file:
5 <applet code="Text" width=200 height=50>
6 <param name="text" value="Hallo Welt!">
7 </applet>
8 */
9
10 import java.applet.Applet;
11 import java.awt.*;
12
13 public class Text extends Applet {
14 private String text;
15
16 public void init() {
17 setBackground (Color.white); // Make applet window stand out
18 text = getParameter("text");
19 if (text == null) {
20 text = "Hello world!";
21 }
22 }
23
24 public void paint (Graphics g) {
25 // Put text in the center of the window
26 Dimension d = this.size(); // Get the size of the window
27 FontMetrics m = g.getFontMetrics();
28 int a = m.getAscent();
29 int baseline = a + (d.height - (a + m.getDescent()))/2;
30 g.drawString (text, (d.width-m.stringWidth(text))/2, baseline);
31 }
32
33 }