Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.

Similar presentations


Presentation on theme: "Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can."— Presentation transcript:

1 Java Applets:

2 How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can not be executed indep endently like applications, but from inside a Web page (or Appletviewer) using a special feature known as HTML tag Applet can not read from or write to the local computer files. But local applet development is possible. Remote applet requires URL. Applet can not run any program on local machine. Applets are restricted to use the library from the other language like c or c++. Application support this through native method. Many of the restrictions are placed on applet because of security.

3 When to use Applets?: Dynamic and graphical/attractive (multi media) web page display To make available an application on Internet

4 Steps to develop an Applet: Building an applet code(.java file) Creating an executable applet(.class file) Designing a web page with HTML tags Use and tags to incorporate the executable version of applet code Test the code

5 Steps to develop an Applet: Every Java applets inherits a set of default behaviors from the Applet class. As a result, when an applet is loaded. It undergoes a series of changes in its state. The applet states include: 1.Born or Initialization state 2.Running state 3.Idle state 4.Dead or Destroyed state

6 Running start() paint() Display Idle stop() Stopped End Dead Destroyed destroy() Born Begin (Load Applet) Begin (Load Applet) Initialization init ()

7 Applets: Java Programs running in the Java-enabled browser import java.awt.*; import java.applet.*; public class Hello extends Applet{ public void paint(Graphics g){ g.drawString("Hello! Wecome to the applet programming...",10,100); } } Write and compile the above program as Hello.java. Also write an html file as Hello.html as : Open this Hello.html file in your browser.

8 Output:

9 Initialization State: Applets enters in the initialization state when it is loaded. This is achieved by the init() method of the applet class. At this stage, we may do the following: Create objects needed Set up initial values Load images or icons Set up colors etc.

10 Running State: Applets enters in the running state when system calls the start() method. This occurs automatically after applet is initialized. Unlike the init() method, the start method can be called many times. (after idle state) We may override the standard start method.

11 User input output thru Applet: import java.awt.*; import java.applet.*; public class UserIn extends Applet{ TextField text1, text2; public void init(){ text1= new TextField(8); text2= new TextField(8); add(text1); add(text2); text1.setText("0"); text2.setText("0"); }

12 User input output thru Applet: public void paint(Graphics g){ int x=0, y=0,z=0; String s1,s2,s; g.drawString("Input a number in each box ",10,50); try{s1=text1.getText(); x= Integer.parseInt(s1); s2=text2.getText(); y=Integer.parseInt(s2); }catch (Exception e){} z=x+y; s= String.valueOf(z); g.drawString("The Sum is ",10,75); g.drawString(s,100,75); } public boolean action(Event event, Object obj) { repaint(); return true;} }

13 Output:


Download ppt "Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can."

Similar presentations


Ads by Google