220 likes | 330 Vues
Applets. An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page.
E N D
Applets • An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included in a page. • When you use a Java technology-enabled browser to view a page that contains an applet, the applet's code is transferred to your system and executed by the browser's Java Virtual Machine • By calling certain methods, a browser manages an applet life cycle, if an applet is loaded in a web page.
Appletsare small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a Web document • Applet has limited access to the client machine’s resources • The limited access can produce an arbitrary multimedia user interface and run complex computations without introducing the risk of viruses or breaching data integrity.
Applets Cont.. • Applets are used to provide interactive features to web applications that cannot be provided by HTML alone. • They can capture mouse input (like rotating 3D object) and also have controls like buttons or check boxes. • In response to the user action an applet can change the provided graphic content. This makes applets well suitable for demonstration, visualization and teaching. • Applets are also used to create online game collections that allow players to compete against live opponents in real-time.
An applet can also be a text area only. • If needed, an applet can leave the dedicated area and run as a separate window. However, applets have very little control over web page content outside the applet dedicated area, so they are less useful for improving the site appearance in general • Applets can also play media in formats that are not natively supported by the browser.. • Java applets run at a speed that is comparable to (but generally slower than) other compiled languages such as C++, but many times faster than JavaScript. • In addition they can use 3D hardware acceleration that is available from Java. This makes applets well suited for non trivial, computation intensive visualizations
HTML pages may embed parameters that are passed to the applet. Hence the same applet may appear differently depending on the parameters that were passed. • The first implementations involved downloading an applet class by class. While classes are small files, there are frequently a lot of them, so applets got a reputation as slow loading components. • Since jars were introduced, an applet is usually delivered as a single file that has a size of the bigger image (hundreds of kilobytes to several megabytes). • Since Java's bytecode is platform independent, Java applets can be executed by browsers for many platforms, including Microsoft Windows, Unix, Mac OSand Linux. • It is also trivial to run a Java applet as an application with very little extra code. This has the advantage of running a Java applet in offline mode without the need for any Internet browser software and also directly from the development IDE. • Many Java developers, blogs and magazines are recommending that the Java Web Start technology be used in place of Applet
Technical Information • Java applets are executed in a sandbox by most web browsers, preventing them from accessing local data like clipboard or file system. • The code of the applet is downloaded from a web server and the browser either embeds the applet into a web page or opens a new window showing the applet's user interface. • A Java applet extends the class java.applet.Applet, or in the case of a Swing applet, javax.swing.JApplet. • The class must override methods from the applet class to set up a user interface inside itself (Applet is a descendant of Panel which is a descendant of Container. As applet inherits from container, it has largely the same user interface possibilities as an ordinary Java application, including regions with user specific visualization. • The domain from where the applet executable has been downloaded is the only domain to which the usual (unsigned) applet is allowed to communicate. This domain can be different from the domain where the surrounding HTML document is hosted. • Java system libraries and runtimes are backwards compatible, allowing to write code that runs both on current and on future versions of the Java virtual machine.
Sample Applet import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet { public void paint(Graphics g) { g.drawString("A Simple Applet", 20, 20); } } Ref:SimpleApplet.java • The first imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user through the AWT, not through the console-based I/O classes. • The AWT contains support for a window-based, graphical interface. • The second importstatement imports the appletpackage, which contains the class Applet. • Every applet that create must be a subclass of Applet.
The class SimpleAppletmustbedeclared as public, because it will be accessed by code that is outside the program. • Inside SimpleApplet, paint()is declared. This method is defined by the AWT and must be overridden by the applet. paint( ) is called each time that the applet must redisplay its output. • Redisplay can happen several reasons, the window in which the applet is running can be overwritten by another window and then uncovered. Or, the applet window can be minimized and then restored. • paint( ) is also called when the applet begins execution. Whatever the cause, whenever the applet must redraw its output, paint( ) is called.
The paint()method has one parameter of type Graphics. This parameter contains the graphics context, which describes the graphics environment in which the applet is running. This context is used whenever output to the applet is required. • Inside paint()is a call to drawString(), which is a member of the Graphicsclass. This method outputs a string beginning at the specified X,Y location. It has the following general form: • void drawString(String message, int x, int y) • messageis the string to be output beginning at x,y. • In a Java window, the upperleft corner is location 0,0. The call to drawString( ) in the applet causes the message "A Simple Applet" to be displayed beginning at location 20,20.
Applet Execution • There are two ways to run the applet • Executing the applet within a Java-compatible Web browser, such as Netscape Navigator. • Using an applet viewer, such as the standard JDK tool, appletviewer. • An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet.
Executing in browser • Compile the Appletclass • Create an html file with the following applet tag <applet code="com.mes.training.applets.SimpleApplet" width=200 height=60> </applet> • The width and height statements specify the dimensions of the display area used by the applet. • If the applet class is within in package, put the html file in the root directory and give fully qualified name • After creating this file, execute browser and then load this file,which causes SimpleAppletto be executed.
Applet viewer • To execute SimpleApplet with an applet viewer, execute the HTML file in the using appletviewer command • For example, if the preceding HTML file is called RunApp.html, then the following command line will run SimpleApplet: • C:\\>appletviewer RunApp.html • Include a comment at the head of Java source code file that contains the APPLET tag. By doing so, code is documented with a prototype of the necessary HTML statements, and we can test the compiled applet merely by starting the applet viewer with your Java source code file. • Ref:SimpleApplet.java • applets.html
Applets are….. • Applets do not need a main()method. • Applets must be run under an applet viewer or a Java-compatible browser. • User I/O is not accomplished with Java's stream I/O classes. Instead, applets use the interface provided by the AWT.
Applet Class • All applet classes need to extend Applet class • Applet class provides all necessary support for applet execution, such as starting and stopping. • It also provides methods that load and display images, and methods that load and play audio clips. • Applet extends the AWT class Panel. In turn, Panelextends Container, which extends Component.
Applet Architecture • An applet is a window-based program and they are event driven • When the user clicks a mouse inside the applet's window, a mouse-clicked event is generated. If the user presses a key while the applet's window has input focus, a keypress event is generated. As you will see in later chapters, applets can contain various controls, such as push buttons and check boxes. When the user interacts with one of these controls, an event is generated.
Life Cycle • Basically, there are four methods in the Applet class on which any applet is built. • init: This method is intended for whatever initialization is needed for your applet. It is called after the param attributes of the applet tag. • start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages. • stop: This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation. • destroy: This method is only called when the browser shuts down normally. • The applet can be initialized once and only once, started and stopped one or more times in its life, and destroyed once and only once. • Another, paint(), is defined by the AWT Componentclass.
Applet skeleton import java.awt.*; import java.applet.*; /* <applet code="AppletSkel" width=300 height=100> </applet> */ public class AppletSkel 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 }
Applet skeleton….. // 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 when an applet's window must be restored. public void paint(Graphics g) { // redisplay contents of window } }
Advantages • It is simple to make it work on Linux, Microsoft Windows and Mac OS X i.e. to make it cross platform. Applets are supported by most web browsers. • The same applet can work on "all" installed versions of Java at the same time, rather than just the latest plug-in version only. However, if an applet requires a later version of the Java Runtime Environment (JRE) the client will be forced to wait during the large download. • Most web browsers cache applets, so will be quick to load when returning to a web page. Applets also improve with use: after a first applet is run, the JVM is already running and starts quickly (the JVM will need to restart each time the browser starts afresh).
Advantage… • Applet can move the work from the server to the client, making a web solution more scalable with the number of users/clients. • If a standalone program (like Google Earth) talks to a web server, that server normally needs to support all previous versions in case a user has not kept his or her client software up to date. In contrast, a properly configured browser loads (and caches) the latest applet version, so there is no need to support legacy versions. • The applet naturally supports the changing user state, such as figure positions on the chessboard. • Developers can develop and debug an applet direct simply by creating a main routine (either in the applet's class or in a separate class) and calling init() and start() on the applet, thus allowing for development in their favorite Java SE development environment. All one has to do after that is re-test the applet in the AppletViewer program or a web browser to ensure it conforms to security restrictions. • An untrusted applet has no access to the local machine and can only access the server it came from. This makes such an applet much safer to run than a standalone executable that it could replace. However, a signed applet can have full access to the machine it is running on if the user agrees. • Java applets are fast - and can even have similar performance to native installed software.
Disadvantages • It requires the Java plug-in. • Some organizations only allow software installed by the administrators. As a result, some users can only view applets that are important enough to justify contacting the administrator to request installation of the Java plug-in. • As with any client-side scripting, security restrictions may make it difficult or even impossible for an untrusted applet to achieve the desired goals. • Some applets require a specific JRE. This is discouraged.[25] • If an applet requires a newer JRE than available on the system, or a specific JRE, the user running it the first time will need to wait for the large JRE download to complete. • Java automatic installation or update may fail if a proxy server is used to access the web. This makes applets with specific requirements impossible to run unless Java is manually updated. The Java automatic updater that is part of a Java installation also may be complex to configure if it must work through a proxy. • Unlike the older applet tag, the object tag needs workarounds to write a cross-browser HTML document.
Ref:SampleApplet1.java • SimpleBannerApplet.java • PraDemo.java • Read Html tags