0% found this document useful (0 votes)
79 views9 pages

Basics of Applets: (A. RATNAKAR, 08NT1A0503)

The document discusses the basics of Java applets including how to define a subclass of Applet or JApplet, how applets interact with the browser environment, and the applet lifecycle methods. It provides an example of a simple applet that draws a string and discusses how applets use threads. The document also briefly describes how to create a simple digital clock applet.

Uploaded by

Jack Ratan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views9 pages

Basics of Applets: (A. RATNAKAR, 08NT1A0503)

The document discusses the basics of Java applets including how to define a subclass of Applet or JApplet, how applets interact with the browser environment, and the applet lifecycle methods. It provides an example of a simple applet that draws a string and discusses how applets use threads. The document also briefly describes how to create a simple digital clock applet.

Uploaded by

Jack Ratan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

BASICS OF APPLETS

(A. RATNAKAR, 08NT1A0503)


ABSTRACT Web design has moved to the next level with the construct their graphical user interface. The introduction of java technology in the computing and browsers java plug-in software manages the life programming industry. This introduction has made a cycle of an applet. tremendous impact in the capabilities and Most of todays browsers can run java applets. The functionalities of web pages. Now using not only ones that cannot run are not of much importance HTML codes but java scripts and java applets, more since very few users have such outdated browsers. interactivities and functionalities can be added to To be more precise, applets can be embedded in web pages. This seminar paper looks at what a java pages viewed by netcape2 and newer versions, applet is, how to write applet codes and how to internet explore 3 and newer version, and others. deploy them on web pages. However, some people have turned off the ability to INTRODUCTION This seminar paper discusses the basics of applets, how to deploy them. These applets interact richly with their environment. An applet is a special type of java program that a browser enabled with java technology can download from the internet and run. An applet must be a sub class of the java.applet.Applet class. The applet class provides the standard interface between the applet and the browser environment. Swing provides a special sub class of the Applet class called run applets in their web browsers. These in most cases are companies with more or less paranoid ideas of potential hacking. No matter what their motivation is, the fact is that there are minor people who will not view your applet. This should be taken into consideration before deciding to add applets to your web pages. javax.Swing.JApplet. The JApplet class should be used for all applets that use the swing components to

9 // draw a string at x-coordinate 25 and yDEFINING A SUB CLASS OF AN APPLET Every applet must define a sub class of Applet or JApplet class. Every java applet is a graphical user interface on which you can place graphical user interface (GUI) components. They inherit the significant functionality from the applet or JApplet class including the capability to communicate with the browsers and present a graphical user interface (GUI) to the user. Below is an example of a simple java program. A SIMPLE JAVA APPLET: DRAWING A STRING. 1 Import java.awt.Graphics; // program imports class graphics. 2 Import javax.Swing.JApplet; // imports class JApplet. 3 4 public class DisplayText extends JApplet { 5 // draws text on the background. 6 public void paint (Graphics g) { Line 1 imports graphics to enable the applet to draw graphics such as lines, rectangles, ovals and strings of characters. The class JApplet (imported at line 2) from package javax.Swing is used to create applet. As applications, every java applet contains at least one public class declaration. An applet container can create only objects of classes that are public and extends JApplet (or the applet class from earlier versions of java). For this reason, class DisplayText (line 4-12), extends JApplet. An applet container expects every java applet to have method named init, start, paint, stop and destroy each of which is declared in the class JApplet. These methods can be overridden (refined) to perform tasks that are specific to your applet. When an applet container loads class DisplayText, the container creates an object of type DisplayText coordinate 25. 10 g.drawString (This is my first java

applet, 25, 25); 11 } // end method paint.

12} // end class welcome.

7 // call super class version of the method paint. 8 super.paint (g);

then calls three of the applet methods. In sequence, these three methods are init, start and paint. If you do not declare the methods in your applet, the applet container calls the inherited versions. The super class methods init and start have empty bodies so, they do not perform any task. It is necessary for the applet container to inherit these methods init, start and paint even when some do not make any use of them so that it will have a consistent start up sequence as other applications start execution with the main method. This inheritance ensures that the applet container executes each applet uniformly. Also, inheriting the default implementation of these methods allows the programmer to concentrate on drafting only the methods required for a particular applet. The method paint() was overridden in order to draw the string (line 6-11) by placing statements in the body of the paint that draws a message on the screen. Line 8 calls the super class method paint that was inherited from JApplet. This statement should be the first in any applet paint method, omitting it can cause subtle errors in applets that combine drawing and GUI components. Line 10 uses graphics to draw the string This is my first java applet at x-

