Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit-vi. Concepts of Applets, differences between applets and Applications Life cycle of an applet Types of applets, creating applets, passing parameters.

Similar presentations


Presentation on theme: "Unit-vi. Concepts of Applets, differences between applets and Applications Life cycle of an applet Types of applets, creating applets, passing parameters."— Presentation transcript:

1 Unit-vi

2 Concepts of Applets, differences between applets and Applications Life cycle of an applet Types of applets, creating applets, passing parameters to applets

3 What is an applet? Applet: A small Java program that can be inserted into a web page and run by loading that page in a browser. An applet is a special kind of Java program that is designed to be transmitted over the Internet and automatically executed by a Java- compatible web browser. Applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a web document.

4

5 Applet classes in Java java.lang.Object java.awt.Component java.awt.Container java.awt.Panel java.applet.Applet javax.swing.JApplet

6 How Applets Differ from Applications Although both the Applets and stand-alone applications are Java programs, there are certain restrictions are imposed on Applets due to security concerns: –Applets don’t use the main() method, but when they are loaded, automatically call certain methods (init, start, paint, stop, destroy). –They are embedded inside a web page and executed in browsers. –Takes input through Graphical User Input ( GUI ). –They cannot read from or write to the files on local computer. –They cannot run any programs from the local computer. The above restrictions ensures that an Applet cannot do any damage to the local system.

7 1.Applets can be embedded in HTML pages and downloaded over the Internet whereas Applications have no special support in HTML for embedding or downloading. 2.Applets can only be executed inside a java compatible web browser or appletviewer whereas Applications are executed at command line by java. 3.After an applet arrives on the client, it has limited access to resources on local computer. Applications have no restriction to access resources. 4. Applets don’t have the main() method as in applications. Instead they operate on an entirely different mechanism where they are initialized by init(),started by start(),stopped by stop() or destroyed by destroy(). 5.A Java Applet is made up of at least one public class that has to be subclasses from java.applet.Applet. Whereas, A Java application is made up of a main() method declared as public static void that accepts a string array argument, along with any other classes that main() calls.

8 Life cycle of an Applet Born RunningIdle Dead Begin init() start() paint() stop() start() destroy() End

9 Life cycle of an Applet It is important to understand the order in which these methods are called. When an applet is started, the following sequence of method calls takes place: 1. init( ) 2. start( ) 3. paint( ) When an applet is terminated, the following sequence of method calls takes place: 1. stop( ) 2. destroy( )

10 Applet States Initialisation  The init( ) method is the first method to be called.  This is where you should initialize variables.  This method is called only once during the run time of your applet. Running – more than once  The start( ) method is called after init( ).  It is also called to restart an applet after it has been stopped.  It is called each time an applet’s HTML document is displayed on screen.  So, if a user leaves a web page and comes back, the applet resumes execution at start( ). Display – more than once  paint() happens immediately after the applet enters into the running state. It is responsible for displaying output.  paint( ) is also called when the applet begins execution.  The paint( ) method is called each time your applet’s output must be redrawn.  The paint( ) method has one parameter of type Graphics.

11 Idle  The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page. Dead/Destroyed State – only once  The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory.  At this point, you should free up any resources the applet may be using. The stop( ) method is always called before destroy( ).

