Friday, 16 January 2015

Applets in java

Applications are of 2 types. They are standalone applications and distributed applications.


A standalone application is one which runs in the context of local disk and whose result is not
sharable
A distributed application is one which runs in the context of browser or World Wide Web and it
can be accessed across the globe.


An applet is a JAVA program which runs in the context of browser or World Wide Web to deal with applets we must import a package called java.applet.*. This package contains only one class Applet whose fully qualified name is java.applet.Applet.


Applet methods
In java.applet.Applet we have four life cycle methods. They are public void init (), public
void start (), public void stop () and public void destroy ().
1. Public void init (): This is the method which is called by the browser only one time after loading the applet. In this method we write some block of statements which will perform one time operations, such as, obtaining the resources like opening the files, obtaining the database connection, initializing the parameters, etc.
2. Public void start (): After calling the init method, the next method which is from second request to sub-sequent requests the start method only will be called i.e., short method will be called each and every time. In this method we write the block of statement which provides business
logic.
3. Public void stop (): This id the method which is called by the browser when we minimize the window. In this method we write the block of statements which will temporarily releases the resources which are obtained in init method.
4. Public void destroy (): This is the method which will be called by the browser when we close the window button or when we terminate the applet application. In this method we write same block of statements which will releases the resources permanently which are obtained in init method.



import java.applet.*;
import java.awt.*;
/*<applet code="MyApp1" height=300 width=300>
</applet>*/
public class MyApp1 extends Applet
{
String s=" ";
public void init ()
{
setBackground (Color.green);
setForeground (Color.red);
s=s+" INIT ";
}
public void start ()
{
s=s+" START ";
}
public void stop ()
{
s=s+" STOP ";
}
public void destroy ()
{
s=s+" DESTROY ";
}
public void paint (Graphics g)
{
Font f=new Font ("arial", Font.BOLD, 40);
g.setFont (f);
g.drawString (s,100,100);
}
};



awt (abstract windowing toolkit):
In JAVA we can develop to types of GUI (Graphic User Interface) applications. They are standalone GUI applications and distributed GUI applications


A standalone GUI application is one which runs in the context of local disk and our class must extends a predefined class called java.lang.Frame class.


A distributed GUI application is one which runs in context of browser and it must extend java.applet.Applet class






AWT hierarchy




Whenever we develop any GUI application we must have readily available window component and window component must contain frame component.


A Container is a class whose object allows us to add ‘n’ number of similar or different GUI components to make a final application


Except Object class and Applet class all the classes in the above belongs to java.awt.* package


Label class:


Label is a class which is used for creating label as a part of windows application
it is a passive component
Labels always improve the functionality and reality readability of active components.
Label class API:
Data members:
These are called alignment parameters or modifiers
public static final int LEFT (0)
public static final int CENTER (1)
public static final int RIGHT (2)


Constructors:
Label ()
Label (String)
Label (String label name, int alignment modifier)


Instance methods:
public void setText (String);
public String getText ();
public void setAlignment (int);
public int getAlignment ();


Ex:
Label l1=new Label (“ENTER EMPNAME”);
String Label=l1.getText ();
l1.setAlignment (Label.RIGHT);










No comments:

Post a Comment