1  import javax.swing.*;
  2  import java.awt.Font;
  3  import java.awt.BorderLayout;
  4  
  5  public class DisplayMessageApp extends JApplet {
  6    private String message = "A default message"; // Message to display
  7    private int x = 20; // Default x coordinate
  8    private int y = 20; // Default y coordinate
  9  
 10    /** Determine if it is application */
 11    private boolean isStandalone = false;
 12  
 13    /** Initialize the applet */
 14    public void init() {
 15      if (!isStandalone) {
 16        // Get parameter values from the HTML file
 17        message = getParameter("MESSAGE");
 18        x = Integer.parseInt(getParameter("X"));
 19        y = Integer.parseInt(getParameter("Y"));
 20      }
 21  
 22      // Create a message panel
 23      MessagePanel messagePanel = new MessagePanel(message);
 24      messagePanel.setFont(new Font("SansSerif", Font.BOLD, 20));
 25      messagePanel.setXCoordinate(x);
 26      messagePanel.setYCoordinate(y);
 27  
 28      // Add the message panel to the applet
 29      add(messagePanel);
 30    }
 31  
 32    /** Main method to display a message
 33       @param args[0] x coordinate
 34       @param args[1] y coordinate
 35       @param args[2] message
 36      */
 37    public static void main(String[] args) {
 38      // Create a frame
 39      JFrame frame = new JFrame("DisplayMessageApp");
 40  
 41      // Create an instance of the applet
 42      DisplayMessageApp applet = new DisplayMessageApp();
 43  
 44      // It runs as an application
 45      applet.isStandalone = true;
 46  
 47      // Get parameters from the command line
 48      applet.getCommandLineParameters(args);
 49  
 50      // Add the applet instance to the frame
 51      frame.add(applet, BorderLayout.CENTER);
 52  
 53      // Invoke applet’s init method
 54      applet.init();
 55      applet.start();
 56  
 57      // Display the frame
 58      frame.setSize(300, 300);
 59      frame.setLocationRelativeTo(null); // Center the frame
 60      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 61      frame.setVisible(true);
 62    }
 63  
 64    /** Get command line parameters */
 65    private void getCommandLineParameters(String[] args) {
 66      // Check usage and get x, y and message
 67      if (args.length != 3) {
 68        System.out.println(
 69          "Usage: java DisplayMessageApp x y message");
 70        System.exit(1);
 71      }
 72      else {
 73        x = Integer.parseInt(args[0]);
 74        y = Integer.parseInt(args[1]);
 75        message = args[2];
 76      }
 77    }
 78  }