Download ppt - kaist

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
Programming for WWW
(ICE 1338)
Lecture #7
July 14, 2004
In-Young Ko
iko .AT. icu.ac.kr
Information and Communications University (ICU)
Announcements

Midterm Exam:


10:00AM – 11:30AM, Friday July 16th
Your grades of homework#1 are posted on
the class Web page
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
2
Review of the Previous Lecture

Use of JavaScript
Document Object Model HTML
 Data types
 Operators
 Pattern matching
 Event handling

July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
3
Contents of Today’s Lecture

Java Applets
Applet GUI structure
 Event handling in Applet GUIs
 Concurrency in Applets


Plug-in programs
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
4
Java Applets



Applets are relatively small Java programs
whose execution is triggered by a browser
The purpose of an applet is to provide
processing capability and interactivity for HTML
documents through widgets
The ‘standard’ operations of applets are
provided by the parent class, JApplet
public class class_name extends JApplet { … }


Use of applets is still widespread, and there is
heavy use in intranets
Applets are an alternative to CGI and
embedded client-side scripts
AW lecture notes
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
5
Applets vs. JavaScript (CGI)






CGI is faster than applets and JavaScript, but it
is run on the server
JavaScript is easier to learn and use than Java,
but less expressive
Java is faster than JavaScript
Java graphics are powerful, but JavaScript has
none
JavaScript does not require the additional
download from the server that is required for
applets
Java may become more of a server-side tool, in
the form of servlets, than a client-side tool
AW lecture notes
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
6
Browser Actions for Running Applets
1. Download and instantiate the applet class
2. Call the applet’s init method
3. Call the applet’s start method – This
starts the execution of the applet
4. When the user takes a link from the
document that has the applet, the
browser calls the applet’s stop method
5. When the browser is stopped by the user,
the browser calls the applet’s destroy
method
AW lecture notes
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
7
An Example
A Scrolling Banner
Applet Code
public class AppletTest extends JApplet {
private String msg;
private boolean needToStop = false;
}
public void init() {
msg = getParameter("message");
setFont(new Font("Arial", Font.BOLD, 24));
}
public void start() {
repaint();
}
public void paint(Graphics g) {
g.setColor(Color.blue);
int x = getWidth(), y = 20;
while (!needToStop && x > 20) {
try { Thread.sleep(10); } catch(Exception e) { }
g.clearRect(0, 0, getWidth(), getHeight());
g.drawString(msg, x--, y);
}
}
public void stop() {
needToStop = true;
}
July 14, 2004
Programming for WWW (Lecture#7)
HTML Document
<applet
code="AppletTest.class“
width=600 height=50>
<param name=“message”
value="Information and
Communications University">
</applet>
In-Young Ko, Information Communications University
8
Applet Parameters

Applets can be sent parameters through HTML, using the
<param> tag and its two attributes, name and value


The applet gets the parameter values with getParameter,
which takes the name of the parameter



Parameter values are strings - e.g., <param name = "fruit" value
= "apple">
e.g., String myFruit = getParameter("fruit");
If no parameter with the given name has been specified in the
HTML document, getParameter returns null
If the parameter value is not really a string, the value
returned from getParameter must be converted like:
String pString = getParameter("size");
if (pString == null) mySize = 24;
else
mySize = Integer.parseInt(pString);

The best place to put the code to get parameters isAWinlecture
init
notes
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
9
The JApplet Class



An applet is a panel that can be embedded in
a Web browser window
An applet panel can contain other GUI
components (e.g., buttons, menus, …), and
customized drawings (using the ‘paint’ method)
External frames can be created from an applet
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
10
Paint Method in Applet



Always called by the browser (not the applet
itself) when it displays/refreshes its window
Takes one parameter, an object of class
Graphics, which is defined in java.awt
The protocol of the paint method is:
public void paint(Graphics grafObj) { … }


The Graphics object is created by the browser
Methods in Graphics: drawImage, drawLine,
drawOval, drawPolygon, drawRect, drawString,
fillOval, fillPolygon, fillRect, …
AW lecture notes
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
11
Applet GUI Structure
http://java.sun.com/products/jfc/tsc/articles/containers/index.html
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
12
Java GUI Component Layers
Layered Pane
http://java.sun.com/docs/books/tutorial/uiswing/components/rootpane.html
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
13
Java GUI Program Example
JLabel queryLabel = new JLabel("Query: ");
JTextField queryField = new JTextField(20);
JButton searchButton = new JButton("Search");
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// search action
}
});
JPanel mainPanel = new JPanel();
mainPanel.add(queryLabel);
mainPanel.add(queryField);
mainPanel.add(searchButton);
JFrame mainFrame = new JFrame("Search Input");
mainFrame.getContentPane().add(mainPanel);
mainFrame.pack();
mainFrame.show();
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
14
Event Handling in Java


