Center Message Applet


//  this applet prints two messages that are passed to it by the html
//  file. a nice border is printed and the messages are centered.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.Color;
import java.awt.FontMetrics;
import java.awt.Image;
import java.net.URL;

public class CenterMsgApplet extends Applet
{
    String theMessage;
    String secondMessage;
    Font theFont;
    Image theWorld;
    Image theLine;

//  get the messages and font info from the html file and get the images

    public void init()
    {
        theMessage = getParameter("message");
        secondMessage = getParameter("another");
        int size = Integer.parseInt(getParameter("textsize"));
        String fontName = getParameter("font");
        theFont = new Font(fontName,Font.BOLD, size);
        this.setBackground(Color.yellow);
        theWorld = getImage(getCodeBase(),"world.gif");
        theLine = getImage(getCodeBase(),"bluebar.gif");
    }

    public void paint(Graphics g)
    {
        int messWidth;
        FontMetrics fontinfo;

//  get the size of the applet and the font size info

        int width = getSize().width;
        int height = getSize().height;
        g.setFont(theFont);
        fontinfo = g.getFontMetrics();

//  draw a nice border around the applet

        g.drawRect(0,0,width-1,height-1);
        g.drawRect(1,1,width-3,height-3);
        g.drawRect(3,3,width-7,height-7);
        g.drawRect(4,4,width-9,height-9);

//  put the images in the applet

        g.drawImage(theWorld,(width-50)/2,50,this);
        g.drawImage(theLine,25,100,this);

//  center the messages and print them in red

        g.setColor(Color.red);
        messWidth = fontinfo.stringWidth(theMessage);
        g.drawString(theMessage,(width-messWidth)/2,150);
        messWidth = fontinfo.stringWidth(secondMessage);
        g.drawString(secondMessage,(width-messWidth)/2,225);
    }
}