Hello everyone,
This is my first java class and I am trying to understand the terms and different types of programs. I finished my program as an application then did it as a gui without realizing it had to be in a applet. It feels like I just learned 2 different languages because the syntax was a little different between them. Now I am trying to figure how to convert this mess into a applet. I am going to mess around with it when I get home from work. Is there any advance help you guys could give to help me start this? I would be very grateful. This is the program that I did in my application and gui. How hard would it be to convert to applet?

import javax.swing.JOptionPane;
import java.util.*;
importstatic java.lang.Math.*;


publicclass program4
{
publicstaticvoid main( String[] args )
{


int number1 = 0;
int number2 = 0;
int actualAnswer = 0;


JOptionPane.showMessageDialog(null, "You will be given math problems to solve.");

number1 = (int)( Math.random () * 9 ); // first variable that will appear in math problem
number2 = (int)( Math.random () * 9 ); // second variable that will appear in math problem
actualAnswer = number1 * number2; // variable that holds true answer to random math problem


String userInput;
double userAnswer = 0.00;
String grade;
String output;
int correctAnswers = 0;
int incorrectAnswers = 0;


userInput = JOptionPane.showInputDialog (null, "Find Product: " + "\n" +
number1 + " Times " + number2);

userAnswer = Double.parseDouble( userInput );


if (userAnswer == actualAnswer) {
JOptionPane.showMessageDialog (null, "You are correct!");
correctAnswers = correctAnswers + 1; // counter to sum total correct answers
JOptionPane.showMessageDialog (null, " You received " + correctAnswers + " Correct answer");

} // if correct, then this is the outcome
else {
JOptionPane.showMessageDialog (null, "You are incorrect! Please try again. ");
incorrectAnswers = incorrectAnswers + 1; // counter to sum total incorrect answers

while (userAnswer != actualAnswer)
{

userInput = JOptionPane.showInputDialog (null, "Find Product: " + "\n" +
number1 + " Times " + number2);
userAnswer = Double.parseDouble( userInput );

if (userAnswer == actualAnswer) {
JOptionPane.showMessageDialog (null, "Great you were able to get the answer!!");
JOptionPane.showMessageDialog (null, " You received " + incorrectAnswers + " incorrect answer");
}

}

}

Dani AI

Generated

— converting your program is doable and in many cases only requires moving the code out of main() into an applet lifecycle method and handling a few UI/runtime differences. is right to point you at applet docs; 's suggestion about clean code formatting helps too. Key points that will make the conversion safe and predictable:

  • Create an applet class (Swing apps use JApplet in older JDKs) and put the code that currently runs in main() into init() or a helper method called from init().
  • Always create or touch Swing components on the Event Dispatch Thread (use EventQueue.invokeLater or SwingUtilities.invokeLater).
  • When using dialogs from an applet, pass the applet instance (e.g., this) as the dialog parent so the dialogs center correctly and behave modal to the applet; handle null (user cancelled) and NumberFormatException from showInputDialog.

Example skeleton to adapt (move your quiz logic into startQuiz() rather than leaving it in main()):

import javax.swing.*;
import java.awt.EventQueue;

public class Program4Applet extends JApplet {
    @Override
    public void init() {
        EventQueue.invokeLater(this::startQuiz);
    }

    private void startQuiz() {
        // move the interaction logic here
        // use JOptionPane.showInputDialog(this, "...") and check for null/cancel
    }
}

Troubleshooting tips: avoid calling System.exit() from applets, break loops if the user cancels, and do input parsing in try/catch blocks. Also note that browser applets are now effectively obsolete; most browsers dropped plugin support and modern migration paths are to a standalone Swing/JavaFX application or a web-based implementation. For context on the state of applets and modern alternatives, see the Java applet overview (Wikipedia) and the OpenJFX project (JavaFX) at:

Recommended Answers

All 2 Replies

Member Avatar for Member #46692

1. try using code tags
2. find a book that shows u how to write an applet then experiment?

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.