applets in java wiki
A Java applet is a small application which is written in Java and delivered to users in the form of bytecode. The user launches the Java applet from a web page, and the applet is then executed within a Java Virtual Machine (JVM) in a process separate from the web browseritself. A Java applet can appear in a frame of the web page, a new application window, Sun's AppletViewer, or a stand-alone tool for testing applets. Java applets were introduced in the first version of the Java language, which was released in 1995.
Java applets can be written in any programming language that compiles to Java bytecode. They are usually written in Java, but other languages such as Jython,[7] JRuby,[8] Pascal,[9] Scala, or Eiffel (via SmartEiffel)[10] may be used as well.
Java applets run at very fast speeds and, until 2011, they were many times faster than JavaScript.[11] Unlike JavaScript, Java applets had access to 3D hardware acceleration, making them well-suited for non-trivial, computation-intensive visualizations. As browsers have gained support for hardware-accelerated graphics thanks to the canvas technology (or specifically WebGL in the case of 3D graphics),[12][13] as well as just-in-time compiled JavaScript,[14][15] the speed difference has become less noticeable.
Since Java's bytecode is cross-platform (or platform independent), Java applets can be executed by browsers (or other clients) for many platforms, including Microsoft Windows, FreeBSD, Unix, OS X and Linux.
Overview[edit]
The Applets are used to provide interactive features to web applications that cannot be provided by HTML alone.They can capture mouse input and also have controls like buttons or check boxes. In response to user actions, an applet can change the provided graphic content. This makes applets well-suited for demonstration, visualization, and teaching. There are online applet collections for studying various subjects, from physics[16] to heart physiology.[2]
An applet can also be a text area only; providing, for instance, a cross-platform command-line interface to some remote system.[17] 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's dedicated area, so they are less useful for improving the site appearance in general (while applets like news tickers[18] or WYSIWYG editors[19] are also known). Applets can also play media in formats that are not natively supported by the browser.[20]
Pages coded in HTML may embed parameters within them that are passed to the applet. Because of this, the same applet may have a different appearance depending on the parameters that were passed.
As applets were available before CSS and DHTML were standard, they were also widely used for trivial effects such as rollover navigation buttons. Heavily criticized, this usage is now declining.[21]
Technical information[edit]
Java applets are executed in a sandbox by most web browsers, preventing them from accessing local data like the clipboard or file system. The code of the applet is downloaded from a web server, after which 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 which 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 first implementations involved downloading an applet class by class. While classes are small files, there are often many of them, so applets got a reputation as slow-loading components. However, since .jars were introduced, an applet is usually delivered as a single file that has a size similar to an image file (hundreds of kilobytes to several megabytes).
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 one to write code that runs both on current and on future versions of the Java virtual machine.
Similar technologies[edit]
Many Java developers, blogs and magazines are recommending that the Java Web Start technology be used in place of applets.[22][23]Java Web Start allows the launching of unmodified applet code, which then runs in a separate window (not inside the invoking browser).
A Java Servlet is sometimes informally compared to be "like" a server-side applet, but it is different in its language, functions, and in each of the characteristics described here about applets.
Embedding into a web page[edit]
The applet can be displayed on the web page by making use of the deprecated
applet
HTML element,[24] or the recommendedobject
element.[25] The embed
element can be used[26] with Mozilla family browsers (embed
was deprecated in HTML 4 but is included in HTML 5). This specifies the applet's source and location. Both object
and embed
tags can also download and install Java virtual machine (if required) or at least lead to the plugin page. applet
and object
tags also support loading of the serialized applets that start in some particular (rather than initial) state. Tags also specify the message that shows up in place of the applet if the browser cannot run it due to any reason.
However, despite
object
being officially a recommended tag, as of 2010, the support of the object
tag was not yet consistent among browsers and Sun kept recommending the older applet
tag for deploying in multibrowser environments,[27] as it remained the only tag consistently supported by the most popular browsers. To support multiple browsers, the object
tag currently requires JavaScript (that recognizes the browser and adjusts the tag), usage of additional browser-specific tags or delivering adapted output from the server side. Deprecating applet
tag has been criticized.[28] Oracle now provides a maintained JavaScript code[29] to launch applets with cross platform workarounds.Example[edit]
The following example illustrates the use of Java applets through the java.applet package. The example also uses classes from the JavaAbstract Window Toolkit (AWT) to produce the message "Hello, world!" as output.
import java.applet.Applet;
import java.awt.*;
// Applet code for the "Hello, world!" example.
// This should be saved in a file named as "HelloWorld.java".
public class HelloWorld extends Applet {
// Print a message on the screen (x=20, y=10).
public void paint(Graphics g) {
g.drawString("Hello, world!", 20, 10);
// Draws a circle on the screen (x=40, y=30).
g.drawArc(40, 30, 20, 20, 0, 360);
}
}
Simple applets are shared freely on the Internet for customizing applications that support plugins.[30]
After compilation, the resulting .class file can be placed on a web server and invoked within an HTML page by using an <applet> or an<object> tag. For example:
<!DOCTYPE html>
<html>
<head>
<title>HelloWorld_example.html</title>
</head>
<body>
<h1>A Java applet example</h1>
<p>Here it is: <applet code="HelloWorld.class" height="40" width="200">
This is where HelloWorld.class runs.
</applet></p>
</body>
</html>
0 Comment:
Post a Comment