Solved

about applets

Posted on 2001-06-09
6
202 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
superschlonz earned 15 total points
ID: 6171826
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
ID: 6173589
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
ID: 6173720
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 3

Expert Comment

by:superschlonz
ID: 6173730
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
ID: 6196555
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
ID: 6198093
They are still called in the same order:
Constructor, init, start
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

734 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