Applet Design





The JAVA is an object oriented and case sensitive language developed under leadership of James Gosling for Sun Microsystems in 1990. In beginning it is called Oak, but its name was changed in 1995 as Java. It was developed to control electronic equipment’s (TVs, VCRs) but it started to control Internet.

It is a full-fledged Object oriented language in which all the transaction revolves around the object. The object is a real world entity it may be person, destination, vehicle, account number, voucher etc. It has ability of inheritance, polymorphism, and encapsulation. It is most popular, platform free, and robust programming language used for developing software.

 

Terminology

Some important terminology related with Applet.

  • Package: The package is a collection of class and methods. In applet it is imported to provide link with class.
  • Class: The class is included in package. The class is a collection of constructors and methods to enhance capability and operability of object.
  • Constructor: The constructors are special types of methods used to create objects. It has same name as class. Suppose, Button is a class and Button () is a constructor of this class.
  • Methods: The every class has constructors and methods to handle objects.

Example:

In java.awt. *, the awt is the name of package which contains class like as Button, Label, TextField, Choice, Menu, Panel, etc.

In C or C++, header files are written to provide functions, just like in Java, classes are imported from specified packages.

Example:

 import java.awt.*;

import. Java.applet.*;

 

Applet

 

Applet is a small program embedded into web pages in the rectangular form and can be displayed by applet viewer of JDK (Java Development Kit) and web browser (IE, Navigator). The java program is written and compiled, class file is produced. The class file is included in the HTML (Hyper Text Markup Language) for web.

 

GUI Design

 

The GUI (Graphical User Interface) design is very creative part of Software design. The Label, TextField, Button, Checkbox, RadioButton, Choice, List, Frame, Panel. Color, Font etc is element of GUI.

(a) Label: The label is used to give written recognition to any object or controls. It has so Label (), Label (String), Label (String, int) constructor used to create label object.

Example:

Label label=new Label (”Science & Future”);

add (label);

 

(b) TextField: It is used to display single row text. The size of text field can be defined with integer numbers. TextField (), TextField (String), TextField (int) are mostly used constructors.

Example:

TextField txt=new TextField(30);

add (txt);

 

(c) Button: The button is also GUI element and it is very important because it is used as a means of communication between user and program. The push type button is available in Button class of AWT package. It has Button (), Button (String) are constructors to create button object. The getLabel (String), setLabel (String), addActionListener (ActionListener) are methods.

Example:

Button button=new Button (”Click Me”);

add (button);

 

(d) Color: The color is used to display colored background and foreground. setColor (), getColor(), setBackground (), setForeground () etc are methods.  The color (int r, int g, int b), Color (float r, float g, float b), color (int rgb) are constructors.

Example:

Color (255, 0, 0), color (0, 255, 0), color (0, 0, 255) are used for red, green and blue color respectively. The black color is denoted by Color (0, 0, 0) and white by color (255, 255, 255)

 

(e) Font: The type face is called font. The Serif, Roman, Courier, SansSerif etc are name of fonts. The size and style easily controlled by programming. Font () is a constructor. getFont(), setFont(Font), and drawstring(String txt, int x, int y) are methods. The style is written as Font. BOLD, Font, ITALIC, and Font. PLAIN

Syntax:

setFont (FontName, Style, Size);

Example:

Font fnt;

fnt = new Font(“Serif”, Font.BOLD | Font.ITALIC, 35);

 

Program:

This program has illustration of all the described components. The program name and Applet class name should be same. In my program, the name of Applet class is Science, so save program as Science. java and compile to form Science class.

import java.awt.event.*;

import java.applet.*;

import java.awt.event.*;

public class Science extends Applet implements ActionListener{

TextFie1d txt;

button btn1, btn2;

public void init()

{

Setforeground (color.blue);

Txt=new TextField(30);

Add(txt);

Btn1=new button (“Click Me”);

Add(btn1);

Btn2=new Button(“Clear text”);

Add(btn2);

Txt.setfont(new Font (“Sserif”, Font.BOLD,25));

Btn1.addActionListener(this);

Btn2.addActionListener(this);

}

Public void actionPerformed(ActionEvent evt)

{

String msg = new String (“AVN (Higher Secondary), Dhangadi”);

If(evt.getsource()==btn1)

{

Txt.settext(msg);

}

Else if(evt.getsource() = = btn2)

{

Txt.settext(“”);}

}

}

 

