Beruflich Dokumente
Kultur Dokumente
1
What is an applet?
applet: a Java program that can be inserted into a web
page and run by loading that page in a browser
brings web pages to life with interactive content, multimedia,
games, and more
the feature of Java that is primarily responsible for its initial
popularity
2
Applet classes in Java
implementation
a top-level container, like a JFrame
behaves more like a JPanel
class javax.swing.JApplet
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
java.applet.Applet
javax.swing.JApplet
3
Browser interaction
applets are reliant on browser's Java Virtual Machine to
run
4
Applet life cycle
browser visits page containing an applet
browser calls init on that applet, once
browser calls start on that applet init()
browser goes away from that page start()
browser calls stop on that applet
do some work
browser comes back to that page
browser calls start again on that applet
... stop()
browser shuts down
browser calls destroy on the applet, once destroy()
5
javax.swing.JApplet
Differences between JApplet and JFrame:
no setDefaultCloseOperation(...) needed
in fact, calling this will crash the program,
because an applet isn't allowed to exit the browser when it closes
6
JApplet control methods
public void init()
Called by the browser when it downloads your applet
for the first time (when you visit the applet's web page
for the first time).
put code that would have been in GUI's constructor in init
instead
7
JApplet control methods 2
public void stop()
Called by browser every time user leaves applet's web
page
If the applet has any long-standing, CPU-draining processes,
put some code in stop to halt them.
8
A very simple applet
An applet can be treated like a drawing panel or a
container to hold other components.
import java.awt.*;
import javax.swing.*;
9
Applet inside a web page
applet programs live inside web pages
when the browser reaches the web page:
all necessary class files are downloaded from server, loaded
onto JVM in web browser, and executed
10
HTML (web pages)
Web pages are written in the HyperText Markup
Language (HTML)
<TITLE> (content)
11
Web page with an applet
Put a page like this in your project's folder:
<HTML>
<HEAD>
<TITLE>My Applet Page</TITLE>
</HEAD>
<BODY>
<APPLET code="mypackage/MyApplet.class"
width=400 height=300> </APPLET>
</BODY>
</HTML>
12
JApplet restrictions
can't read or write any files on user's hard disk
can't make network connections to computers other
than web server hosting applet
can't execute other programs
can't read much information about system on which it
is running
any window popped up by applet will have a warning at
the bottom
13
Other JApplet methods
public void showStatus(String str)
Places the given text into the browser's status bar.
14
Loading images in applet
public Image getImage(URL url)
Loads and returns the image file at the given URL.
15
java.net.URL
public URL(String site)
throws MalformedURLException
public URL(URL folder, String file)
throws MalformedURLException
Constructs a new URL object to represent the given
string.
16
Playing sounds from applet
public AudioClip getAudioClip(URL url)
public AudioClip getAudioClip(URL folder,
String file)
Loads an audio clip object from the given URL location.
19
Creating a JAR archive
1. create the JAR archive
DOS: jar -cvf filename.jar files
Example: jar -cvf MyAppletJar.jar *.class *.gif *.jpg
2. Modify your web page to use the JAR file using ARCHIVE attr.
20
Other stuff about JARs
Running a JAR from the command line:
java -jar filename.jar
(or you can just double-click the .jar file in Windows Explorer)
21
Resources inside a JAR
If your program reads files that you've now embedded
inside your JAR archive, you must now explicitly load
those resources from the JAR using Java's "class
loader":
before:
Scanner in = new Scanner(new File(PIECES_FILE_NAME));
after:
InputStream stream =
this.getClass().getClassLoader().getResourceAsStream(
PIECES_FILE_NAME);
Scanner in = new Scanner(stream);
22
JAR to EXE (JSmooth)
JSmooth is a free program that
converts JARs into EXE files.
http://jsmooth.sourceforge.net/
If the machine does not have the Java
Runtime installed, your EXE will
display an error message to the user
and help them download Java.
Using JSmooth:
choose Skeleton -> Windowed Wrapper
name your .exe under Executable -> Executable Binary
browse to your .jar under Application -> Embedded JAR
select the main class under Application -> Main class
(optional) set minimum Java VM version to 1.5 in JVM Selection
23