Alright, this is a school assignment. Unfortunately, we were given about a paragraph of info on apps so I'm not sure what I'm doing wrong. I know it's a pretty random app, but these are the elements that she wanted.

My problem is that it runs infinitely when I run the program and when I view it as an html. Help me!

import java.applet.Applet;
     import java.awt.Graphics;
      import javax.swing.JOptionPane;
     /**
      * The HelloWorld class implements an applet that
      * simply displays "Hello World!".
      */
     public class HelloWorld extends Applet {
         public void paint(Graphics g) {
             g.drawString("Hello World Applet", 50, 25);
             g.drawString("Author: Joshua Cimbron", 50, 10);
             String input1 = JOptionPane.showInputDialog("Please enter a value");
             double a = Double.parseDouble(input1);
             String input2 = JOptionPane.showInputDialog("Please enter another value");
             double b = Double.parseDouble(input2);
             String input3 = JOptionPane.showInputDialog("Please enter a final value");
             double c = Double.parseDouble(input3);
             g.drawString(("The average is: " + average(a, b, c)), 50, 100);
             System.out.println ("Hi! This is a test! What's println do in an applet? :)");
         }
         public static double average (double a, double b, double c) {
             double sum = a + b + c;
             double average = sum / 3;
             return average;
         }
}
// html file
// file:///C:/Users/Josh/Documents/NetBeansProjects/HelloWorldApplet/build/HelloWorld.html

Recommended Answers

All 2 Replies

bump...

Your paint method gets called when the applet is first displayed, and again every time things in the applet change (that was over-simplified, but will do for now). But every time it's called you do the whole prompt-for-input-and-display-output thing, which causes paint to be called again etc ad infinitum.
You shouldn't prompt the user in paint - maybe just in init (to run once) or maybe in response to a "calc" button - it's up to you.

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.