1 CHAPTER 2 JAVA APPLET Java Programming ITec3056
What is Java Applet? 2  Applet is a special type of program that is embedded in the webpage to generate the dynamic content.  It runs inside the browser and works at client side. Advantage of Applet • It works at client side so less response time. • Secured • It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc. Drawback of Applet • Plugin is required at client browser to execute applet.
Hierarchy of Applet 3  As displayed in the diagram, Applet class extends Panel.  Panel class extends Container which is the subclass of Component.
Lifecycle of Java Applet 4 1. Applet is initialized. 2. Applet is started. 3. Applet is painted. 4. Applet is stopped. 5. Applet is destroyed.
Lifecycle methods for Applet: 5  The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet. java.applet.Applet class  For creating any applet java.applet.Applet class must be inherited.  It provides 4 life cycle methods of applet. 1. public void init(): is used to initialized the Applet. It is invoked only once. 2. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. 3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. 4. public void destroy(): is used to destroy the Applet. It is invoked only
Lifecycle methods for Applet: 6 java.awt.Component class  The Component class provides 1 life cycle method of applet. 1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc. Who is responsible to manage the life cycle of an applet?  Java Plug-in software. How to run an Applet?  There are two ways to run an applet 1. By html file. 2. By appletViewer tool (for testing purpose).
Simple example of Applet by html file: 7  To execute the applet by html file, create an applet and compile it.  After that create an html file and place the applet code in html file.  Now click the html file. 1. //First.java 2. import java.applet.Applet; 3. import java.awt.Graphics; 4. public class First extends Applet{ 5. public void paint(Graphics g){ 6. g.drawString("welcome",150,150); 7. } 8. 9. }
Simple example of Applet by html file: 8 myapplet.html 1. <html> 2. <body> 3. <applet code="First.class" width="300" height="300"> 4. </applet> 5. </body> 6. </html>
Example of Applet by appletviewer tool: 9  To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it.  After that run it by: appletviewer First.java.  Now Html file is not required but it is for testing purpose only. 1. //First.java 2. import java.applet.Applet; 3. import java.awt.Graphics; 4. public class First extends Applet{ 5. public void paint(Graphics g){ 6. g.drawString("welcome to applet",150,150); 7. } 8. }
Example of Applet by appletviewer tool: 10  To execute the applet by appletviewer tool, write in command prompt:
JApplet class in Applet 11  As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing.  The JApplet class extends the Applet class. Example of EventHandling in JApplet: 1. import java.applet.*; 2. import javax.swing.*; 3. import java.awt.event.*; 4. public class EventJApplet extends JApplet implements ActionListen er{ 5. JButton b; 6. JTextField tf; 7. public void init(){
Example of EventHandling in JApplet: 12 8. tf=new JTextField(); 9. tf.setBounds(30,40,150,20); 10. b=new JButton("Click"); 11. b.setBounds(80,150,70,40); 12. add(b);add(tf); 13. b.addActionListener(this); 14. setLayout(null); 15. } 16. public void actionPerformed(ActionEvent e){ 17. tf.setText("Welcome"); 18. } 19. }
The difference between Java Application and Applet 13 Applet Definition program that can be executed web browser. Applets are small Java programs that are web browser for execution. method is required. Compilation compile application programs, “java” command. Applet programs are compiled with the “appletviewer” command or the web browser. and network.
The difference between Java Application and Applet 14 Access level Applications can access all kinds of resources available on the system. Applets can only access browser-specific services. They don’t have access to the local system. Installation First and foremost, the installation of a Java application on the local computer is required. The Java applet does not need to be installed beforehand. Execution Applications can execute the programs from the local system. Applets cannot execute programs from the local machine. Program An application program is needed to perform some tasks directly for the user. An applet program is needed to perform small tasks or part of them.
The difference between Java Application and Applet 15 Run It cannot run on its own; it needs JRE to execute. It cannot start on its own, but it can be executed using a Java-enabled web browser. Connection with servers Connectivity with other servers is possible. It is unable to connect to other servers. Read and Write Operation It supports the reading and writing of files on the local computer. It does not support the reading and writing of files on the local computer. Security Application can access the system’s data and resources without any security limitations. Executed in a more restricted environment with tighter security. They can only use services that are exclusive to their browser. Restrictions Java applications are self-contained and require no additional security because they are trusted. Applet programs cannot run on their own, necessitating the maximum level of security.
16  Draw Ethiopian National Flag using Java Applet. 1

