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.

Holger
  • 285,553
  • 42
  • 434
  • 765
Shadov
  • 5,421
  • 2
  • 19
  • 38
  • Well you never actually create a `MyApplet` – thatidiotguy May 28 '15 at 18:21
  • One question: do you know what an **Applet** is? – Holger May 28 '15 at 18:31
  • What you want is a _hybrid applet/application_. `init()` must be manually invoked from `main()` when running as an application. – JosEduSol May 28 '15 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](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) 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. – Andrew Thompson May 29 '15 at 05:46

1 Answers1

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.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157