Java:Applet 中的方法调用顺序是什么?

发布于 2024-07-10 12:21:09 字数 2094 浏览 7 评论 0原文

所有这些方法中正在运行什么以及以什么顺序运行? 我想首先要问的问题是首先运行什么?

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

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);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

且行且努力 2024-07-17 12:21:09

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

这反过来会创建一个Thread并启动该线程,这会导致调用此类的run()方法。

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

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

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

把人绕傻吧 2024-07-17 12:21:09

浏览器或Applet查看器首先调用

  • init()方法来通知该Applet它已经加载到系统中。
  • 然后在 init start() 方法被调用之后。 有关更多信息,请参阅 Applet 类文档。
  • start() 方法内部调用了 th.start()。 这意味着 start() 线程执行
  • 将导致 run() 被调用

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
眸中客 2024-07-17 12:21:09

来自 Applet 的生命周期 部分 < a href="http://java.sun.com/docs/books/tutorial/index.html" rel="nofollow noreferrer">Java 教程,Applet 的按顺序调用以下方法:

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

此外,该代码实现了 Runnable 接口,因此 BallAppletrun() 方法也会在新的 Thread(此处称为 th)通过调用th.start()方法来运行。 (调用 Thread.start() 方法启动一个新线程并调用其 run() 方法。

) com/docs/books/tutorial/essential/concurrency/runthread.html" rel="nofollow noreferrer">Java 教程中的定义和启动线程部分提供了有关 RunnableThread 以及 Java 中线程是如何启动的。

run() 方法包含对 repaint() 的调用,这是应用程序触发的更新,它将调用 BallAppletupdate(Graphics g) 方法。 另外,系统触发的重绘会触发paint(Graphics g)方法。

有关在 AWT 中重新绘制的详细信息,请参阅 在 AWT 中绘制和摇摆。 有关系统和应用程序触发的绘画的信息,请参阅 系统触发与应用程序触发绘画

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.

别挽留 2024-07-17 12:21:09

请参阅 Thread.start()。

public void start()

使该线程开始执行;
Java 虚拟机调用 run
该线程的方法。

结果是两个线程
并发运行:当前
线程(从调用返回
启动方法)和另一个线程
(执行其 run 方法)。

启动线程永远是不合法的
不止一次。 特别是,一个
线程一旦启动就无法重新启动
已完成执行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文