Advanced programming chapter 2 - Java Applet.pdf

  • 1.
    1 CHAPTER 2 JAVA APPLET JavaProgramming ITec3056
  • 2.
    What is JavaApplet? 2  Applet is a special type of program that is embedded in the webpage to generate the dynamic content.  It runs inside the browser and works at client side. Advantage of Applet • It works at client side so less response time. • Secured • It can be executed by browsers running under many platforms, including Linux, Windows, Mac Os etc. Drawback of Applet • Plugin is required at client browser to execute applet.
  • 3.
    Hierarchy of Applet 3 As displayed in the diagram, Applet class extends Panel.  Panel class extends Container which is the subclass of Component.
  • 4.
    Lifecycle of JavaApplet 4 1. Applet is initialized. 2. Applet is started. 3. Applet is painted. 4. Applet is stopped. 5. Applet is destroyed.
  • 5.
    Lifecycle methods forApplet: 5  The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet. java.applet.Applet class  For creating any applet java.applet.Applet class must be inherited.  It provides 4 life cycle methods of applet. 1. public void init(): is used to initialized the Applet. It is invoked only once. 2. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. 3. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. 4. public void destroy(): is used to destroy the Applet. It is invoked only
  • 6.
    Lifecycle methods forApplet: 6 java.awt.Component class  The Component class provides 1 life cycle method of applet. 1. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc. Who is responsible to manage the life cycle of an applet?  Java Plug-in software. How to run an Applet?  There are two ways to run an applet 1. By html file. 2. By appletViewer tool (for testing purpose).
  • 7.
    Simple example ofApplet by html file: 7  To execute the applet by html file, create an applet and compile it.  After that create an html file and place the applet code in html file.  Now click the html file. 1. //First.java 2. import java.applet.Applet; 3. import java.awt.Graphics; 4. public class First extends Applet{ 5. public void paint(Graphics g){ 6. g.drawString("welcome",150,150); 7. } 8. 9. }
  • 8.
    Simple example ofApplet by html file: 8 myapplet.html 1. <html> 2. <body> 3. <applet code="First.class" width="300" height="300"> 4. </applet> 5. </body> 6. </html>
  • 9.
    Example of Appletby appletviewer tool: 9  To execute the applet by appletviewer tool, create an applet that contains applet tag in comment and compile it.  After that run it by: appletviewer First.java.  Now Html file is not required but it is for testing purpose only. 1. //First.java 2. import java.applet.Applet; 3. import java.awt.Graphics; 4. public class First extends Applet{ 5. public void paint(Graphics g){ 6. g.drawString("welcome to applet",150,150); 7. } 8. }
  • 10.
    Example of Appletby appletviewer tool: 10  To execute the applet by appletviewer tool, write in command prompt:
  • 11.
    JApplet class inApplet 11  As we prefer Swing to AWT. Now we can use JApplet that can have all the controls of swing.  The JApplet class extends the Applet class. Example of EventHandling in JApplet: 1. import java.applet.*; 2. import javax.swing.*; 3. import java.awt.event.*; 4. public class EventJApplet extends JApplet implements ActionListen er{ 5. JButton b; 6. JTextField tf; 7. public void init(){
  • 12.
    Example of EventHandlingin JApplet: 12 8. tf=new JTextField(); 9. tf.setBounds(30,40,150,20); 10. b=new JButton("Click"); 11. b.setBounds(80,150,70,40); 12. add(b);add(tf); 13. b.addActionListener(this); 14. setLayout(null); 15. } 16. public void actionPerformed(ActionEvent e){ 17. tf.setText("Welcome"); 18. } 19. }
  • 13.
    The difference betweenJava Application and Applet 13 Applet Definition program that can be executed web browser. Applets are small Java programs that are web browser for execution. method is required. Compilation compile application programs, “java” command. Applet programs are compiled with the “appletviewer” command or the web browser. and network.
  • 14.
    The difference betweenJava Application and Applet 14 Access level Applications can access all kinds of resources available on the system. Applets can only access browser-specific services. They don’t have access to the local system. Installation First and foremost, the installation of a Java application on the local computer is required. The Java applet does not need to be installed beforehand. Execution Applications can execute the programs from the local system. Applets cannot execute programs from the local machine. Program An application program is needed to perform some tasks directly for the user. An applet program is needed to perform small tasks or part of them.
  • 15.
    The difference betweenJava Application and Applet 15 Run It cannot run on its own; it needs JRE to execute. It cannot start on its own, but it can be executed using a Java-enabled web browser. Connection with servers Connectivity with other servers is possible. It is unable to connect to other servers. Read and Write Operation It supports the reading and writing of files on the local computer. It does not support the reading and writing of files on the local computer. Security Application can access the system’s data and resources without any security limitations. Executed in a more restricted environment with tighter security. They can only use services that are exclusive to their browser. Restrictions Java applications are self-contained and require no additional security because they are trusted. Applet programs cannot run on their own, necessitating the maximum level of security.
  • 16.
    16  Draw EthiopianNational Flag using Java Applet. 1