Solved

about applets

Posted on 2001-06-09
6
199 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
eclipse apache tomcat admin console 52 147
ejb wildfly example 2 70
Notify sent to other threads in Java 9 41
restrict decimal places for double datatype 10 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

740 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