Handling applet initialization status with event handlers - Java Applet

Java examples for Applet:Applet Creation

Description

Handling applet initialization status with event handlers

<HTML>
<HEAD>
<TITLE>Checking Applet Status</TITLE>
<SCRIPT>
    function determineAppletState() {
        if (sampleApplet.status == 1)  {
            document.getElementById("statediv").innerHTML = "Applet loading ...";
            sampleApplet.onLoad = onLoadHandler;
        } else if (sampleApplet.status == 2) {
            document.getElementById("statediv").innerHTML = "Applet already loaded";
        } else {
            document.getElementById("statediv").innerHTML = "Applet entered error while loading";
        }
    }
 
    function onLoadHandler() {
        document.getElementById("loadeddiv").innerHTML = "Applet has loaded";
    }

</SCRIPT>
</HEAD>
<BODY onload="determineAppletState()">
<DIV ID="statediv">state</DIV>
<DIV ID="loadeddiv"></DIV>
<DIV>
    <SCRIPT src="http://www.java.com/js/deployJava.js"></SCRIPT>
    <SCRIPT>
        var attributes = { id:'sampleApplet', code:'SampleApplet.class', archive:'SampleApplet.jar', width:200, height:100} ;
        var parameters = {java_status_events: 'true'} ;
        deployJava.runApplet(attributes, parameters, '1.7');
    </SCRIPT>
</DIV>
</BODY>
</HTML>

Demo Code

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

public class Main extends Applet {
    BufferedImage image;/* w  w  w  . j a v a 2 s  . c  o m*/
    Graphics2D g2d;
 
    public void init() {
        image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
        g2d = image.createGraphics();
        g2d.fillRect(0, 0, 100, 100); 
        try {
            Thread.sleep(2000);
        } catch (InterruptedException ie) {
            ie.printStackTrace();
        }
    }
    
    public void paint(Graphics g) { 
        g.drawImage(image, 0, 0, this); 
    }
 
}

Related Tutorials