Solved

about applets

Posted on 2001-06-09
6
189 Views
Last Modified: 2013-11-23
in an applet if constructor, init() method, start() method and paint() method are called, what will be the execution flow of these methods
0
Comment
Question by:farooqnasim
  • 4
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
superschlonz earned 15 total points
Comment Utility
After the applet has been constructed init() is called. Here you can do some initialisations
like creating the gui components and adding them to the applet.
then start() is called. Here you could start a thread that does some work.
when stop() is called you should stop that thread.
in the destroy() method you should release all resources.
paint() is called whe the component must be painted the first time or when it must be repainted.
If you use buttons, textfields and other awt or swing components you dont need to override
the paint method.
0
 

Author Comment

by:farooqnasim
Comment Utility
i knew all these things, there is nothing new, i repeat the question again,
      see the code below

import java.applet.Applet;
import java.awt.*;
class MyApplet extends Applet{
          Button b1,b2;
          MyApplet(){
          b1=new Button("one");
           add(b1);
           setBackground(Color.blue);
       }            
          public void init(){
          b2=new Button("Two");
          add(b2);

          setBackground(Color.red);
       }
}
now whether init() or constructor will be called first
or both at the same time and if a new thread is created in constructor, then what will happen
0
 
LVL 3

Expert Comment

by:superschlonz
Comment Utility
There must be an instance of MyApplet to call init.
So first the constructor must be called. After that init will be called.

I compiled your code and the applet displays 2 buttons and the background is red.

But I had to change 2 lines for getting it running: The class must be public and the
constructor also.

Now it looks like this:

...
public class MyApplet extends Applet {
  ...
  public MyApplet() {
  ...
}
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 3

Expert Comment

by:superschlonz
Comment Utility
Oh I forgot the Thread.

If you create a thread in constructor and start it, it will run and do its work. But you
should start threads in init() if they initialize something or in start() if they show an
animation for example.
I don't know what will happen if you start it in the constructor and the user presses
reload, perhaps only stop(), destroy(), init() and start() are called.
0
 

Author Comment

by:farooqnasim
Comment Utility
the answer is again incomplete, if a thread is created inside the constructor of an applet then what will happen, what method will be executed first, init() or start()
0
 
LVL 3

Expert Comment

by:superschlonz
Comment Utility
They are still called in the same order:
Constructor, init, start
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to use external config file with Spring MVC 4 59
pairs challenge 5 44
for i loop in grovy 1 32
unix in java example 9 37
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now