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

查看:244
本文介绍了Java的:按照什么顺序的方法调用一个Applet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有这些方法都什么正在运行,以什么顺序占地
我想第一个问题问的是什么被首先运行?

又为何th.start()启动的run()?

 进口java.applet中的*。
进口java.awt中的*。进口javax.swing.JFrame中;公共类BallApplet扩展的Applet实现Runnable {
    INT X_POS = 10;
    INT Y_POS = 100;
    INT半径= 20;    私人图像数据库图像;
    私有图形DBG;    公共无效的init(){
    //的setBackground(Color.BLUE);
    }    公共无效的start(){
    主题日=新主题(本);
    th.start();
    }
    公共无效停止(){}
    公共无效的destroy(){}    公共无效的run(){
    每帧刷新// 20秒的延迟(动画不
    //需要是完全连续的)
    。Thread.currentThread()setPriority(从Thread.MIN_PRIORITY);    而(真){
    X_POS ++;
    重绘();
    尝试{
    视频下载(20);
    }
    赶上(InterruptedException的前){
    的System.out.println(中招!);
    }
    。Thread.currentThread()setPriority(Thread.MAX_PRIORITY);
    }
    }
    公共无效更新(图形G){
    //实现双缓冲
    //借鉴doublebufferImage,注意DBG = dbImage.getGraphics(),所以一切dbG.whatever()是
    //其上后来与g.drawImage绘制图像的图形绘制()    //初始化缓冲区
    如果(数据库图像== NULL){
    数据库图像=的createImage(this.getSize()宽,this.getSize()的高度。);
    DBG = dbImage.getGraphics();
    }    //清屏背景
    dbG.setColor(的getBackground()); //获取背景颜色
    dbG.fillRect(0,0,this.getSize()宽度,this.getSize()高度。);    //绘制背景元素
    dbG.setColor(getForeground());
    油漆(DBG);    //在屏幕上绘制图像
    g.drawImage(数据库图像,0,0,这一点);
    }
    公共无效漆(图形G){
    g.setColor(Color.RED);
    g.fillOval(X_POS半径,Y_POS半径,2 *半径2 *半径);
    }
}


解决方案

的init()开始()方法首先被调用。

这反过来又创造了一个并启动线程,这会导致此类的的run()方法是调用。

的paint()由Swing独立于GUI事件处理线程调用,如果摆动检测小程序需要重新绘制。

我注意到类的主的run()方法也多次呼吁重绘()。这明确地告诉GUI线程调用更新()

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

解决方案

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

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

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