11
Passing parameters to an applet
- import java.applet.Applet;
- import java.awt.Graphics;
- import java.awt.Color;
- import java.awt.Font;
- public class SimpleParams extends Applet {
- private Color fg = Color.white;
- private Color bg = Color.black;
- private String msg = "?";
- public void init() {
- String p;
- try {
- p = getParameter("fg");
- if (p != null)
- fg = Color.decode(p);
- p = getParameter("bg");
- if (p != null)
- bg = Color.decode(p);
- }
- catch (java.lang.NumberFormatException e) {
- }
- p = getParameter("message");
- if (p != null)
- msg = p;
- }
- public void paint(Graphics g) {
- java.awt.Dimension size = getSize();
- g.setColor(bg);
- g.fillRect(0, 0, size.width - 1, size.height - 1);
- g.setColor(fg);
- g.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 20));
- java.awt.FontMetrics fm = g.getFontMetrics();
- int x = (size.width - fm.stringWidth(msg)) / 2;
- int y = (size.height - fm.getHeight()) / 2 + fm.getAscent();
- g.drawString(msg, x, y);
- }
- }
- <html>
- <head>
- <title>SimpleParams</title>
- </head>
- <body>
- <applet code="SimpleParams" width="300" height="100">
- <param name="message" value="Good day from Java" />
- <param name="fg" value="#FF00FF" />
- <param name="bg" value="#FCFFF0" />
- </applet>
- </body>
- </html>
$ javac SimpleParams.java
$ firefox SimpleParams.html
Comments