Java: In what order are the methods called in the Applet used?

Of all these methods, which is executed and in what order? I'm guessing the first question to be asked is what gets started first?

And why does th.start () run run ()?

import java.applet.*;
import java.awt.*;

import javax.swing.JFrame;

public class BallApplet extends Applet implements Runnable {
    int x_pos = 10;
    int y_pos = 100;
    int radius = 20;

    private Image dbImage;
    private Graphics dbG;

    public void init() {
        // setBackground(Color.BLUE);
    }

    public void start() {
        Thread th = new Thread (this);
        th.start();
    }
    public void stop() {}
    public void destroy() {}

    public void run() {
        // 20 second delay per frame refresh (animation doesn't
        // need to be perfectly continuous)     
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

        while (true) {
            x_pos++;
            repaint();
            try {
                Thread.sleep(20);
            }
            catch (InterruptedException ex) {
                System.out.println("Caught!");
            }
            Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
        }
    }
    public void update(Graphics g) {
        // implements double buffering
        // drawing on doublebufferImage, note the dbG=dbImage.getGraphics(), so everything dbG.whatever() is
        //      drawing on the Image graphics which is later drawn with g.drawImage()

        // initialize buffer
        if (dbImage == null) {
            dbImage = createImage (this.getSize().width, this.getSize().height);
            dbG = dbImage.getGraphics();
        }

        // clear screen in background
        dbG.setColor(getBackground());  // gets background color
        dbG.fillRect(0, 0, this.getSize().width, this.getSize().height);

        // draw elements in background
        dbG.setColor(getForeground());
        paint(dbG);

        // draw image on the screen
        g.drawImage(dbImage, 0, 0, this); 
    }
    public void paint(Graphics g) {
        g.setColor(Color.RED);
        g.fillOval(x_pos-radius, y_pos-radius, 2*radius, 2*radius);
    }
}

      

-1


source to share


4 answers


The methods init()

and are run first start()

.

This, in turn, creates Thread

and starts this thread, which invokes the class method run()

.



The method paint()

is called independently of Swing in the GUI event thread if Swing detects that the applet needs to be redrawn.

I note that the main method of the run()

class is also called repeatedly repaint()

. This clearly says what the GUI thread is calling update()

.

+4


source


The browser or applet viewer first calls



  • init () to tell this applet that it has been loaded into the system.
  • then after calling the init start () method. See the Applet class docs for details.
  • Inside the start () method, there is a call to th.start (). This means that start () execution of the thread
  • This will call the run () function
+3


source


From the Life Cycle of the Applet The Java Tutorials , the following methods Applet

are called in order:

  • init()

  • start()

  • stop()

  • destroy()

In addition, the code implements the interface Runnable

, so the method is BallApplet

run()

also executed after the new Thread

(here, called th

) is started by calling the method th.start()

. (A method call Thread.start()

starts a new thread and calls its method run()

.)

The Defining and Starting a Thread section of the Java Guide contains more information about Runnable

and Thread

and how threads are started in Java.

The method run()

contains a call repaint()

, and this application-initiated update calls the method BallApplet

update(Graphics g)

. In addition, the system-called redraw will call the method paint(Graphics g)

.

For more information on overpainting in AWT, see Overpainting in AWT and Swing . For information on painting with the system and application, see the section Starting with the system and Running application .

+3


source


See Thread.start () .

public void start()

      

Causes this thread to begin execution; the Java virtual machine calls a run

method on this topic.

This results in two threads at the same time: the current thread (which returns from the call to the start method) and the other thread (which executes its start method).

You cannot start a thread more than once. Specifically, the thread cannot be restarted when it has finished executing.

0


source







All Articles