Applet tags: The Java class is placed between applet tags to view through web browsers. The width and height is written to define size of applet.

<html>

<body><center>

<applet code=science.class

Width=525

Height=80>

</applet></center>

</body>

</html>

 

How compile and view applet?

 

Java third party tools like as Ed for Window, JBuilder, JCreater, JPadPro, Kawa, Sernentic Café etc are available for Java program editing and compiling. These IDE are like as turbo C/C++ editor. You only have to write program, it compiles and displays applet. If IDE is not available, the following process is required.

  • Write your program in notepad and save with java extension in bin folder of JDK if autoexec path is not available for JDK.
  • Compile as C:\JDK1.2\BIN>javac science.java
  • The science.class file is created in BIN folder or sub directory. Create html file using applet tags and class file and save it as science.html (in same folder where class file present).
  •  At the DOS prompt, follow it: C:\JDK1.2\BIN>appletviewer science.html

Java Compiler (javac.exe) converts java source file (file with .java extension) into byte code (file with .class extension). These byte codes are loaded and executed in the JVM (Java Virtual Machine) which is embeddable within web browser. In next issue, you learn about Checkbox, Radio button, Scrollbar, Choice and List of AWT (Abstract Window Tools) package of JAVA.

 

(f) Choice: It is used to make list of data. It has Choice () as constructor. The add (String), additem (String), select (String), addItemListener (ItemListener) like methods.

Example:

Choice mag=new Choice ();

mag.add (“Science & Future”);

mag.add (“Computer Today”);

add (mag);

 

(g) List: The list box contains scrolling list of data. You browse the data and select according to need. List (), List (int) etc are constructors, add(String), additem(String, int) and additem(String), clear(), etc are methods.

Example:

List staff = new List ();

staff.add (”Subash Chandra Poudel”);

staff.add (“Subash Ghimire”);

 

(h) Checkbox and Radio buttons: The checkbox and Radiobuttons are used to receive user choice. Chcckbox(), Checkbox(String), Checkb0x(Group, boolean) are used as constructor.

Example:

Checkbox ch1= new Checkbox (“BCA”);

add (ch1);

 

(i) ScrollBar: It is also GUI element used to scroll up and down value of data. The Scrollbar(int) and Scrollbar(int, int, int, int) are used as constructors.

Scrollbar.VERTICAL is used for vertical orientation. The horizontal setting is default setting otherwise you can use as ScrollBar.HORIZONTAL.

Example:

Scrollbar sbar new Scrollbar (Scrollbar.VERT ICAL, 1, 10, 1,100);

add(sbar);

 

Program:

The following program should be saved as StaffDemo.java because it is a class. After compilation, it should be placed in applet tag of html document.

import java.awt.*;

import java.applet.*;

public class staffdemo extends Applet{

public void inti()

{

Choice staff=new choice();

Staff.add(“principal: Dabal Bdr bist”);

Staff.add(“mathematics:Gulab Chand Sharma”);

Staff.add(“mathematics:naresh pant”);

Add(staff);

}

}

 

Web Document

 

<!—StaffDemo.html..!>

<html>

<body>

<center>

<applet code=staffdemo.class width=240 height=80>

</center></applet>

</body>

</html>

 

Program:

This program illustrates concept of List class. You should save it as editdemo.java before compilation.

Import java.awt.*;

Import java.applet.*;

Public class editdemo extends Applet{

Public void init()

{

List staff= new list();

Staff.add(“Mr.subash Chandra poudel’);

Staff.add(“Mr. Ran badhur B.K.”);

Staff.add(“Mr. sandeep gautam”);

Staff.add(“Mr.Subash Ghimire”);

Add(staff);

}

}

 

Program:

This program is designed to handle check box. Save this program as CheckDemo.java and compile.

Import java.awt.*;

Import java.applet.*;

Public class checkdemo extends Applet{

Public void init()

{

Checkbox ch1 = new checkbox (“Science and future”);

Add(ch1);

Checkbox ch2= new checkbox (“computer today”);

Add(ch2);

}

}

 

