11

Passing parameters to an applet

  1. import java.applet.Applet;
  2. import java.awt.Graphics;
  3.  
  4. import java.awt.Color;
  5. import java.awt.Font;
  6.  
  7. public class SimpleParams extends Applet {
  8.     private Color fg = Color.white;
  9.  
  10.     private Color bg = Color.black;
  11.  
  12.     private String msg = "?";
  13.  
  14.     public void init() {
  15.         String p;
  16.  
  17.         try {
  18.             p = getParameter("fg");
  19.             if (p != null)
  20.                 fg = Color.decode(p);
  21.             p = getParameter("bg");
  22.             if (p != null)
  23.                 bg = Color.decode(p);
  24.         }
  25.         catch (java.lang.NumberFormatException e) {
  26.         }
  27.  
  28.         p = getParameter("message");
  29.         if (p != null)
  30.             msg = p;
  31.     }
  32.  
  33.     public void paint(Graphics g) {
  34.         java.awt.Dimension size = getSize();
  35.  
  36.         g.setColor(bg);
  37.         g.fillRect(0, 0, size.width - 1, size.height - 1);
  38.  
  39.         g.setColor(fg);
  40.         g.setFont(new Font("Serif", Font.BOLD + Font.ITALIC, 20));
  41.  
  42.         java.awt.FontMetrics fm = g.getFontMetrics();
  43.         int x = (size.width - fm.stringWidth(msg)) / 2;
  44.         int y = (size.height - fm.getHeight()) / 2 + fm.getAscent();
  45.         g.drawString(msg, x, y);
  46.     }
  47. }
  1. <html>
  2. <head>
  3. <title>SimpleParams</title>
  4. </head>
  5. <body>
  6. <applet code="SimpleParams" width="300" height="100">
  7.     <param name="message" value="Good day from Java" />
  8.     <param name="fg" value="#FF00FF" />
  9.     <param name="bg" value="#FCFFF0" />
  10. </applet>
  11. </body>
  12. </html>
$ javac SimpleParams.java 
$ firefox SimpleParams.html

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].