coordinate of 25 and y-coordinate of 25 on the drawing area. APPLET LIFE-CYCLE METHODS: An applet class provides a frame work for execution, defining methods that the system call when milestone occur. Milestones are major events in an applet life cycle. Most applets override some or all of these methods to respond appropriately to milestones. These methods are explained below. Other than method paint (), these methods have empty bodies by default. If you want to declare any of these methods and have the applet container call them, you must use the method headers shown below. If you modify the header (e.g. by changing the names or by providing parameters), the applet container will not call your methods instead it will call the super class method inherited from JApplet. public void init () This method is called once by the applet container when an applet is loaded for execution. This method initializes an applet. The action performed her are purely initialization of field, creating GUI components, loading sound to play, loading images

to display and creating threads. Keep your initialization method so short that your applet may load quickly. public void start () Every applet that perform tasks after initialization (except in direct response to user actions.) must override the start method. It is good to practice to return quickly from the start method. If you need to perform computionally intensive operations, it might be better to start a new thread for this purpose. This method is called by the applet container after the initialization method. Action performed might include starting an animation or starting other threads of execution. public void paint (Graphics g) This method is called after init () and start () methods. The method is also called when an applet needs to repainted. Example is if a user minimizes or cover an open page containing applet and uncovers it, the paint method is called. Action performed involves drawing with the graphic object g that is passed to the paint method by the applet container. public void stop ()

Most applets that override the start method should also override the stop method. The stop method should stop the applets execution to save memory and resources when the user is not viewing it. Example, an applet that draws animation should stop drawing it when the user is not viewing it. Typically, action performed here would stop the execution and threads. public void destroy () This method is called by the applet container when the applet is removed from the memory. There is no need to override the destroy method because the stop method which is called before destroy will perform tasks necessary to shut down the applets execution. This occurs when the user exits the session by closing all the browser windows. The method performs any task that is required to release additional resources allocated to the applet.

APPLETS EXECUTION ENVIRONMENT. An applet runs in the context of browser. Java plugin software in the browser controls the loading and execution of the applet. The brower also has the JavaScript interpreter which runs the JavaScript code on a web page. Java plug-in

The java plug-in created a working thread for every applet. It loads the applet in an instance of a java runtime environment (JRE) software. Normally all applets run in the same instance of JRE. The java pug-in software starts a new instance of the JRE in the following cases. 1. When an applet requests to be executed in a specific version of the JRE. 2. When an applet specifies its own JRE start up parameters. For example, the heap size. A new applet uses an existing JRE when its requirements are a subset of the existing JRE otherwise, a new instance is started. Java plug-in and java script interpreter interaction. Applets can invoke JavaScript function present in the web page. JavaScript functions are also allowed to invoke methods of an applet embedded on the same web page. The java plug-in software and the java interpreter calls from JavaScript code to java code and from calls from java code to JavaScript code. The java plug-in software is multithreaded while the java interpreter runs on a single thread. Hence, to avoid thread related issues especially when multi applets are running simultaneously. Keep the calls between java code and JavaScript code short to avoid round trip.

Single application or applets can have many threads doing different things independently. The thread class is defined in Java.lang package. To use threads in your program, you need to define your own local extension of the thread class and overrides its run () method. When an object is controlled by a thread, Its run () method should be invoked by that thread. This will happen automatically when the threads start method is invoked if the object class implements the runnable interface. Accordingly, applets that run animation by means of thread normally implement runnable. A SIMPLE APPLET THAT WILL DISPLAY A DIGITAL CLOCK ON A WEB PAGE. 1. import java.awt.*; 2. import java.text.DateFormat; 3. import java.util.Date; 4. import javax.swing 5. 6. public class clock extends JApplet implement Runnable { 7. 8. 9. 10. 11. 12. 13. public void init() { setSize( 400, 800); panel = new MainPanel(); getContentPane().add(panel); } MainPanel panel; Thread thread;

THE THREAD CLASS A thread is an independent sequential flow of control within a process. The threads run within program.

14. 15.

16. public void run() {

17. while (thread != null ) { 18. 19. 20. 21. 22. 23. 25. 26. public void start() { 27. 28. 29. 30. 31. } 32. 33. public void stop() { 34. 36. } 37. 38. 39. class MainPanel extends Jpanel { 40. DateFormat dateformat = Date.getTimeInstance(DateFormat.DEFAUL T); 41. 42. 43. 44. 45. public void paintComponent(Graphics g); super.paintComponent(g); g.setFont(new Font(Monospaced, Font.BOLD, 50)); g.drawString(Date.toString, 50, 50)); Thread = null; 35. } } if ( thread == null) { Thread= new thread(this); Thread.start(); } 24. } } panel.repaint(); try { Thread.sleep(10); } catch (InterruptedException e) {

46. 47. }