An event is created by an external action such as
a user interaction through a GUI
The event handler (event listener) is a segment
of code that is called in response to an event
A JButton
Button Pressed Event
Event Listeners
JButton helloButton = new JButton(“Hello”);
helloButton.addActionListener(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
System.out.println(“Hello World!”);
}
}
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
15
Event Listener Registration

Connection of an event to a listener is
established through event listener registration




July 14, 2004
Done with a method of the class that implements the
listener interface (e.g., ActionListener)
The panel object that holds the components can be
the event listener for those components
Event generators send messages (call methods, e.g.,
actionPerformed) to registered event listeners when
events occur
Event handling methods must conform to a standard
protocol, which comes from an interface
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
16
Event Classes and Handler Methods

Semantic Event Classes




ActionEvent - click a button, select from a menu or list, or type
the enter button in a text field
ItemEvent - select a checkbox or list item
TextEvent - change the contents of a text field or text area
For the two most commonly used events, ActionEvent and
ItemEvent, there are the following interfaces and handler
methods:
Interface
------------------ActionListener
ItemListener

Handler method
------------------------actionPerformed
itemStateChanged
The methods to register the listener is the interface name
with “add” prepended
e.g., button1.addActionListener(this);
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
17
Concurrency in Java
“A program is said to be concurrent if it contains
more than one active execution context” [M. Scott]
void concurrentPrint() {
(new Thread () {
public void run() {
while (true) {
try { System.out.print("A"); sleep(0,1);
} catch (Exception e) { }
}
}).start();
}
(new Thread () {
public void run() {
while (true) {
try { System.out.print("B"); sleep(0,1);
} catch (Exception e) { }
}
}).start();
Main Program Control
Printing “A”
Printing “B”
Forking two concurrent
execution threads
Java
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
18
Java Threads


A thread of control is a sequence of program points
reached as execution flows through the program
Java threads are lightweight tasks – all threads run in the
same address space




c.f., Ada tasks are heavyweight threads (processes) that run in
their own address spaces
The concurrent program units in Java are methods
named run, whose code can be in concurrent execution
with other run methods and with main
There are two ways to implement threads, as a subclass
of Thread and by implementing the interface Runnable
Two essential methods:


July 14, 2004
run is the concurrent method
start tells the run method to begin execution
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
19
Thread Methods
class MyThread extends Thread {
public void run() {
…
// Task body
…
}
}
…
Thread aTask = new MyThread();
aTask.start();
…
aTask.setPriority(Thread.MAX_PRIORITY);
…
aTask.yield();
…
aTask.sleep(2000);
…
aTask.join();
…
July 14, 2004

Concurrent method
definition

Tells the run method to
begin execution

Sets the scheduling priority

Gives up the processor to
other threads

Blocks the thread for some
milliseconds

Waits for the thread to
complete
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
20
An Applet with A Thread
public class AppletTestThread extends JApplet implements Runnable {
private String msg;
private boolean needToStop = false;
private int x, y;
public void init() {
msg = getParameter("message");
setFont(new Font("Arial", Font.BOLD, 24));
x = getWidth(); y = 20;
}
public void start() {
Thread appletThread = new Thread(this);
appletThread.start();
}
public void run() {
while (!needToStop && x-- > 20) {
try { Thread.sleep(10); } catch(Exception e) { }
repaint();
}
}
public void paint(Graphics g) {
g.setColor(Color.blue);
g.clearRect(0, 0, getWidth(), getHeight());
g.drawString(msg, x, y);
}…
}
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
21
Applet & Swing References

How to make Applets:
http://java.sun.com/docs/books/tutorial/uiswing/components/applet.
html

The Swing Tutorial:
http://java.sun.com/docs/books/tutorial/uiswing/

Painting in AWT and Swing:
http://java.sun.com/products/jfc/tsc/articles/painting/

Understanding Containers:
http://java.sun.com/products/jfc/tsc/articles/containers/index.html
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
22
Plug-ins



Code modules that are inserted into the
browser
Adds new capabilities to the Web browser
e.g.,
http://wp.netscape.com/plugins/?cp=brictrpr3
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
23
Java Plug-in




Extends the functionality of a web browser,
allowing applets to be run under Sun's Java 2
runtime environment (JRE) rather than the Java
runtime environment that comes with the web
browser
Java Plug-in is part of Sun's JRE and is
installed with it when the JRE is installed on a
computer or can be automatically downloaded
Works with both Netscape and Internet Explorer
Makes it more suitable for widespread use on
consumer client machines that typically are not
as powerful as client platforms
http://java.sun.com/j2se/1.4.2/docs/guide/plugin/developer_guide/contents.html
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
24
Java Plug-in Tags
Internet Explorer
<OBJECT classid="clsid:CAFEEFAC-0014-0002-0000-ABCDEFFEDCBA"
width=“600" height=“50"
Static
codebase="http://java.sun.com/products/plugin/autodl/
Versioning
jinstall-1_4_2-windows-i586.cab">
<PARAM name="code" value=“AppletTest.class">
<PARAM name="codebase" value=“http://bigbear.icu.ac.kr/~iko/classes/ice1338/">
<PARAM name="type" value="application/x-java-applet;jpi-version=1.4.2">
<PARAM name=“message" value=“Information and Communications University”>
</OBJECT>
Netscape
<EMBED type="application/x-java-applet;jpi-version=1.4.2“ width="200" height="200“
pluginspage="http://java.sun.com/j2se/1.4.2/download.html"
codebase="http://bigbear.icu.ac.kr/~iko/classes/ice1338/ “
code=“AppletTest.class“
message=“Information and Communications University“>
<NOEMBED>No Java 2 SDK, Standard Edition v 1.4.2 support for APPLET!!
</NOEMBED>
</EMBED>
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
25
Autodownload Files
http://java.sun.com/j2se/1.5.0/docs
/guide/deployment/deploymentguide/autodl-files.html
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
26
Java Plug-in Tags (cont.)
Combined Form
<OBJECT classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width=“600" height=“50"
Dynamic
codebase="http://java.sun.com/products/plugin/autodl/
Versioning
jinstall-1_4_2-windows-i586.cab#Version=1,4,2,0">
<PARAM name="code" value=“AppletTest.class">
<PARAM name="codebase" value="http://bigbear.icu.ac.kr/~iko/classes/ice1338/">
<PARAM name="type" value="application/x-java-applet;jpi-version=1.4.2">
<PARAM name=“message" value=“Information and Communications University”>
<COMMENT>
<EMBED type="application/x-java-applet;jpi-version=1.4.2“ width="200" height="200“
pluginspage=http://java.sun.com/j2se/1.4.2/download.html
code=“AppletTest.class“
codebase="http://bigbear.icu.ac.kr/~iko/classes/ice1338/ “
message=“Information and Communications University">
<NOEMBED>No Java 2 SDK, Standard Edition v 1.4.2 support for APPLET!!
</NOEMBED>
</EMBED>
“If no version of Java is installed, or a version less
</COMMENT>
than the major version of the family is installed,
</OBJECT>
July 14, 2004
then this will cause automatic redirection to the
latest
the latest
version
the family.”
Programming.cab
for WWW for
(Lecture#7)
In-Young
Ko, Informationin
Communications
University
27
Other Plug-ins

Macromedia Flash Player
<object
classid=“clsid:D27CDB6E-AE6D-11cf-96B8-444553540000”
codebase=“http://download.macromedia.com/pub/
shockwave/cabs/flash/swflash.cab#version=6,0,29,0”
width="832" height="240">
<param name="movie" value="images/flash/intropic.swf">
<param name="quality" value="high">
<embed src="images/flash/intropic.swf" quality="high"
pluginspage="http://www.macromedia.com/go/getflashplayer"
type="application/x-shockwave-flash“
width="832" height="240">
</embed>
</object>
July 14, 2004
Programming for WWW (Lecture#7)
In-Young Ko, Information Communications University
28