Java:Applet 中的方法调用顺序是什么?
所有这些方法中正在运行什么以及以什么顺序运行? 我想首先要问的问题是首先运行什么?
为什么 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先调用
init()
和start()
方法。这反过来会创建一个
Thread
并启动该线程,这会导致调用此类的run()
方法。如果 Swing 检测到小程序需要重绘,则 Swing 在 GUI 事件处理线程中独立调用
paint()
方法。我注意到该类的主
run()
方法也重复调用repaint()
。 这明确告诉 GUI 线程调用 update()。The
init()
andstart()
methods are invoked first.That in turn creates a
Thread
and starts that thread, which causes this class'srun()
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 callsrepaint()
. That explicitly tells the GUI thread to invokeupdate()
.浏览器或Applet查看器首先调用
The browser or Applet viewer first calls
来自 Applet 的生命周期 部分 < a href="http://java.sun.com/docs/books/tutorial/index.html" rel="nofollow noreferrer">Java 教程,
Applet
的按顺序调用以下方法:init()
start()
stop()
destroy()
此外,该代码实现了
Runnable
接口,因此BallApplet
的run()
方法也会在新的Thread
(此处称为th
)通过调用th.start()
方法来运行。 (调用 Thread.start() 方法启动一个新线程并调用其 run() 方法。) com/docs/books/tutorial/essential/concurrency/runthread.html" rel="nofollow noreferrer">Java 教程中的定义和启动线程部分提供了有关
Runnable
和Thread
以及 Java 中线程是如何启动的。run()
方法包含对repaint()
的调用,这是应用程序触发的更新,它将调用BallApplet
的update(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:init()
start()
stop()
destroy()
In addition, the code implements the
Runnable
interface, so theBallApplet
'srun()
method is also executed after a newThread
(here, calledth
) is run by calling theth.start()
method. (Calling theThread.start()
method starts a new thread and calls itsrun()
method.)The Defining and Starting a Thread section from The Java Tutorials has more information on
Runnable
andThread
and how threads are started in Java.The
run()
method contains a call torepaint()
, and this is an app-triggered update, it will call theBallApplet
'supdate(Graphics g)
method. In addition, the system-triggered repaint will trigger thepaint(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.
请参阅 Thread.start()。
See Thread.start().