Java:Applet 中方法的调用顺序是什么? [英] Java: In what order are the methods called in an Applet?

查看:31
本文介绍了Java: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?

为什么 th.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);
    }
}

推荐答案

首先调用 init()start() 方法.

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

依次创建一个 Thread 并启动该线程,这会导致调用该类的 run() 方法.

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

paint() 方法在 GUI 事件处理线程中由 Swing 独立调用,如果 Swing 检测到小程序需要重绘.

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

我注意到该类的主要 run() 方法也重复调用 repaint().这明确告诉 GUI 线程调用 update().

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

这篇关于Java:Applet 中方法的调用顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