Java Applet programming - playing with various utilities


Java applet programming is more than just running it. you can do much more with java applets, like parameter passing and running in browser, and much more, learn in this tutorial

In the last article We told you to do some practice and observation with the applet window code we provided, We hope you already practiced that. However in this article we are going to tell you about more on applets and we will also be creating a small animation using Java applet and threads.

If you have not read the introduction article please read it here, The Java Applet - first step to web application programming.

Let us show you the working of the applet we created in last post. In the images below you will see what happens when we do what.

when first you run the applet using applet viewer it will start the applet viewer window and execute the init and start messages and hence prints the message on the console like image shows

applet started

Then we minimized the window of applet to see what happens, now you see that the stop method is called

applet minimized

when we again maximize/restore the window of applet it will again move to start stage.

applet restarted

 

applet destroyed

And finally when we closed the window it will call the stop followed by destroyed method. Hope you now properly understand the working of an applet.

Now its time to show you how an applet looks into the web browser. For this purpose we are going to do little bit of change into our precious applet and of course these changes are much more than nothing. First let us change our original applet file. And because we are going to run this applet on the browser we will not need the applet tag in the Java file so we are going to remove it. instead we are creating the HTML file which will invoke the applet.


<html>
<body>
<applet code="FirstApplet" height="400" width="400"></applet>
</body>
</html>

and the Java file looks like this with the addition of a method paint(Graphics g), which takes Graphics type of argument. The Graphics is found in the java.awt package so we also need to import this package or the class we are using. We need graphics because we want to draw the string on the applet as the browser has not consult with the console in personal.


import java.applet.Applet;
import java.awt.*;

public class FirstApplet extends Applet{
    public void init(){
    }

    public void start(){
    }
   
    public void stop(){
    }

    public void destroy(){
    }

    public void paint(Graphics g){
        g.drawString("Hello World from Applet!", 20, 100);
    }

}

When you will call your html file with the applet tag in it, you will be asked for permission to run this applet, as we told you already that the applet needs permission to be executed as otherwise it may be cause security breach. You might be wondering what is the paint() method all about.

We need paint method to paint something on the applet, we are in this case painting a simple string message, using the method drawString(String message, int x-axis, int y-axis);, this will draw the message on the specified position, where x-axis is the distance from left and -axis is distance from top.

Image below will show the IcedTea web plug-in asking permission to run the applet, and second to this is the applet itself on the browser.

Iced tea plug in

 

applet in browser

Note: the iced tea is Java plug-in for the browser for Linux for windows the jvm will provide the plugin all you need to do is to allow and activate it. Every time you try to run an applet It will ask for permission.

This tutorial was to show you how you can work with browser. From now on we will demonstrate in the applet viewers for easy understanding and debugging.

When you are working with applets it is useful to pass the parameters in the applet, you can accomplish this task by using <param> tag from HTML which is used to pass parameters to the applet. In the following example We will show you how you can pass the values using parameters and how you can retrieve them into your program to use them. Also We will show you how you can show the applet status or some custom message in your status bar of applet.


import java.applet.Applet;
import java.awt.*;

public class FirstApplet extends Applet{
    public void init(){
    }

    public void start(){
       
    }
   
    public void stop(){
    }

    public void destroy(){
    }

    public void paint(Graphics g){
        String msg = getParameter("examsmyantra");
        String tag = getParameter("tagline");
        g.drawString(msg, 20, 100);
        showStatus(tag);
    }

}

/*
<applet code="FirstApplet" width="500" height="500">
    <param name="examsmyantra" value="www.examsmyantra.com" >
    <param name="tagline" value="Study with Awesomeness" >
</applet>
*/

This program on running results following applet:

Parameter applet

If you see there is nothing special in this program but only two extra methods and two extra tags in the comment. the <param> tag is used to pass the parameter, the format is:

<PARAM name="name-of-parameter" value="value-of-parameter" >

when you want to access these parameters in your program you will use the getParameter(String name-of-parameter), and corresponding to the parameter name it will get you the value.

In the program above we have done the same, we passed two parameters and in the program we fetched these two parameters and used them as message to display, however one is done through the drawString and one is shown in the status bar using the showStatus() method.

If you have come through all the content above you would not want to miss this part, this will teach you how to create the animations with applet and threads. The Applet and Animations.