tags:

views:

455

answers:

4

Of all these methods what's being run and in what order?? I guess the first question to ask is whats being run first?

And why does th.start() start 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's 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);
    }
}
+2  A: 

The browser or Applet viewer first calls

  • init() method to inform this applet that it has been loaded into the system.
  • then after init start() method gets called. For more see the Applet class docs.
  • Inside start() method there is a call to th.start(). That means start() the thread execution
  • That will cause the run() to get invoked
Adeel Ansari
+2  A: 

The init() and start() methods are invoked first.

That in turn creates a Thread and starts that thread, which causes this class's run() method to be invoked.

The paint() method is invoked by Swing independently in the GUI event handling thread, if Swing detects that the applet needs to be redrawn.

I note that the class's main run() method also repeatedly calls repaint(). That explicitly tells the GUI thread to invoke update().

Alnitak
A: 

See Thread.start().

public void start()

Causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.

The result is that two threads are running concurrently: the current thread (which returns from the call to the start method) and the other thread (which executes its run method).

It is never legal to start a thread more than once. In particular, a thread may not be restarted once it has completed execution.

eed3si9n
+2  A: 

From the Life Cycle of an Applet section of The Java Tutorials, the Applet's following methods are called in order:

  1. init()
  2. start()
  3. stop()
  4. destroy()

In addition, the code implements the Runnable interface, so the BallApplet's run() method is also executed after a new Thread (here, called th) is run by calling the th.start() method. (Calling the Thread.start() method starts a new thread and calls its run() method.)

The Defining and Starting a Thread section from The Java Tutorials has more information on Runnable and Thread and how threads are started in Java.

The run() method contains a call to repaint(), and this is an app-triggered update, it will call the BallApplet's update(Graphics g) method. In addition, the system-triggered repaint will trigger the paint(Graphics g) method.

For more information about repainting in AWT, refer to Painting in AWT and Swing. For information on system- and app-triggered painting, see the section on System-Triggered vs. App-Triggered Painting.

coobird