I can't figure out why my java applet is not displaying, I have all of the files in the same folder, and I have java enabled on my browser. Here is the code

//java code
import java.applet.*;
import java.awt.*;
import javax.swing.*;

     /**
      * The HelloWorld class implements an applet that
      * simply displays "Hello World!".
      */
     public class HelloWorld extends Applet {

        private static final long serialVersionUID = 1L;

        public void init()

        {
            //Third thing - Change applet size (Also background color is included)
            setSize(500, 500);
        }

        public void paint(Graphics g) {
            g.setColor(Color.cyan); 
            g.fillRect(0, 0, 500, 500);

            int x;
            String s = "Hello World";
            Dimension d = getSize();
            FontMetrics fm = g.getFontMetrics();
            x = d.width/2 - fm.stringWidth(s)/2;
            g.setColor(Color.black);
            g.drawString(s, x, 100);

            String s2 = "My name is Jeremy Spence";
            Dimension d2 = getSize();
            FontMetrics fm2 = g.getFontMetrics();
            x = d2.width/2 - fm2.stringWidth(s)/2;
            g.drawString(s2, x, 150);

            String input = JOptionPane.showInputDialog("Number 1: ");
            double num1 = Double.parseDouble(input);
            input = JOptionPane.showInputDialog("Number 2: ");
            double num2 = Double.parseDouble(input);
            input = JOptionPane.showInputDialog("Number 3 ");
            double num3 = Double.parseDouble(input);
            double average = (num1 + num2 + num3)/3;

            String s3 = "The average is: " + average;
            Dimension d3 = getSize();
            FontMetrics fm3 = g.getFontMetrics();
            x = d3.width/2 - fm3.stringWidth(s)/2;
            g.drawString(s3, x, 200);

         }

     }

//html code
<html>
<head>
<title>Jeremy Spence Applet</Title>
</head>
<body>
<applet Code="HelloWorld.class" width=200 Height=100></applet>
</body>
</html> 

It is madness to use JOptionPane to collect input from the user in the paint method. The paint method is called whenever the applet needs to paint itself, which could happen at almost any time so it is practically unpredictable. For example, if the dialog box covers any part of the applet then when it closes paint will be called again, and if the user moves the dialog box, paint will be called again, creating endless dialog boxes.

If you must pop up dialog boxes and you aren't trying to deliberately annoy the user, then you should only pop up your dialog boxes at carefully chosen moments when the user will be expecting a dialog box. For example, the dialog box could pop up when the user clicks some button or menu item or when the applet starts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.