Sie sind auf Seite 1von 23

Applets

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

 users can run applets simply by


visiting a web page that
contains an applet program
(if they have the Java runtime
environment installed on their
computer)

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

 problem: most web browsers (MS IE, Netscape, etc.)


only have Java v1.1.8 installed as their Java VM

 solution 1: only use Java classes/features that have


been around since v1.1.8 (blech!)
 solution 2: use Java Plug-in to give browser ability to
use newer Java features

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 main method needed; applet's init/start are run by browser

 no setVisible(true) call needed (done automatically)

 no setDefaultCloseOperation(...) needed
 in fact, calling this will crash the program,
because an applet isn't allowed to exit the browser when it closes

 no setSize(...) / pack() needed; size is determined by web


page

 no setTitle(String) needed; window title is set by web page

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

 public void start()


Called by browser every time the user's browser visits
the applet's web page
 put code that would have been in GUI's main method in start
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.

 public void destroy()


Called by browser when it shuts down.
 not usually necessary to override this method

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.*;

public class HelloWorldApplet extends JApplet {


public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString("Hello World!", 30, 30);
}
}

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)

 Web pages have two main sections:


 head: header information
(not shown on screen) <HTML>
 body: actual text of the web page

 HTML text is placed


into tags between <HEAD> <BODY>
< and >

<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.

 public URL getCodeBase()


Returns the URL of the folder where this applet's web
page resides on the web.

14
Loading images in applet
 public Image getImage(URL url)
Loads and returns the image file at the given URL.

 public Image getImage(URL folder, String file)


Loads the image in the given folder with the given
name. Often used with getCodeBase().

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.

 public InputStream openStream()


Returns an input stream to read bytes from the given
URL address's corresponding file.
 Since applets cannot directly open files, read them with

InputStream file = new URL(filename).openStream();

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.

 public void play(URL url)


 public void play(URL folder, String file)
Plays the sound file at the given URL location.

 public class AudioClip


 public void loop()
 public void play()
 public void stop()
17
Passing parameter to applet
 Parameters can be placed in the HTML code of your
web page, which your applet can read:
<APPLET code="mypackage/MyApplet.class"
width=400 height=300>
<PARAM name="password" value="Tacoma"> </PARAM>
</APPLET>

 Methods in the JApplet class:


 public String getParameter(String name)
Returns the value of the parameter with the given name.

String password = this.getParameter("password");

 public String[][] getParameterInfo()


Returns an array of all parameter names, descriptions, and
values.
18
JAR Files (Yousa likey!)
 JAR: Java ARchive. A group of Java classes and
supporting files combined into a single file compressed
with ZIP format, and given .JAR extension.

 Advantages of JAR files:


 compressed; quicker download
 just one file; less mess
 can be executable

 Disadvantages of JAR files:


 somewhat hard to get them working!

19
Creating a JAR archive
1. create the JAR archive
 DOS: jar -cvf filename.jar files
Example: jar -cvf MyAppletJar.jar *.class *.gif *.jpg

 some IDEs (JBuilder, Eclipse) can create JARs automatically


 Eclipse: File -> Export... -> JAR file

2. Modify your web page to use the JAR file using ARCHIVE attr.

<APPLET code="MyApplet.class" archive="MyAppletJar.jar"


width=300 height=400> </APPLET>

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)

 a JAR file can be opened in WinZIP or other archive program to


examine its contents

 Manifest file: Used to create an executable application JAR.


jar -cvfm MANIFEST MyAppletJar.jar
mypackage/*.class *.gif

Contents of MANIFEST file:


Main-Class: MyApplet

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

Das könnte Ihnen auch gefallen