Pageviews

Sunday 23 June 2013

Applets in Java

by Unknown  |  in Java at  Sunday, June 23, 2013

Applets: Applets are Window-based programs.They are a special type of Java programs that are designed for the transmission over the Internet,Applets can run on Web browsers as well as they can be used for developing Console based applications.From the Applets,we enter into the GUI(Graphical User Interface) environment.

Applets initialization and Termination methods:

1.init(): it is the first method to be called.In init(),our applet will initialize variables and perform other startup activities.

2.start():The Start() method is called after init().It is also called to restart an applet after it has been stopped,start can be called more than once during the life of an applet.

3.paint(): This method is called each time our applet's output must be redrawn

4.stop(); This method is used to suspend any child threads created by the applet and to perform any other activities required to put the applet in safe state.

5. destroy(); This method is called when the applet is no longer required.It is basically used to perform the Shutdown operations required of the applet.

Now we are going to make our first applet,to build your own applet you can try the below code,for executing Applet you must have a Java browser.

WAP in JAVA to print String in Applet

import java.applet.*;
import java.awt.*;
public class Applet1 extends Applet                             //Applet class is inherited
{
public void paint(Graphics g)
{
g.setFont(new Font("VERDANA",2,30));                        //Method to change Font size and type
g.drawString("Coding Garage!!!",150,150);                     //Method to draw String
}
}

As our applet run in browser so we need to provide a HTML file which will make it run in browser.So,here is the HTML code we need to write in our Text editor and save it in .html extension.

<html>
<body>
<applet code="Applet1.class" width="300", height="300">
</applet>
</body>
</html>

How to run Applets in Java?

1.Firstly we need to compile our code,after compiling Filename.class file will be created which we will use in .html file.
2.Secondly,we need to write the following command i.e on the cmd to run our Applet

appletviewer Filename.html

3.You can also directly open Filename.html to open your Applet.



Applets

Here is the output of our first Applet:

Applets

  

0 comments:

Proudly Powered by Blogger.