# Java Review: Applets

- Purpose: Program that runs in a web page.
- How is this possible?
  - Machines have different machine instruction sets (Power P/C, Intel x86, Motorola 68000, Sparc, ...).
  - Clicking on a web page and having it run an arbitrary program is a potential security problem.

# Applets and Java Byte Code, Sandbox

- Applets are distributed as Java Byte Code, so the Interpretation or JIT compilation happens on the local machine.
- Applets are verified as part of the class loading mechanism.
- Applets (unlike applications) run in a "sandbox", which restricts what they can do. Eg, they can't read or write your files (without your explicit permission).

# Applets - How useful?

- Applets were overhyped.
- Applets depend on Java (JRE - Java Runtime Environment) being installed.
- Applets depend on the version of the JRE.
- Applets have to be loaded each time.
- Why textbook authors (over)use applets
  - Early enthusiasm.
  - Slightly easier to do graphics (don't have to build a window).
  - Usually "cheat" by using `appletviewer`.
- Java Web Start is more like applets should have been implemented.
  - The are written like an application.
  - Similar sandbox concept.
  - Appears as a standard application on your desktop.
  - When run, it will automatically check for updates.
  - If offline, it will still run.

# Java Web Start - Alternative to Applets

- Java Web Start is more like applets should have been implemented.
- The are written like an application.
- Similar sandbox concept.
- Appears as a standard application on your desktop.
- When run, it will automatically check for updates.
- If offline, it will still run.

# Applets - Basic Structure

- Class extends `java.awt.Applet` or `javax.swing.JApplet`
- Applet is initialized in three ways.
  - Its constructor is called. For some reason this standard way of initializing classes isn't used by most programs.
  - Its `init()` method is called. This is the most common place to put the initialization (eg, for the GUI).
  - Its `start()` method is called. This is called whenever the applet becomes visible (`stop()` is called when it becomes hidden), so this should only be used to start/stop animiations, sound, etc.

# End