12 Building Applet Code: An Example import java.awt.*; import java.applet.Applet; public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString (“A Simple Applet",20, 20); } Begins with two import classes. java.awt.* -- required for GUI java.applet.* -- every applet you create must be a subclass of Applet, which is in java.applet package. The class should start with public, because is accessed from outside.

13 Contd..  Applets do not begin execution at main().  An applet begins its execution when the name of its class is passed to an applet viewer or to a java compatible browser.  Compile the applet in the same way that we have been compiling programs.  Running an applet involves a different process.

14 Running an Applet There are two ways in which you can run an applet:  Executing the applet within a Java-compatible browser.  Using a tool called, appletviewer. An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet.

15 Executing in a web browser. To execute an applet in a web browser, you need to write a short HTML file that contains a tag ( Applet ) that loads the applet. HTML file that contains a SimpleApplet Save the file with.html extension (Example: Simple.html) After you create this file, open your browser and then load this file, which causes SimpleApplet to be executed. width and height specify the dimensions of the display used by the applet.

16 Executing by using appletviewer There are two ways 1. Use earlier html page, which contains applet tag, then execute by using following command. C:\>appletviewer htmlfilename.html 2. Include a comment at the beginning of your source code file that contains the applet tag, then start applet viewer with your java source code file. C:\>appletviewer SimpleApplet.java import java.awt.*; import java.applet.Applet; /* */ public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString (“A Simple Applet",20, 20); }

17 Four of these methods init(), start(), stop(), and destroy() are defined by Applet. Another, paint() is defined by the AWT Component class. Although the above program does not do anything, it can be compiled and run.

18 Structure of an applet // An Applet AppletStructure import java.awt.*; import java.applet.*; /* */ public class AppletStructure extends Applet { // Called first. public void init() { // initialization } /* Called second, after init(). Also called whenever the applet is restarted. */ public void start() { // start or resume execution } // Called when the applet is stopped. public void stop() { // suspends execution } /* Called when applet is terminated. This is the last method executed. */ public void destroy() { // perform shutdown activities } // Called whenever an applet's output must be redisplayed. public void paint(Graphics g) { // redisplay contents of window }

19 Creating Applets /* A simple applet that sets the foreground and background colors and outputs a string. */ import java.awt.*; import java.applet.*; /* */ public class Sample extends Applet{ String msg; public void init() {// set the foreground and background colors. setBackground(Color.cyan); setForeground(Color.red); msg = "Inside init( ) --"; } public void start() { // Initialize the string to be displayed. msg += " Inside start( ) --"; } public void paint(Graphics g) {// Display msg in applet window. msg += " Inside paint( )."; g.drawString(msg, 60, 40); }

20 Passing Parameters to Applet  The APPLET tag in HTML allows you to pass parameters to your applet.  To retrieve a parameter, use the getParameter( ) method.  It returns the value of the specified parameter in the form of a String object.

21 Applet Program Accepting Parameters import java.applet.Applet; import java.awt.*; /* */ public class HelloAppletMsg extends Applet { String msg; public void init(){ msg = getParameter("Greetings"); if( msg == null) msg = "Hello"; } public void paint(Graphics g) { g.drawString (msg,10, 100); } This is name of parameter specified in PARAM tag; This method returns the value of paramter.

22 Types of applets There are two types of applets: First is based on the Applet class  These applets use the AWT to provide the GUI.  This style of applet has been available since java was created.  It is used for simple GUI’s. The second is based on the Swing class JApplet.  These applets use the Swing classes to provide the GUI.  Swing offers a richer and easy to use interface than AWT.  Swing based applets are more popular.

23 Simple Swing Application import java.awt.*; import javax.swing.*; public class myjframe extends JFrame{ myjframe(){ Container contentPane = getContentPane(); JLabel jl=new JLabel("swing more powerful than AWT"); contentPane.add(jl); setSize(250,250); setVisible(true); } public static void main(String arg[]){ new myjframe(); }

24 The methods defined by Applet String getParameter(String paramName) Returns the parameter associated with paramName. null is returned if the specified parameter is not found. void showStatus(String str) Displays str in the status window of the browser or applet viewer. If the browser does not support a status window, then no action takes place. URL getCodeBase( ) Returns the URL associated with the invoking applet. URL getDocumentBase( ) Returns the URL of the HTML document that invokes the applet.

25 The HTML APPLET Tag The syntax for the standard APPLET tag is shown here. Bracketed items are optional. < APPLET [CODEBASE = codebaseURL] CODE = appletFile [ALT = alternateText] [NAME = appletInstanceName] WIDTH = pixels HEIGHT = pixels [ALIGN = alignment] [VSPACE = pixels] [HSPACE = pixels] > [ ]... [HTML Displayed in the absence of Java]

26 void setBackground(Color newColor) void setForeground(Color newColor) Here, newColor specifies the new color. The class Color defines the constants shown here that can be used to specify colors: Color.black Color.magentaColor.blue Color.orange Color.cyan Color.pinkColor.darkGray Color.red Color.gray Color.whiteColor.green Color.yellow Color.lightGray For example, this sets the background color to green and the text color to red: setBackground(Color.green);setForeground(Color.red); A good place to set the foreground and background colors is in the init( ) method. You can obtain the current settings for the background and foreground colors by calling getBackground( ) and getForeground( ), respectively. They are also defined by Component and are shown here: Color getBackground( )Color getForeground( )

27 AudioClip getAudioClip(URL url, String clipName) Returns an AudioClip object that encapsulates the audio clip found at the location specified by url and having the name specified by clipName. Image getImage(URL url,String imageName) Returns an Image object that encapsulates the image found at the location specified by url and having the name specified by imageName. void play(URL url, String clipName) If an audio clip is found at the location specified by url with the name specified by clipName, the clip is played.

28 8). Design the user screen as follows and handle the events appropriately.

29 import java.awt.*; import java.awt.event.*; public class AddWindow extends Frame implements ActionListener{ Label l1,l2,l3; TextField t1,t2,t3; Button b1,b2,cl; AddWindow(){ super("add window"); //setting the size of the window setSize(250,400); l1=new Label("First Number :"); l2=new Label("Second Number:"); l3=new Label("RESULT :"); t1=new TextField(10); t2=new TextField(10); t3=new TextField(10); b1=new Button("add "); b2=new Button("subtract "); cl=new Button("clear"); //setting the layout of the container window setLayout(new FlowLayout());

30 //adding the controls to the container window add(l1); add(t1); add(l2); add(t2); add(l3); add(t3); add(b1); add(b2); add(cl); b1.addActionListener(this); b2.addActionListener(this); cl.addActionListener(this); setVisible(true); } public void actionPerformed(ActionEvent ae){ int n1=Integer.parseInt(t1.getText()); int n2=Integer.parseInt(t2.getText()); if(ae.getSource()==b1) { int add=n1+n2; t3.setText(add+""); } if(ae.getSource()==b2) { int subtract=n1-n2; t3.setText(subtract+""); } if(ae.getSource()==cl) { t1.setText(""); t2.setText(""); t3.setText(""); } }//actionPerformed method public static void main(String args[]) { new AddWindow(); }//main method }//class

31 Handling Mouse Events using Applets // Demonstrate the mouse event handlers. import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class MouseEvents extends Applet implements MouseListener, MouseMotionListener { String msg = ""; int mouseX = 0, mouseY = 0; // coordinates of mouse public void init() { addMouseListener(this); addMouseMotionListener(this); }

32 // Handle mouse clicked. public void mouseClicked(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse clicked."; repaint(); } // Handle mouse entered. public void mouseEntered(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse entered."; repaint(); } // Handle mouse exited. public void mouseExited(MouseEvent me) { // save coordinates mouseX = 0; mouseY = 10; msg = "Mouse exited."; repaint(); }

33 // Handle button pressed. public void mousePressed(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Down"; repaint(); } // Handle button released. public void mouseReleased(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "Up"; repaint(); } // Handle mouse dragged. public void mouseDragged(MouseEvent me) { // save coordinates mouseX = me.getX(); mouseY = me.getY(); msg = "*"; showStatus("Dragging mouse at " + mouseX + ", " + mouseY); repaint(); }

34 // Handle mouse moved. public void mouseMoved(MouseEvent me) { // show status showStatus("Moving mouse at " + me.getX() + ", " + me.getY()); } // Display msg in applet window at current X,Y location. public void paint(Graphics g) { g.drawString(msg, mouseX, mouseY); }

35 /*Week 7 : Write a Java program that works as a simple calculator. Use a grid layout to arrange buttons for the digits and for the +, -,*, % operations. Add a text field to display the result. */

36 import java.awt.event.*; import java.applet.Applet; import java.awt.*; /* */ public class Calc extends Applet implements ActionListener { TextField t;String a;int p=0,tmp=0; Button b0,b1,b2,b3,b4,b5,b6,b7,b8,b9; Button badd,bsub,bmul,bdiv,bper,beql,bc; public void init(){ t = new TextField(50); b0 = new Button("0"); b1 = new Button("1"); b2 = new Button("2"); b3 = new Button("3"); b4 = new Button("4"); b5 = new Button("5"); b6 = new Button("6"); b7 = new Button("7"); b8 = new Button("8"); b9 = new Button("9"); badd = new Button("+"); bsub = new Button("-"); bmul = new Button("*"); bdiv = new Button("/"); bper = new Button("%"); bc = new Button("c"); beql = new Button("="); add(t);add(b0);add(b1); add(b2);add(b3);add(b4); add(b5);add(b6);add(b7); add(b8);add(b9);add(badd); add(bsub);add(bmul);add(bdiv); add(bper);add(bc);add(beql); b0.addActionListener(this); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); b4.addActionListener(this); b5.addActionListener(this); b6.addActionListener(this); b7.addActionListener(this); b8.addActionListener(this); b9.addActionListener(this); badd.addActionListener(this); bsub.addActionListener(this); bmul.addActionListener(this); bdiv.addActionListener(this); bper.addActionListener(this); bc.addActionListener(this); beql.addActionListener(this); setLayout(new GridLayout(4,4)); }

37 public void actionPerformed(ActionEvent ae) { if(ae.getSource()==bc) { t.setText("0"); } if(ae.getSource()==b0) { int k=Integer.parseInt(t.getText()); k=k*10+0; t.setText(String.valueOf(k)); } if(ae.getSource()==b1) { int k=Integer.parseInt(t.getText()); k=k*10+1; t.setText(String.valueOf(k)); } if(ae.getSource()==b2) { int k=Integer.parseInt(t.getText()); k=k*10+2; t.setText(String.valueOf(k)); } if(ae.getSource()==b3){ int k=Integer.parseInt(t.getText()); k=k*10+3; t.setText(String.valueOf(k)); } if(ae.getSource()==b4) { int k=Integer.parseInt(t.getText()); k=k*10+4; t.setText(String.valueOf(k)); }

38 if(ae.getSource()==b5) { int k=Integer.parseInt(t.getText()); k=k*10+5; t.setText(String.valueOf(k)); } if(ae.getSource()==b6) { int k=Integer.parseInt(t.getText()); k=k*10+6; t.setText(String.valueOf(k)); } if(ae.getSource()==b7) { int k=Integer.parseInt(t.getText()); k=k*10+7; t.setText(String.valueOf(k)); } if(ae.getSource()==b8) { int k=Integer.parseInt(t.getText()); k=k*10+8; t.setText(String.valueOf(k)); } if(ae.getSource()==b9) { int k=Integer.parseInt(t.getText()); k=k*10+9; t.setText(String.valueOf(k)); }

39 if(ae.getSource()==badd) { tmp=Integer.parseInt(t.getText()); p=1; t.setText("0"); } if(ae.getSource()==bsub) { tmp=Integer.parseInt(t.getText()); p=2; t.setText("0"); } if(ae.getSource()==bmul) { tmp=Integer.parseInt(t.getText()); p=3; t.setText("0"); } if(ae.getSource()==bdiv) { tmp=Integer.parseInt(t.getText()); p=4; t.setText("0"); } if(ae.getSource()==bper) { tmp=Integer.parseInt(t.getText()); p=5; t.setText("0"); } if(ae.getSource()==beql) { float ewval=Integer.parseInt(t.getText()); float res=0;

40 switch(p) { case 1: res=tmp+newval; break; case 2: res=tmp-newval; break; case 3: res=tmp*newval; break; case 4: res=tmp/newval; break; case 5: res=tmp%newval; break; } t.setText(String.valueOf(res)); }

41 /*Week 6 : b) Develop an applet that receives an integer in one text field, and computes its factorial Value and returns it in another text field, when the button named “Compute” is clicked. */

42 import java.applet.*; import java.awt.*; import java.awt.event.*; /* */ public class FactCompute extends Applet implements ActionListener{ Button btn,clearbtn; Label lbl1,lbl2; TextField tf1,tf2; public void init() { btn=new Button("COMPUTE"); btn.addActionListener(this); clearbtn=new Button("CLEAR"); clearbtn.addActionListener(this); tf1=new TextField(30); tf2=new TextField(30); lbl1=new Label("NUMBER"); lbl2=new Label("RESULT"); setLayout(new GridLayout(3,2)); add(lbl1); add(tf1); add(lbl2); add(tf2); add(btn); add(clearbtn); }

43 public void actionPerformed(ActionEvent e) { if(e.getSource()==btn) { int a=Integer.parseInt(tf1.getText()); int fact=1; for(int i=1;i<=a;i++) fact*=i; tf2.setText(""+fact); } else { tf1.setText(""); tf2.setText(""); }

44 // Demonstrate FlowLayout using applets import java.awt.*; import java.awt.event.*; import java.applet.*; /* */ public class FlowLayoutDemo2 extends Applet implements ActionListener { String msg = ""; Button yes, no, maybe; public void init() { setLayout(new FlowLayout()); yes = new Button("Yes"); no = new Button("No"); maybe = new Button("Undecided"); add(yes); add(no); add(maybe); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); }

45 public void actionPerformed(ActionEvent ae) { String str = ae.getActionCommand(); if(str.equals("Yes")) { msg = "You pressed Yes."; } else if(str.equals("No")) { msg = "You pressed No."; } else { msg = "You pressed Undecided."; } repaint(); } public void paint(Graphics g) { g.drawString(msg, 6, 100); }


Download ppt "Unit-vi. Concepts of Applets, differences between applets and Applications Life cycle of an applet Types of applets, creating applets, passing parameters."

Similar presentations


Ads by Google