Download Applets vs Applications

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Applets
What is an Applet?
 According to Sun “An applet is a small
program that is intended not to be run on its
own, but rather to be embedded inside
another application.”
Applet
 An applet is a java program that executes
using an appletviewer or a java enabled web
browser (such as Internet Explorer).
 The appletviewer is a utility for applets that
is included with the Java SDK.
 The appletviewer or web browser only
executes an applet when a HTML document
containing the applet is opened in the
appletviewer or web browser.
Hello World Applet
import java.applet.*;
import java.awt.*;
public class AppletExample extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World", 50, 25);
}//end paint
}
Basic Applet start
 All applets are started via a HTML file
<APPLET CODE=“AppletExample.class"
WIDTH=200 HEIGHT=50>
</APPLET>
 This identifies the name of the main class
and the portion of the browser window that
should be made available for the applet.
HTML
<html>
<head>
<title>Java Applet</title>
</head>
<body>
This is a HTML file with an embeded applet
<applet code="AppletExample.class" width="150"
height="50"></applet>
</body>
</html>
Running the Applet

There are two ways of executing the Java
applet:
1. Simply double click on the html file which
contains the applet.
2. Within the DOS console, use the
appletviewer command to execute the applet.
E.g. appletviewer Example1.html
OUTPUT
What can an Applet do?
 An applet can:
– Draw pictures on a web page
– Create a new window and draw in it
– Play sounds
 Multimedia, jpegs’s etc.
– Receive input from the user through the
keyboard or the mouse
– Make a network connection to the server from
which it came and can send to and receive
arbitrary data from that server.
Applet Security
 Main concerns with applets is that the
browser downloads code across the
network, possibly from an unknown source
and executes on the client’s system.
 Breeding ground for viruses
 As a security measure, the class loader in
the Java VM checks the bytecode in the
Java class file to make sure they haven’t
been tampered with since compilation
Applet Security: What an applet
cannot do
 Write data on any of the host’s disks.
 Read any data from the hosts disks without the
users permission. In some environments,an applet
cannot read data from the users disks even with
permission.
 Delete files.
 Read from or write to arbitrary blocks of memory,
even on a non-memory protected operating
system like the Mac OS. All memory access is
strictly controlled.
Applet Security: What an applet
cannot do
 Java enabled browsers place security
features on applets.
 IE does not allow applets to access files on
the local disk or communicate with systems
on the network apart from the web server
that provides the HTML page that contains
the applet.
Applet Security: What an applet
cannot do
 An applet provider can sign a Java applet
with a digital signature to verify that it comes
from a trusted source. If an applet is
downloaded by an enabled browser, the
client has to decide whether he/she trusts
the applet provider. If so, the browser may
grant extended powers to the applet.
An applet cannot (continued)
 Introduce a virus or trojan horse into the
host system.
 An applet is not supposed to be able to
crash a host system.
Applets vs Applications
GUI Applets
 Applets are capable of containing swing GUI
components.
 It is possible to convert a previously
constructed Java application into a Java
applet.
Running an Applet class
 The applet class is loaded into the VM
 The VM creates an object from this class
 The VM calls the following methods in the
following sequence:
– init
– start
– paint
Entry point methods
 init – Executed ONLY at the creation of the applet object.
Generally used to setup user interface, variables, etc. (like
a constructor)
 start - executed each time an appletviewer window is
selected as well as at the creation of the object
 paint (Graphics) – Used to draw lines/ shapes on the
appletviewer window. Can be called at any point by calling
‘repaint()’ which wipes the screen and executes paint
again. paint is supplied an object of type graphics which is
contains the screen. paint can use methods in this object to
draw on the screen.
Converting an Application into an
Applet
 Remember that an init method of an applet is
similar to the main method in an application.
 Applications normally are derived from JFrame,
whereas Applets are derived from JApplet.
 Size and Visibility of the JFrame in an application
is set within the application via the setSize and
setVisible methods. The size of an Applet should
be set within the HTML document
 Applets must be declared public.
Example
import Java.awt.*;
import Java.awt.Window.*;
import Java.awt.event.*;
import javax.swing.*;
class MyApplication extends JFrame
{
JButton north = new JButton ("North");
JButton south = new JButton ("South");
JButton east = new JButton ("East");
JButton west = new JButton ("West");
JTextArea textArea = new JTextArea();
MyApplication()
{
setUpGUI();
}
public static void main(String[] args)
{
MyApplication m = new MyApplication();
}
void setUpGUI()
{
Container c = getContentPane();
c.setLayout (new BorderLayout());
c.add (BorderLayout.NORTH, north);
c.add (BorderLayout.SOUTH, south);
c.add (BorderLayout.EAST, East);
c.add (BorderLayout.WEST, West);
c.add(BorderLayout.CENTER "Center",
new JScrollPane(textArea));
setSize (200,200);
setVisible(true);
}
}
Example:Applet
/*HTML code necessary to start the Applet
<HTML>
<BODY>
<APPLET
CODE =
"MyJApplet.class"
WIDTH= "200"
HEIGHT="200">
</APPLET>
</BODY>
</HTML>
*/
Applet
public class MyJApplet extends JApplet
{
JButton north = new JButton ("North");
JButton south = new JButton ("South");
JButton east = new JButton ("East");
JButton west = new JButton ("West");
JTextArea textArea = new JTextArea();
public void init()
{
setUpGUI();
}
void setUpGUI()
{
Container c = getContentPane();
c.setLayout (new BorderLayout());
c.add (BorderLayout.NORTH, north);
c.add (BorderLayout.SOUTH, south);
c.add (BorderLayout.EAST, East);
c.add (BorderLayout.WEST, West);
c.add(BorderLayout.CENTER "Center",
new JScrollPane(textArea));
setSize (200,200);
setVisible(true);
}
}