Title: Applets, Graphical User Interfaces, and Threads
1Applets, Graphical User Interfaces, and Threads
2Interface ?
- an agent between two entities
3Interface
4Interface
gas
pump
5Interface
mortgage agent
dream house
you
6Interfaces
real interfaces
- ATM machine
- remote control
- bank teller
- browser (Netscape Navigator or Internet Explorer)
7Interfaces
- are designed to handle events initiated by user
8Events
- single click
- double click
- dragging
9Events
- An applet has mechanisms to handle events
10Applet
- A Java programs designed to run in a browser
11Applet
- applet class files are embedded in a HTML document
12Applet
all applets are subclasses of the applet class
public class SomeApplet extends Applet
13Applet
Applets differ from applications in that a
browser controls the applets execution
14Applet
the browser calls your applet's init() method
when the applet is started. An applet's start()
and stop() methods are called when the page where
the applet is running gains or loses focus.
15Why Restrict Applets ?
- An applet is code from another machine that you
are not familiar
16Applet Restrictions
- cannot automatically send information to a users
printer - prevented from performing file operations
- prevented from certain network activities
17Applet Restriction Relaxed
- can be relaxed by changing the browsers security
settings
18Applet Syntax
public class appletName extends Applet
19Sample Applet
import java.applet.Applet import
java.awt.Graphics public class Welcome extends
Applet public void paint(Graphics g)
g.drawString("Java is fuuuuuuuuuun )
...",25,25)
20Applets HTML File
ltapplet codeWelcome.class width215
height100gt lt/appletgt
- the HTML file name does not have to match the
name of the class name as in the java file - however, the class name in the applet code tag
must match the class name
21Applets HTML File
ltapplet codeWelcome.class width215
height100gt lt/appletgt
the area of the applet is determined by the width
and height parameters
22Applet Template
import java.awt. import java.applet. public
class SampleApplet extends Applet public
void init() System.out.println("
SampleApplet.init() executed")
public void paint(Graphics g) public
void start() public void stop()
23Applets Life
- the browser manages the life cycle of the applet
- the life cycle includes the following
- init
- start
- stop
- destroy
24Applet Methods(init)
- called when an applet begins
- called once during the applets execution
- code to initialize the applet is placed in init()
25Applet Methods(start)
- called immediately after init()
- when the page where the applet is running gains
focus
26Applet Methods(stop)
- called when the page where the applet is running
loses focus
27Applet Methods(destroy)
- called when the browser terminates
28Applet Methods(paint)
- refreshes the browser display