Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
167 views
in Technique[技术] by (71.8m points)

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

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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...