Program:

 

This program is designed to illustrate concept of radio button. Save program as RadioDemp.java and compile.

Import java.awt.*;

Import java.applet.*;

Public class radioDemo extends Applet{

Public void init()

{

Checkboxgroup radiobtn=new checkboxgroup();

Chebox male=new checkbox(“male”, radibtn, true);

Add(male);

Checkbox female=new checkbox(“female”, radiobtn, false);

Add(female);

}

}

 

Program:

 

The scrollDemo.java illustrates Scrollbar objects. The setforground() method is used to provide forground color and setfont() method supports font type, style and size of displayed text.

Import java.awt.*;

Import java.applet.*;

Public class scrollDemo extends Applet{

Label label;

Scrollbar sbar1, sbar2;

Public void init()

{

Label=new label(“Horizontal and vertical scrollbar:”);

Label.setfont(new font(“Roman”, font.BOLD, 16);

Label.setforeground(color.blue);

Add(label);

Sbar1=new scrollbar(scrollbar.HORIZONTAL, 1, 10, 1, 100);

Add(sbar1);

Sbar2=new scrollbar (scrollbar.VERTICAL, 1, 10, 1, 100);

Add(sbar2);

}

}

In the next lesson, we will illustrate the concept of Layout manager, Menu, Panel, and Frames which make frame work of software.

 

(j) Choice:  it is used to make list of data. It has choice () as constructor. The add(string), additem(string), select(string), additemlistener(itemlistener) like methods.

Example:

Choice mag = new choice ();

Mag.add(“Science and Future”);

Mag.add(“computer Today”);

Add(mag);

 

(k) List:  The list box contains scrolling list of data. You brouse the data and select according to need. List (), list(int) etc are constructors, add(string), additem(string, int) and additem(string), clear() etc are methods.

Example:

List staff=new list();

Staff.add(“Subash Chandra poudel”);

Staff.add(subash ghimire”);

 

(l) Checkbox and radiobuttons: The checkbox and radiobuttons are used to receive user choice. Checkbox(), checkbox(string), checkbox(Group, Boolean) are used as constructor.

Example:

Checkbox ch1=new checkbox (“BCA”);

Add(ch1);

 

Program:

 

This program illustrates concept of list class. You should save it as EditDemo.java before compilation.

Import java.awt.*;

Import java.applet.*;

Public class editdemo extends Applet{

Public void init()

{

List staff=new list();

Staff.add(“subash Chandra poudel”);

Staff.add(“Ran B.K.”);

Staff.add(“sandeep gautam”);

Staff.add(“subash ghimire”);

Add(staff);

}

}

 

Program:

 

This program is designed to handle check box. Save this program as CheckDemo.java and compile.

Import java.awt.8;

Import java.applet.*;

Public class CheckDemo extends Applet{

Public void init()

{

Checkbox ch1=new checkbox(“Science and Future”);

Add(ch1);

Checkbox ch2= new checkbox (“Computer today”);

Add(ch2);

}

}

 

Program:

 

The scrollDemo.java illustrates scroll objects. The setForeground() method is used to provide foreground color and setfont() method supports font type, style and size of displayed text.

Import java.awt.*;

Import java.applet.*;

Public class scrolldemo extends applet{

Label lable;

Scrollbar sbar1, sbar2;

Public void init()

{

Label=new label(“Horizontal and vertical scrollbar”);

Label.setfont(new font(“Roman”, Font.Bold,16);

Label.setforeground(color.blue);

Add(label);

Sbar1=new scrollbar(scrollbar.HORIZONATL, 1, 10, 1, 100);

Add(sbar1);

Sbar2=new Scrollbar ( Scrollbar.VERTICAL, 1, 10, 1, 100);

Add(sbar2);

}

}



Related posts:

  1. C++ Functions C++ Functions A function is meant for performing a specified...
  2. System Analysis and Design 1. System The term system is derived from Greek word...
  3. C++ Program To Display Content of Array This program is used to initialize a set of string...
  4. Defining Member Function In C++ Class methods definitions  (Defining the member functions) Member functions can...
  5. C++ Program to Input and Output String This is a simple program which is designed  to take...