This is something that is happening in eclipse, I have a package and a have stored the applet file and the HTMLL file in the same package. (Is this the way to write an applet in eclipse?). The code is is supposed to take parameters from an HTML file to change the color, font etc on a message on a panel. The program also has a default message.
The default message gets printed when the program is run (if run w/o the HTML file) but when the parameters from the HTML file are used, I get a NPE.
Is there something I am doing wrong?
Here is the code and the corresponding HTML file for parameters.
import java.applet.Applet;
import java.awt.*;
import javax.swing.*;
public class Exercise18_04 extends JApplet {
private String message; // Message to display
private int x = 20; // Default x coordinate
private int y = 20; // Default y coordinate
private String color;
private String fontName;
private int fontSize = 20;
// Initialize the applet
public void init() {
// Get parameter values from the HTML file
message = getParameter("MESSAGE");
x = Integer.parseInt(getParameter("X"));
y = Integer.parseInt(getParameter("Y"));
color = getParameter("COLOR");
fontName = getParameter("FONTNAME");
fontSize = Integer.parseInt(getParameter("FONTSIZE"));
// Create a message panel
MessagePanel messagePanel = new MessagePanel(message);
messagePanel.setXCoordinate(x);
messagePanel.setYCoordinate(y);
if (color.equals("red")) {
messagePanel.setForeground(Color.red);
}
else if (color.equals("yellow"))
messagePanel.setForeground(Color.yellow);
else if (color.equals("green"))
messagePanel.setForeground(Color.green);
messagePanel.setFont(new Font(fontName, Font.BOLD, fontSize));
// Add the message panel to the applet
add(messagePanel);
}
}
<html>
<body>
<applet
code = "Exercise18_04.class"
width = 250
height = 80
>
<param name = MESSAGE value = "Welcome to Java" />
<param name = X value = 40 />
<param name = Y value = 50 />
<param name = COLOR value = "red" />
<param name = FONTNAME value = "Monospaced" />
<param name = FONTSIZE value = 20 />
</applet>
</body>
</html>