The applet starts execution in line 10 with its init() method. It sets the size of the applet container, instantiates the main panel, and adds it to the applets content pane. Then the applets start() method invoked(automatically by the browser). Since the thread is initially null, this creates a new thread and starts it in line 29. Since the applet class implement runnable, the threads start() method invokes the applets run() method which invokes the panels repaint() method(at line 22) every 10 milliseconds. The JPanel classs repaint() method calls its paint() method which calls its paintComponent() method. This is overridden in MainPanel class at line 42. It calls the JPanel classs paintComponent() method at line 43, sets a big and bold font in line 44, and then finally draws the current time at line 45. TESTING AN APPLET For testing purpose, an applet viewer is most convenient. The java software development kit (java SDK) includes an applet viewer named applet viewer.exe. it is located in the bin directory of the SDK. To run the program example above, first, compile the program to get the clock.class bytecode file, then enter this at the command prompt appletviewer clock.html The applet class should launch and display the applet. You can also use a web browser to test your applet.

The essential codes are lines 6-7. This is an applet DEPLOYMENT OF AN APPLET After the compilation of your applet which can be done in two ways: 1. By using the command prompt: Type your applet code in word editor like note pad and save it with .java. Enter the command prompt and type javac filename.java. the compiler will generate a class file (i.e. filename.class). 2. By using an integrated development environment (IDE): After typing code in a text editor of the IDE, use the compile feature of the IDE to compile the source code to a class format. Design your web page as you like and put the code as below; A WEB PAGE WITH JAVA APPLET DEPLOYED. <html> <title> clock </title> <body> Current Time! <br> <hr> <applet code = clock.class width = 300 height = 60 > </applet> <hr> </body> </html> WHAT APPLET CAN AND CANNOT DO Applets are loaded on a client when the user visits a page containing an applet. The security model behind applet has been designed with the goal of protecting the user from malicious applets. Applets that are not signed using a security certificate are considered to be untrusted and referred to as unsigned applets. When running on a client, unsigned applets operates within a security sandbox that allows only a set of safe operations. Applet can be signed using a security certificate to indicate that it came from trusted source. Signed applets operate outside the security sandbox and have extensive capabilities to access the client. A signed applet will run outside the security sandbox only if the user accepts its security certificate. Below are the security restrictions and capabilities of applets. 1. Unsigned Applets: Unsigned applet can perform the following operations; tag with three attributes: code, width, and height. The values for these three attributes are clock.class, 300 and 60, respectively each is assigned on line 6. This tells the HTML environment (browser or applet viewer) to run the clock.class applet in a from 300 pixels wide and 60 pixel high. Note that the program is enclosed between the <html> tag(line) and </html> tag (line10). The web page has two main part, its title and its body (line 2 and 3-9 respectively)

They can make network connections to the host they came from. They can easily display HTML documents using the showDocument() method of the java.applet.AppletContext class.

Unsigned applet cannot perform the following operations They cannot access client resources such as the local file system, executable file, system clipboard and printers. They cannot connect to or retrieve resources from any third party server (any server other than that of origin). They cannot load native libraries. They cannot change the security manager. They cannot create a class loader. They cannot read certain system properties. SIGNED APPLETS. They do not have the security restrictions imposed on unsigned ones and can run outside the security sandbox. Note: when a signed applet is accessed from JavaScript code in an HTML page, the applet is executed within the sandbox. This implies that signed applet essentially behaves like unsigned applet. DISADVANTAGES OF APPLETS. Before allowing an applet to run, make sure you know and trust the source as it may harm your system because some programmers use it as a harking tool, a means to introduce viruses, and can act as a spyware or adware to your system. It needs a java plug-in on your browser before it can be able to run. ADVANTAGES OF APPLETS. 1. Java applets add interactivity to web pages.

They can invoke public methods of other applets on the same page. Applets that are loaded from the local file system (from a directory in the users CLASSPATH) have none of the restrictions that applets loaded over the network do.

They can read secure system properties. When launched by using java network Launch protocol (JNLP), unsigned applets can also perform the following: o They can open, read, and save files on the client. o They can access the systemwide clipboard. o They can access printing functions. o They can store data in the client; decide how applets should be downloaded and cached, and much more.

2. Allow application to run on web pages like playing games while browsing, chatting on web pages as applets have network connectivity ability. CONCLUSION Having strategically discussed java applets, its development, deployment, advantages and disadvantages, it can be seen that writing java applet codes are very simple to do as well as can add functionality and interactivity to your web pages. RECOMMENDATIONS When designing a web page that you need that some logical applications run on, using java applet is more convenient. Finally, when designing a java applet, bear in mind that there are some fractions of people that will not view your applet, also before allowing an applet to run on your browser, make sure that you know and trust its source or that it is signed.

John .R. Hubbard, Ph.D (2004): Programming with Java Second Edition. The McGraw-Hill Companies, Inc. USA. Lesson: Applet (The Java Tutorials > Deployment). http://java.sun.com/doc/books/tutorial/deployment/a pplet/,

REFERENCES Deitel (2007): Java How to Program Seventh Edition, Pearson Education, Inc. USA.

You might also like