280 likes | 407 Views
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
E N D
Applets, Graphical User Interfaces, and Threads Applets, Graphical User Interfaces, and Threads / Chapter 9
Interface ? • an agent between two entities Applets, Graphical User Interfaces, and Threads / Chapter 9
Interface Applets, Graphical User Interfaces, and Threads / Chapter 9
Interface gas pump Applets, Graphical User Interfaces, and Threads / Chapter 9
Interface mortgage agent dream house you Applets, Graphical User Interfaces, and Threads / Chapter 9
Interfaces real interfaces • ATM machine • remote control • bank teller • browser (Netscape Navigator or Internet Explorer) Applets, Graphical User Interfaces, and Threads / Chapter 9
Interfaces • are designed to handle eventsinitiated by user Applets, Graphical User Interfaces, and Threads / Chapter 9
Events • single click • double click • dragging Applets, Graphical User Interfaces, and Threads / Chapter 9
Events • An applet has mechanisms to handle events Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet • A Java programs designed to run in a browser Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet • applet class files are embedded in a HTML document Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet all applets are subclasses of the applet class public class SomeApplet extends Applet{ … } Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Applets differ from applications in that a browser controls the applet’s execution Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet 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. Applets, Graphical User Interfaces, and Threads / Chapter 9
Why Restrict Applets ? • An applet is code from another machine that you are not familiar Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Restrictions • cannot automatically send information to a user’s printer • prevented from performing file operations • prevented from certain network activities Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Restriction Relaxed • can be relaxed by changing the browser’s security settings Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Syntax public class appletName extends Applet{ … } Applets, Graphical User Interfaces, and Threads / Chapter 9
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); } } Applets, Graphical User Interfaces, and Threads / Chapter 9
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 Applets, Graphical User Interfaces, and Threads / Chapter 9
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 Applets, Graphical User Interfaces, and Threads / Chapter 9
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(){ } } Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet’s Life • the browser manages the life cycle of the applet • the life cycle includes the following • init • start • stop • destroy • paint Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Methods(init) • called when an applet begins • called once during the applet’s execution • code to initialize the applet is placed in init() Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Methods(start) • called immediately after init() • when the page where the applet is running gains focus Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Methods(stop) • called when the page where the applet is running loses focus Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Methods(destroy) • called when the browser terminates Applets, Graphical User Interfaces, and Threads / Chapter 9
Applet Methods(paint) • refreshes the browser display Applets, Graphical User Interfaces, and Threads / Chapter 9