0
import javax.swing.*;
import java.applet.*;

public class MyApplet extends Applet {
        static public int m,n,p,k;
    public void init () {
        m=Integer.parseInt(getParameter("m"));
                n=Integer.parseInt(getParameter("n"));
                p=Integer.parseInt(getParameter("p"));
                k=Integer.parseInt(getParameter("k"));
    }
    public static void main(String[] args) {
            int m1,n1,k1,p1;
            System.out.println(m+""+n+""+""+k+""+p+"44");
            m1 = (args.length>0) ? Integer.parseInt(args[0]) : m;
            n1 = (args.length>1) ? Integer.parseInt(args[1]) : n;
            k1 = (args.length>2) ? Integer.parseInt(args[2]) : k;
            p1 = (args.length>3) ? Integer.parseInt(args[3]) : p;
            JFrame frame = new JFrame("App");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLocation(200,200);
            Board p;
            try {
                    p = new Board(m1,n1,k1,p1);
            } catch (Exception ex) { p = new Board(5, 5, 1, 1); }
            frame.add(p);
            frame.pack();
            frame.setVisible(true);
    }
}

I will explain first: I have those 4 parameters in HTML file, but if someone gives his own arguments then they have priority, and catch is if someone gives wrong arguments, like letters instead of numbers. Point is, I don't think init method is ever called, as you can see I added print in main method and it always prints zeroes, even when I set 'm' in init manually for 15 or something, still prints 0. Is it because it goes straight to main method, ignoring init? How can I prevent that for happening, I really need those HTML parameters to work.

4
  • Well you never actually create a MyApplet Commented May 28, 2015 at 18:21
  • One question: do you know what an Applet is? Commented May 28, 2015 at 18:31
  • What you want is a hybrid applet/application. init() must be manually invoked from main() when running as an application. Commented May 28, 2015 at 18:33
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to Why CS teachers should stop teaching Java applets. 2) Why use AWT? See this answer for many good reasons to abandon AWT using components in favor of Swing. 3) An applet typically does not have a main(String[]) method, and it is not called during normal applet execution. Commented May 29, 2015 at 5:46

1 Answer 1

2

If you use your class as an Applet then the applet container hosting it is responsible for instantiating it and invoking the lifecycle methods on it at appropriate times (init(), start(), stop(), and destroy()). Note in particular that the main() method has nothing to do with running an instance as an Applet -- it's the entry point for running your class directly on a VM as an application.

On the other hand, if you run your class as an application, nothing in its main() method does anything that would cause init() to be invoked. The main() method does not even create an instance that init() could be invoked on.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.