28
Applets, Graphical User I nterfaces, and Threads / Chapter 9 1 Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads

  • Upload
    onaona

  • View
    23

  • Download
    1

Embed Size (px)

DESCRIPTION

Applets, Graphical User Interfaces, and Threads. Interface ?. an agent between two entities. Interface. Interface. gas. pump. Interface. mortgage agent. dream house. you. Interfaces. real interfaces. ATM machine remote control bank teller - PowerPoint PPT Presentation

Citation preview

Page 1: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

1

Applets, Graphical User Interfaces, and Threads

Page 2: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

2

Interface ?

• an agent between two entities

Page 3: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

3

Interface

Page 4: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

4

Interface

Page 5: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

5

Interface

Page 6: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

6

Interfaces

• ATM machine

• remote control

• bank teller

• browser (Netscape Navigator or Internet Explorer)

real interfaces

Page 7: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

7

Interfaces

• are designed to handle events initiated by user

Page 8: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

8

Events

• single click

• double click

• dragging

Page 9: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

9

Events

• An applet has mechanisms to handle events

Page 10: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

10

Applet

• A Java programs designed to run in a browser

Page 11: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

11

Applet

• applet class files are embedded in a HTML document

Page 12: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

12

all applets are subclasses of the applet class

Applet

public class SomeApplet extends Applet{ … }

Page 13: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

13

Applets differ from applications in that a browser controls the applet’s execution

Applet

Page 14: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

14

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.

Applet

Page 15: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

15

Why Restrict Applets ?

• An applet is code from another machine that you are not familiar

Page 16: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

16

Applet Restrictions

• cannot automatically send information to a user’s printer

• prevented from performing file operations

• prevented from certain network activities

Page 17: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

17

Applet Restriction Relaxed

• can be relaxed by changing the browser’s security settings

Page 18: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

18

Applet Syntax

public class appletName extends Applet{ … }

Page 19: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

19

Sample 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);

}

}

Page 20: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

20

Applet’s HTML File

<applet code=Welcome.class width=215 height=100>

</applet>

•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

Page 21: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

21

Applet’s HTML File

<applet code=Welcome.class width=215 height=100>

</applet>

the area of the applet is determined by the width and height parameters

Page 22: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

22

Applet 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(){ }

}

Page 23: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

23

Applet’s Life

•the browser manages the life cycle of the applet•the life cycle includes the following

› init› start› stop› destroy

•paint

Page 24: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

24

Applet Methods(init)

•called when an applet begins•called once during the applet’s execution

•code to initialize the applet is placed in init()

Page 25: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

25

Applet Methods(start)

•called immediately after init() •when the page where the applet is running gains focus

Page 26: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

26

Applet Methods(stop)

•called when the page where the applet is running loses focus

Page 27: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

27

Applet Methods(destroy)

•called when the browser terminates

Page 28: Applets, Graphical User Interfaces, and Threads

Applets, Graphical User Interfaces, and Threads / Chapter 9

28

Applet Methods(paint)

•refreshes the browser display