Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
CSC 205 – Java
Programming II
Lecture 23
Applet
Types of Java Programs
Applets
Applications
Console applications
Graphics applications
Applications are stand-alone programs
An application must have a main method
What Is Applet
An applet is
a program that adheres to a set of
conventions
run within a Java-compatible Web browser
downloaded from a Web server
An applet must extend the Applet class
Download From Web Server
Advantages &
Disadvantages
• Advantages
– Can be accessed where Internet is available
– When GUIs need to be upgraded, just change them at
one location: on the Web server
• Disadvantages
– Time to download may be long
– Security could be an issue
Security Policies
Browsers impose the following restrictions
on any applet that is loaded over the
network:
An applet cannot load libraries or define
native methods.
It cannot ordinarily read or write files, or start
any program on the host that's executing it.
It cannot make network connections except
to the host that it came from.
It cannot read certain system properties.
Windows that an applet brings up look
different than windows that an application
brings up.
Inheritance Hierarchy
java.applet
Class Applet
java.lang.Object
|
+--java.awt.Component
|
+--java.awt.Container
|
+--java.awt.Panel
|
+--java.applet.Applet
A Typical Applet
import java.applet.Applet;
import java.awt.Graphics;
public class Simple extends Applet {
//methods to be overridden
public void init() {…}
public void start() {…}
public void stop() {…}
public void destroy() {…}
void addItem(String newWord) { …; repaint();
}
public void paint(Graphics g) {…}
//inherited from the Container class
}
Life Cycle of an Applet
• Loading the Applet
– An instance of the applet's controlling class (an
Applet subclass) is created
– The applet initializes itself
– The applet starts running
• Leaving and Returning to the Applet's Page
– The applet stops running when leaving the page
– the applet can start itself again when returning
Methods for Milestones
An applet can override the following
methods
init To initialize the applet each time it's
loaded (or reloaded).
start To start the applet's execution, such
as when the applet's loaded or when the
user revisits a page that contains the applet.
stop To stop the applet's execution, such as
when the user leaves the applet's page or
quits the browser.
destroy To perform a final cleanup in
preparation for unloading
Methods for Drawing
An applet can override the following two
display methods (of the Container class) :
paint
The basic display method. Many applets
implement the paint method to draw the
applet's representation within a browser
page.
update
A method you can use along with paint to
improve drawing performance
Animation
To update applet changes
invoke the repaint method (inherited from the
Component class) periodically
The paint method will be invoked when the
applet is repainted
Event Handling
Applets inherit a group of event-handling
methods from the Component class
To react to an event, an applet must
override
either the appropriate event-specific method,
or
the handleEvent method (from the
Component class)
Event Handling – Example
Adding the following code to the Simple
applet makes it respond to mouse clicks.
import java.awt.Event;
. . .
public boolean mouseDown(Event event, int x,
int y) {
addItem("click!... ");
return true;
}
Deprecation
Unfortunately, many of the samples
available online or in textbooks use
deprecated methods
You will be warned when you compile code
with deprecated methods
Use the –deprecation option to see detailed
info
javac –deprecation Simple.java
Replace deprecated methods with newer
methods as recommended by latest version
of Java API
Deprecation
Using UI Components
Because the Applet class inherits from the
AWT Container class, it's easy to add
components to applets and to use layout
managers to control the components'
onscreen positions.
add
Adds the specified Component
remove
Removes the specified Component
setLayout
Sets the layout manager
Testing Applets
Two ways to run an applet
Use the applet viewer
appletviewer simple.html
Embed applets into Web pages
Both need to use the applet tag in HTML
files
<APPLET CODE=AppletSubclass.class
WIDTH=anInt HEIGHT=anInt>
</APPLET>
HTML – An Overview
Markup language
use tags to represent the meaning and/or
content of the enclosed data
Some features
Not case sensitive
Loose syntax
Predefined tags
Text is the only data type
More Examples
The following two tags are equivalent
<APPLET CODE=Simple.class WIDTH=100
HEIGHT=100>
</APPLET>
<applet code=“Simple.class” width=“100”
height=“100”>
</applet>
A Simple HTML File
<html>
<body>
<p>Click on the applet to start
the animation.</p>
<!– this line is comment -->
<applet
code="SelectionSortApplet.class"
width="300" height="300">
</applet>
</body>
</html>
Thread
A thread is a program unit that is executed
independently of other parts of the program
The JVM executes each thread for a short
time and then switches to another thread
A programmer can concentrate on what
tasks each thread need to perform
and possibly, the communication between
threads
Write a Thread
Follow the steps
Write a class that extends the Thread
class
Place the code for the task in the run
method
Create an object of the your thread
class
Call the start method to activate
your thread
Sample Thread Class
public class GreetingThread extends Thread{
public GreetingThread(String aGreeting){
greeting = aGreeting;
}
public void run() {
try {
for (int i = 1; i <= REPETITIONS; i++) {
Date now = new Date();
System.out.println(now + " " +
greeting);
sleep(DELAY);
}
} catch (InterruptedException exception){
}
}
Running A Thread – I
public class GreetingThreadTest{
public static void main(String[] args)
{
GreetingThread t1
= new GreetingThread("Hello, World!");
GreetingThread t2
= new GreetingThread("Goodbye, World!");
t1.start();
t2.start();
}
}
Sample Output
The Runnable Interface
Problems of writing a thread which extends
another class, e.g. the Frame class
Since Java doesn’t allow multiple inheritance
Can implement the Runnable interface
instead
public class MyFrame extends Frame
implements Runnable {
public MyFrame() {…}
public void run() {…}
}
Running A Thread – II
Follow the steps
Construct a Runnable object
Create a thread using the Runnable
object
Invoke the start method of the
thread
MyFrame frame = new MyFrame();
Thread myThread = new Thread(frame);
myThread.start();
start
interrupt