Title: Java Applet
1Java Applet
- Lin, Cui
- Computer Science Department
- Wayne State University
2What is Applet
- A small program written in Java and included in a
HTML page. It is independent of the operating
system on which it runs - A small computer program that can be transmitted
from a server to a client computer and executed
on the client. - A small program that can be sent along with a Web
page to a user. Java applets can perform
interactive animations, immediate calculations,
or other simple tasks without having to send a
user request back to the server.
3Features of Applet
- Run inside web browser or AppletViewer
- OOP programming
- Graphical Java programs
- Platform-neutral
4Applet and HTML
- An applet is accessed by an HTML file.
- HTML is a mark-up language (it adds to text
content by marking it up with tags) - Browsers display the text and process the tags
For example - CIS255 ltbgt Homework lt/bgt Assignment
- The browser would display
- CIS255 Homework Assignment
5Applet and HTML
- CrossWord Example
- ltHTMLgt
- ltHEADgt
- ltTITLEgtCrossWordlt/TITLEgt
- lt/HEADgt
- ltBODYgt
- ltCENTERgt
- ltAPPLET codebase"classes" code"CrossWord.class"
width960 height600gtlt/APPLETgt - lt/CENTERgt
- lt/BODYgt
- lt/HTMLgt
6How Applet works
7The Applet Class (Overview)
- The Java class Applet contains all the behaviors
(methods), which an applet container (browser)
expects to see. (e.g. init, start, paint, stop,
destroy) - When creating an applet class, your class extends
the Applet class, and inherits these behaviors. - Your class can then override any of the behaviors
8The Applet Class (Basic operations)
9The Applet Class (Paint)
- System-triggered Paintingthe system requests a
component to render its contents, usually for one
of the following reasons - The component is first made visible on the
screen. - The component is resized.
- The component has damage that needs to be
repaired. - App-triggered Painting
- the component decides it needs to update its
contents because its internal state has changed.
10The Applet Class (Paint)
11The Applet Class (Repaint)
- The program determines that either part or all of
a component needs to be repainted in response to
some internal state change. - The program invokes repaint() on the component,
which registers an asynchronous request to the
AWT that this component needs to be repainted.
12The Applet Class (Repaint)
- public void Solve()
-
- for (int i 0 i lt BLKSDOWN i)
- for (int j 0 j lt BLKSACROSS j)
- guessesij Character.toUpperCase(
answersij) - repaint()
-
- public void Clear()
-
- for (int i 0 i lt BLKSDOWN i)
- for (int j 0 j lt BLKSACROSS j)
- guessesij ' '
- repaint()
13The Applet Class (Update)
- When the applet invokes repaint(), to request its
drawing area be repainted, AWT (Abstract Window
Toolkit) invokes update(). - The inherited update() method clears the applet's
drawing area to its background color before
invoking paint().
14The Applet Class (Update)
public void update(Graphics g) Image
backbuffer Graphics backg
Font numFont new Font("Arial", Font.PLAIN,
10 ) Font letFont new Font("Arial",
Font.BOLD, 12 ) backbuffer
createImage( WIDTH5, HEIGHT5) backg
backbuffer.getGraphics()
backg.setColor(Color.WHITE)
backg.setFont(letFont)..
15The Applet Class (Event Listener)
- Every time the user types a character or pushes a
mouse button, an event occurs. - Any object can be notified of the event.
- All the object has to do is implement the
appropriate interface and be registered as an
event listener on the appropriate event source.
16The Applet Class (Event Listener)
- In the declaration for the event handler class,
one line of code specifies that the class either
implements a listener interface or extends a
class that implements a listener interface.
public class MyClass implements ActionListener
- Another line of code registers an instance of the
event handler class as a listener on one or more
components. someComponent.addActionListener(inst
anceOfMyClass) - The event handler class has code that implements
the methods in the listener interface. public
void actionPerformed(ActionEvent e) ...//code
that reacts to the action...
17The Applet Class (Event Listener)
public void mouseClicked(MouseEvent e)
int i (int)(e.getY() / (1.0
HEIGHT/BLKSDOWN)) public
void mousePressed(MouseEvent e)
public void mouseReleased(MouseEvent e)
public void mouseEntered(MouseEvent e)
public void mouseExited(MouseEvent e)
18Procedures of creating an Applet
- Develop Java source code to implement your applet
- Compile the source file to produce class file
- Make an HTML file with the applet tag that
references that class file - Use browser to execute that HTML file OR run
AppletViewer utility.
19Implement Applet (6-1)
- Every applet must implement one or more of the
init, start, and paint methods - The first lines must import the necessary classes
used in the applet
import java.io. import java.lang.String import
java.lang.System import javax.swing. import
javax.swing.event. import java.awt.event. impo
rt java.awt. ...
20Implement Applet (6-2)
- Important linkshttp//java.sun.com/downloads/
(JDK and JRE)http//java.sun.com/j2se/1.4.2/docs
/api/index.html (Applet, Paint, Repaint,
Update)http//java.sun.com/docs/books/tutorial/u
iswing/events/generalrules.html(Listener and
Event)
21Implement Applet (6-3)
22Implement Applet (6-4)
- An example of good program// CheckerDrag.java
- import java.awt.
- import java.awt.event.
- public class CheckerDrag extends
java.applet.Applet -
- // Dimension of checkerboard square.
- final static int SQUAREDIM 40
23Implement Applet (6-5)
public void init () // Obtain the size
of the applet's drawing area. width
getSize ().width height getSize
().height // Create image buffer.
imBuffer createImage (width, height)
24Implement Applet (6-6)
// Attach a mouse motion listener to the applet.
That listener // listens for mouse drag events.
addMouseMotionListener (new
MouseMotionAdapter ()
public void mouseDragged
(MouseEvent e)
if
(inDrag)
//
Calculate draggable checker's new
// origin (the
upper-left corner of
// the checker rectangle). int
tmpox e.getX () - relx
25Checklist before submission
- Project Due 4/18/2007 midnight
- Submit a zip file(david_smith_px.zip) by
blackboard system, which includes - Readme.txt
- Report.doc
- Source code
- Compiled classes