Java – Applet Class.

Slides:



Advertisements
Similar presentations
1 More on Applets Overview l Changing Colors l Changing Fonts & Styles l Applet Life-Cycle l Input using Dialog Window l Input using HTML parameters l.
Advertisements

LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
©2004 Brooks/Cole Applets Graphics & GUIs. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Graphical Programs Most applications these days are.
Lecture 24 Applets. Introduction to Applets Applets should NOT have main method but rather init, stop, paint etc They should be run through javac compiler.
Applets. The Applet Class public class MyApplet extends java.applet.Applet {... /** The no-arg constructor is called by the browser when the Web page.
Review of Java Applets Vijayan Sugumaran Decision and Information Sciences Oakland University.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
1 Applets Chapter 1 To understand:  why applets are used to extend the capabilities of Web pages  how an applet is executed and know about the restrictions.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 14 Applets, Images,
A Simple Applet. Applets and applications An applet is a Java program that runs on a web page –Applets can be run from: Internet Explorer Netscape Navigator.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L05 (Chapter 16) Applets.
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Chapter 12 Applets and Advanced Graphics  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets.
Threads and Multimedia Animation, Images, Sound. Animation nAnimation, displaying a sequence of frames to create the illusion of motion, is a typical.
Applets Chapter 17.  Java’s big splash onto the scene came in the mid 90’s. The people at Sun Microsystems had managed to work java programs into Web.
1 APPLETS. 2 Applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as.
JAPPLET.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Applets An applet is a program written in the Java programming language that can be included in an HTML page, much in the same way an image is included.
Chapter 8 Introduction to HTML and Applets Fundamentals of Java.
Chapter 12 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Chapter 10: Applets and Advanced Graphics The Applet Class The Applet Class The HTML Tag The HTML Tag Passing Parameters to Applets Passing Parameters.
Applets Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer.
Java Applets 1. What is an applet? An applet is a small Java program that is typically embedded in a Web page and can be run using the applet viewer or.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
Chapter 6 Applets and HTML  Overview  HTML tags for Applets  Applet Life Cycle  Applet Class  JAR files.
Chapter 14 Applets and Advanced GUI  The Applet Class  The HTML Tag F Passing Parameters to Applets F Conversions Between Applications and Applets F.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Chapter 8 (Horstmann’s Book) Frameworks Hwajung Lee.
1 Applets are small applications that are accessed on an Internet server, transported over the internet, automatically installed and run as a part of web.
Chapter 8 Frameworks. Frameworks Framework is a set of cooperating classes and interface types that structures the essential mechanisms of a particular.
Applets, Images, and Audio Chapter 14 CSCI CSCI 1302 – Applets, Images, and Audio2 Outline Introduction The Applet Class –The init Method –The start.
Creating a Java Application and Applet
1 Applets. 2 What is an applet? Applet: a Java program that can be inserted into a web page and run by loading that page in a browser brings web pages.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Applet: An applet is a java program that is transmitted over the network from the server to client & executed within clients browser. Applets are used.
Java Thread Programming
Sachin Malhotra Saurabh Choudhary
Chapter 02 Data and Expressions.
User-Written Functions
“Under the hood”: Angry Birds Maze
Applets.
A First Look at GUI Applications
Lecture 09 Applets.
Object Oriented Programming
JAVA Applets Pavan D.M..
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
APPLETS.
APPLET.
APPLETS.
UNIT-5.
Java Applets.
APPLETS.
Applet in Java.
Java Programming COMP-417 Applet
Applet Fundamentals Applet are small applications that are accessed on an Internet server, transported over the Internet, automatically installed and run.
11.1 Applets & graphics.
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
Presentation transcript:

Java – Applet Class

Intro The applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as part of a web document. After an applet arrives on the client, it has limited access to resources so that it can produce a graphical user interface and run complex computations without introducing the risk of viruses or breaching data integrity. Let’s begin with the simple applet shown here: This applet begins with two import statements. The first imports the Abstract Window Toolkit (AWT) classes. Applets interact with the user (either directly or indirectly) through the AWT, not through the console-based I/O classes. The AWT contains support for a window-based, graphical user interface. The second import statement imports the applet package, which contains the class Applet. Every applet that you create must be a subclass of Applet.

The next line in the program declares the class SimpleApplet The next line in the program declares the class SimpleApplet. This class must be declared as public, because it will be accessed by code that is outside the program. Inside SimpleApplet, paint( ) is declared. This method is defined by the AWT and must be overridden by the applet. paint( ) is called each time that the applet must redisplay its output. paint( ) is also called when the applet begins execution. The paint( ) method has one parameter of type Graphics, which describes the graphics environment in which the applet is running. This method outputs a string beginning at the specified X,Y location. void drawString(String message, int x, int y) Here, message is the string to be output beginning at x,y. In a Java window, the upper-left corner is location 0,0. Notice that the applet does not have a main( ) method. An applet begins execution when the name of its class is passed to an applet viewer or to a network browser.

There are two ways in which you can run an applet: Executing the applet within a Java-compatible web browser. Using an applet viewer, such as the standard tool, appletviewer. An applet viewer executes your applet in a window. This is generally the fastest and easiest way to test your applet. First Method: To execute an applet in a web browser, you need to write a short HTML text file that contains a tag that loads the applet. <applet code="SimpleApplet" width=200 height=60> </applet> The width and height statements specify the dimensions of the display area used by the applet. After you create this file, you can execute your browser and then load this file, which causes SimpleApplet to be executed. To execute SimpleApplet with an applet viewer, you may also execute the HTML file shown earlier. For example, if the preceding HTML file is called RunApp.html, then the following command line will run SimpleApplet: C:\>appletviewer RunApp.html Second Method: Simply include a comment at the head of your Java source code file that contains the APPLET tag. By doing so, your code is documented with a prototype of the necessary HTML statements, and you can test your compiled applet merely by starting the applet viewer with your Java source code file.

With this approach, you can quickly iterate through applet development by using these three steps: 1. Edit a Java source file. 2. Compile your program. 3. Execute the applet viewer, specifying the name of your applet’s source file. The applet viewer will encounter the APPLET tag within the comment and execute your applet.

Two Types of Applets The first are those based directly on the Applet class which use the Abstract Window Toolkit (AWT) to provide the graphic user interface. This style of applet has been available since Java was first created. The second type of applets are those based on the Swing class Japplet, which use the Swing classes to provide the GUI. Swing offers a richer and often easier-to-use user interface than does the AWT. However, traditional AWT-based applets are still used, especially when only a very simple user interface is required. Thus, both AWT- and Swing-based applets are valid. Because JApplet inherits Applet, all the features of Applet are also available in Japplet.

The Applet Class The Applet class defines the methods shown in Table. Applet provides all necessary support for applet execution, such as starting and stopping. It also provides methods that load and display images, and methods that load and play audio clips. Applet extends the AWT class Panel. In turn, Panel extends Container, which extends Component. These classes provide support for Java’s window-based, graphical interface. Thus, Applet provides all of the necessary support for window-based activities.

Applet Architecture An applet is a window-based program. First, applets are event driven. An applet resembles a set of interrupt service routines. An applet waits until an event occurs. The run-time system notifies the applet about an event by calling an event handler that has been provided by the applet. Once this happens, the applet must take appropriate action and then quickly return. It must perform specific actions in response to events and then return control to the run-time system. Second, the user initiates interaction with an applet. The user interacts with the applet as he or she wants, when he or she wants. These interactions are sent to the applet as events to which the applet must respond. For example, when the user clicks the mouse inside the applet’s window, a mouse- clicked event is generated. Applets can contain various controls, such as push buttons and check boxes. When the user interacts with one of these controls, an event is generated.

An Applet Skeleton Applets override a set of methods that provides the basic mechanism by which the browser or applet viewer interfaces to the applet and controls its execution. Four of these methods, init( ), start( ), stop( ), and destroy( ), apply to all applets and are defined by Applet. Default implementations for all of these methods are provided. Applets do not need to override those methods they do not use. AWT-based applets will also override the paint( ) method, which is defined by the AWT Component class. This method is called when the applet’s output must be redisplayed.

Applet Initialization and Termination When an applet begins, the following methods are called, in this sequence: init( ) start( ) paint( ) When an applet is terminated, the following sequence of method calls takes place: stop( ) destroy( ) The init( ) method is the first method to be called. This is where you should initialize variables. This method is called only once during the run time of your applet. The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. start( ) is called each time an applet’s HTML document is displayed onscreen. So, if a user leaves a web page and comes back, the applet resumes execution at start( ).

paint( ) The paint( ) method is called each time your applet’s output must be redrawn. For example, the window in which the applet is running may be overwritten by another window and then uncovered. Or the applet window may be minimized and then restored. paint( ) is also called when the applet begins execution. The paint( ) method has one parameter of type Graphics. This parameter will contain the graphics context, which describes the graphics environment in which the applet is running. stop( ) The stop( ) method is called when a web browser leaves the HTML document containing the applet—when it goes to another page, for example. When stop( ) is called, the applet is probably running. You should use stop( ) to suspend threads that don’t need to run when the applet is not visible. You can restart them when start( ) is called if the user returns to the page.

destroy( ) The destroy( ) method is called when the environment determines that your applet needs to be removed completely from memory. At this point, you should free up any resources. The stop( ) method is always called before destroy( ). Overriding update( ) In some situations, your applet may need to override another method defined by the AWT, called update( ). This method is called when your applet has requested that a portion of its window be redrawn. The default version of update( ) simply calls paint( ).

Simple Applet Display Methods To output a string to an applet, use drawString( ), which is a member of the Graphics class. Typically, it is called from within either update( ) or paint( ). void drawString(String message, int x, int y) Here, message is the string to be output beginning at x,y. In a Java window, the upper-left corner is location 0,0. The drawString( ) method will not recognize newline characters. If you want to start a line of text on another line, you must do so manually, specifying the precise X,Y location where you want the line to begin. To set the background color of an applet’s window, use setBackground( ). To set the foreground color (the color in which text is shown, for example), use setForeground( ). void setBackground(Color newColor) void setForeground(Color newColor) The class Color defines the constants You can obtain the current settings for the background and foreground colors by calling getBackground( ) and getForeground( ), respectively. Color getBackground( ) Color getForeground( ) Color Constants Color.black Color.magenta Color.blue Color.orange Color.cyan Color.pink Color.darkGray Color.red Color.gray Color.white Color.green Color.yellow Color.lightGray

The methods stop( ) and destroy( ) are not overridden, because they are not needed b this simple applet.

Requesting Repainting How can the applet itself cause its window to be updated when its information changes? For example, if an applet is displaying a moving banner, what mechanism does the applet use to update the window each time this banner scrolls? Whenever your applet needs to update the information displayed in its window, it simply calls repaint( ). The repaint( ) method is defined by the AWT. It causes the AWT run-time system to execute a call to your applet’s update( ) method, which, in its default implementation, calls paint( ). Thus, for another part of your applet to output to its window, simply store the output and then call repaint( ). The AWT will then execute a call to paint( ), which can display the stored information. The repaint( ) method has four forms.

The simplest version of repaint( ) is shown here: void repaint( ) This version causes the entire window to be repainted The following version specifies a region that will be repainted: void repaint(int left, int top, int width, int height) These dimensions are specified in pixels. Calling repaint( ) is essentially a request that your applet be repainted sometime soon. Multiple requests for repainting that occur within a short time can be collapsed by the AWT in a manner such that update( ) is only called sporadically. This can be a problem in many situations, including animation, in which a consistent update time is necessary. One solution to this problem is to use the following forms of repaint( ): void repaint(long maxDelay) void repaint(long maxDelay, int x, int y, int width, int height) maxDelay specifies the maximum number of milliseconds that can elapse before update( ) is called. If the time elapses before update( ) can be called, it isn’t called. There’s no return value or exception thrown, so you must be careful.

A Simple Banner Applet This applet scrolls a message, from right to left, across the applet’s window. Since the scrolling of the message is a repetitive task, it is performed by a separate thread, created by the applet when it is initialized.

First, notice that SimpleBanner extends Applet, as expected, but it also implements Runnable. This is necessary, since the applet will be creating a second thread of execution that will be used to scroll the banner. Inside init( ), the foreground and background colors of the applet are set. After initialization, the run-time system calls start( ) to start the applet running. start( ), a new thread of execution is created and assigned to the Thread variable t. Then, the boolean variable stopFlag, which controls the execution of the applet, is set to false. Next, the thread is started by a call to t.start( ). Inside run( ), the characters in the string contained in msg are repeatedly rotated left. Between each rotation, a call to repaint( ) is made. This eventually causes the paint( ) method to be called, and the current contents of msg are displayed. Between each iteration, run( ) sleeps for a quarter of a second. The stopFlag variable is checked on each iteration. When it is true, the run( ) method terminates. If a browser is displaying the applet when a new page is viewed, the stop( ) method is called, which sets stopFlag to true, causing run( ) to terminate. This is the mechanism used to stop the thread when its page is no longer in view.

Using the Status Window The status window is a good place to give the user feedback about what is occurring in the applet, suggest options, or possibly report some types of errors. The status window also makes an excellent debugging aid, because it gives you an easy way to output information about your applet. To do so, call showStatus( ) with the string that you want displayed.

Passing Parameters to Applets The APPLET tag in HTML allows you to pass parameters to your applet. To retrieve a parameter, use the getParameter( ) method. It returns the value of the specified parameter in the form of a String object. For numeric and boolean values, you will need to convert their string representations into their internal formats.

Improving the Banner Applet In the previous version, the message being scrolled was hard-coded into the applet. However, passing the message as a parameter allows the banner applet to display a different message each time it is executed.

getDocumentBase( ) and getCodeBase( ) Often, you will create applets that will need to explicitly load media and text. Java will allow the applet to load data from the directory holding the HTML file that started the applet (the document base) and the directory from which the applet’s class file was loaded (the code base). These directories are returned as URL objects by getDocumentBase( ) and getCodeBase( ). They can be concatenated with a string that names the file you want to load. To actually load another file, you will use the showDocument( ) method defined by the AppletContext interface.

AppletContext and showDocument( ) One application of Java is to use active images and animation to provide a graphical means of navigating the Web. To allow your applet to transfer control to another URL, you must use the showDocument( ) method defined by the AppletContext interface. AppletContext is an interface that lets you get information from the applet’s execution environment. The methods defined by AppletContext are shown in Table. The context of the currently executing applet is obtained by a call to the getAppletContext( ) method defined by Applet. Within an applet, once you have obtained the applet’s context, you can bring another document into view by calling showDocument( ). There are two showDocument( ) methods. The method showDocument(URL) displays the document at the specified URL. The method showDocument(URL, String where) displays the specified document at the specified location within the browser window.

The Methods Defined by the AppletContext Interface

The following applet demonstrates AppletContext and showDocument( ) The following applet demonstrates AppletContext and showDocument( ). Upon execution, it obtains the current applet context and uses that context to transfer control to a file called Test.html. This file must be in the same directory as the applet. Test.html can contain any valid hypertext that you like.

The AudioClip Interface The AudioClip interface defines these methods: play( ) (play a clip from the beginning) stop( ) (stop playing the clip) loop( ) (play the loop continuously). After you have loaded an audio clip using getAudioClip( ), you can use these methods to play it. The AppletStub Interface The AppletStub interface provides the means by which an applet and the browser (or applet viewer) communicate.