0
package Program1;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Scanner;

public class Demo extends Applet{
    static int i=0;

    public static void test(){
        i=50;
        System.out.println("This is my static method");
    }


    public void paint (Graphics g)  {
        test();
        g.drawOval (60, 20, i, i);
    }

}

I have two method paint and test. I want to execute test method only once which is going to initialize the value of i and that i am going to use in paint method. But the problem is when I am executing the program it is calling text method once and when I am re-sizing the Applet window it is calling that test method again and again.

For my program the text method do lot of computation so calling test method just to assign the value again and again is not a good choice. How to call that Demo class only once ?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Rahul Kumar
  • 2,184
  • 3
  • 24
  • 46
  • use a variable to identify if it has run yet or not ... – SomeJavaGuy Mar 01 '16 at 06:56
  • Ya using a separate flag variable I can do that but creating a new variable and assigning and comparing I don't feel is a good choice. There should be better method for this. – Rahul Kumar Mar 01 '16 at 06:57
  • 1
    then put the `test` method inside of a constructor if it´s only doing initializations – SomeJavaGuy Mar 01 '16 at 06:58
  • 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. – Andrew Thompson Mar 02 '16 at 02:39

3 Answers3

2

You can use Applet class init() method..which is specially provided for this purpose.

Arker
  • 61
  • 5
  • Agree. The `init()` method which is guaranteed to be called only once, rather than the constructor, is the correct place to add the method call. – Andrew Thompson Mar 02 '16 at 02:43
1
package Program1;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Scanner;

public class Demo extends Applet{
    static int i=0;
    boolean hasRun = false;

    public Demo (){
       this.test();
       this.hasRun = true;
    }

    public static void test(){
        if (hasRun==false){
           i=50;
           System.out.println("This is my static method");
        }
    }


    public void paint (Graphics g)  {
        test();
        g.drawOval (60, 20, i, i);
    }

}

If you do NOT want Demo to have a constructor then create a method called and call it after you initialize.

public void setFlag() {
   hasRun = false;
}
codeCompiler77
  • 508
  • 7
  • 22
0

Using constructor class I can call this test method once.

package Program1;

import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.Scanner;

public class Demo extends Applet{
    static int i=0;

    public static void test(){
        i=50;
        System.out.println("This is my static method");
    }

    public Demo(){
        test();
    }


    public void paint (Graphics g)  {

        g.drawOval (60, 20, i, i);
    }

}
Rahul Kumar
  • 2,184
  • 3
  • 24
  • 46