APPLET.

Slides:



Advertisements
Similar presentations
PHY-102 SAPIntroductory GraphicsSlide 1 Introductory Graphics In this section we will learn how about how to draw graphics on the screen in Java:  Drawing.
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.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
Graphics Programming With Applets Feb 23, Applets There are three different types of executable java code. –Standalone application, which has main()
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 16 Applets.
18-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.
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.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
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.
Chapter 3 - Introduction to Java Applets Outline 3.1Introduction 3.2Thinking About Objects 3.4A Simple Java Applet: Drawing a String 3.5Two More Simple.
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.
A Simple Applet.
Java Applets. Lecture Objectives  Learn about Java applets.  Know the differences between Java applets and applications.  Designing and using Java.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Applets Java API.
20-753: Fundamentals of Web Programming Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 16: Java Applets & AWT Fundamentals of Web Programming.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Java On The Web Session 15. Memory Upload JAVA Applets Colors Fonts Drawing Methods Posting your Applet.
1 APPLETS. 2 Applets are small applications that are accessed on an Internet server, transported over the Internet, automatically installed, and run as.
JAPPLET.
Learn about the types of Graphics that are available Develop a basic Graphics applet Develop a basic Graphics application Review the Java API and use.
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.
Abstract Window Toolkit (AWT) The Abstract Window Toolkit (AWT) supports Graphical User Interface (GUI) programming. AWT features include:  A rich set.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Introduction to Applets Course Lecture Slides 29 th July 2010.
Applets & Graphics. Applets programs that run inside a browser Java platform-independence makes applets possible security restrictions: –cannot read or.
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.
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
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.
Pravin Yannawar, DOCS, NMU Jalgaon Basic Java : Applets 2 Objectives of This Session Identify the need for Applets Distinguish between Applets and Applications.
APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
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.
1 A Simple Applet. 2 Applets and applications An applet is a Java program that runs on a web page Applets can be run within any modern browser To run.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Graphics Methods public abstract void drawString(String str, int x, int y): is.
CSI 3125, Preliminaries, page 1 AWT. CSI 3125, Preliminaries, page 2 AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based.
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.
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
AWT Java AWT (Abstract Windowing Toolkit) is an API to develop GUI or window-based application in java. Java AWT components are platform-dependent i.e.
1 Applets Programming. Introduction Java programs are divided into two main categories, applets and applications. An application is an ordinary Java program.
CPCS 391 Computer Graphics Lab One. Computer Graphics Using Java What is Computer Graphics: Computer graphics are graphics created using computers and,
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.
Sachin Malhotra Saurabh Choudhary
Applets.
Lecture 09 Applets.
Object Oriented Programming
JAVA Applets Pavan D.M..
Java Applet.
Applets In Java Visit for more Learning Resources 1.
Java Applets.
Distributed Computing, M. L. Liu
Distributed Computing, M. L. Liu
APPLETS.
APPLETS.
UNIT-5.
Java Applets.
Java applets 1/3/2019.
Java – Applet Class.
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.
Handout-14 Applets and Graphics
JAVA APPLET PREPARED BY Mr. Jahanzaib Ahmed
Enabling Application Delivery Via the Web
ITEC324 Principle of CS III
APPLET PROGRAMMING.
Presentation transcript:

APPLET

Introduction Applet 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 various computations without introducing the risk of viruses or breaching data integrity.

public class SimpleApplet extends Applet{ import java.awt.*; import java.applet.*; public class SimpleApplet extends Applet{ public void paint(Graphics g){ g.drawString(“A simple Applet”,20,20); }

After you enter the source code for SimpleApplet, compile in the same way that you have been compiling programs. However, running SimpleApplet involves a different process. In fact, 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. One way to execute an applet in a web browser is to write a short HTML text file that contains a tag that loads the applet. At the time of this writing, Oracle recommends using the APPLET tag for this purpose. <applet code=“SimpleApplet.class” width=200 height=200> </applet>

However, a more convenient method exists that you can use to speed up testing. 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.

Few things of Applets Applets do not need a main( ) method. Applets must be run under an applet viewer or a Java-compatible browser. User I/O is not accomplished with Java’s stream I/O classes. Instead, applets use the interface provided by a GUI framework.

Two Types of Applets It is important to state at the outset that there are two varieties of applets based on Applet. The first are those based directly on the Applet class These applets use the Abstract Window Toolkit (AWT) to provide the graphical user interface (or use no GUI at all). 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 inherits Applet. Swing applets use the Swing classes to provide the GUI. Swing offers a richer and often easier-to-use user interface than does the AWT. Thus, both AWT- and Swing-based applets are valid.

The Applet Class 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 waits until an event occurs. The runtime 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. Instead, 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. If the user presses a key while the applet’s window has input focus, a keypress event is generated. When the user interacts with one of these controls, an event is generated.

An Applet Skeleton All but the most trivial 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. However, only very simple applets will not need to define all of them. AWT-based will also often override the paint( ) method, which is defined by the AWT Component class. This method is called when the applet’s output must be redisplayed. (Swing-based applets use a different mechanism to accomplish this task.)

import java. awt. ; import java. applet. ; / import java.awt.*; import java.applet.*; /* <applet code="AppletSkel" width=300 height=300> </applet> */ public class AppletSkel extends Applet{ public void init(){ //initialiazation } public void start(){ //start or resume execution

public void paint(){ //redisplay contents of window } public void stop(){ //suspends execution public void destroy(){ //perform shutdown activities

Applet Initialization and Termination It is important to understand the order in which the various methods shown in the skeleton are called. When an applet begins, the following methods are called, in this sequence: 1. init( ) 2. start( ) 3. paint( ) When an applet is terminated, the following sequence of method calls takes place: 1. stop( ) 2. destroy( )

init( ) start( ) paint( ) 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. start( ) The start( ) method is called after init( ). It is also called to restart an applet after it has been stopped. Whereas init( ) is called once—the first time an applet is loaded—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 an AWT-based applet’s output must be redrawn. This situation can occur for several reasons. 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. Whatever the cause, whenever the applet must redraw its output, paint( ) is called. 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. This context is used whenever output to the applet is required.

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 applet may be using. The stop( ) method is always called before destroy( ).

The HTML Applet Tag As mentioned earlier, at the time of this writing, Oracle recommends that the APPLET tag be used to manually start an applet when JNLP is not used. An applet viewer will execute each APPLET tag that it finds in a separate window, while web browsers will allow many applets on a single page. So far, we have been using only a simplified form of the APPLET tag. Now it is time to take a closer look at it.

<APPLET [CODEBASE =codebaseURL] CODE=appletfile [ALT=alternateText] [NAME=appletInstanceName] WIDTH=pixels HEIGHT=pixels [ALIGN=alignment] [VSPACE=pixels][HSPACE=pixels] >  [<PARAM NAME=AttributeName VALUE=AttributeValue>] ..... [HTML displayed in the absence of java] [/APPLET>

Passing Parameters to Applets As just discussed, the APPLET tag 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. Thus, for numeric and boolean values, you will need to convert their string representations into their internal formats. Here is an example that demonstrates passing parameters:

import java. applet. ; import java. awt. ; / import java.applet.*; import java.awt.*; /* <applet code="ParamDemo.class" width=300 height=100> <param name=fontName value=Courier> <param name=fontSize value=14> <param name=Leading value=2> <param name=accountEnabled value=true> </applet> */

public class ParamDemo extends Applet{ String fontName; int fontSize; float leading; boolean active; //Initialize the string to be displayed public void start(){ String param; fontName=getParameter("fontName"); if(fontName==null){ fontName="Not Found"; }

param=getParameter("fontSize"); try{ if(param!=null){ fontSize=Integer.parseInt(param); } else{ fontSize=0; }catch(NumberFormatException e){ fontSize=-1;

param=getParameter("leading"); try{ if(param. =null){ leading=Float param=getParameter("leading"); try{ if(param!=null){ leading=Float.valueOf(param).floatValue(); } else{ leading=0; }catch(NumberFormatException e){ leading=-1;

param=getParameter("accountEnabled"); if(param!=null) active=Boolean.valueOf(param).booleanValue(); } //Display parameters public void paint(Graphics g){ g.drawString("Font name:"+fontName,0,10); g.drawString("Font size:"+fontSize,0,26); g.drawString("Leading:"+leading,0,42); g.drawString("Account Active:"+active,0,58);

Practices import java.awt.*; import java.applet.*; /* <applet code="Sample" width=300 height=100> </applet> */ public class Sample extends Applet{ String msg; //set the foregorund and background colors public void init(){ setBackground(Color.cyan); setForeground(Color.red); msg="Inside init()--"; }

public void start(){ msg="Inside start()--"; } public void paint(Graphics g){ msg="Inside paint()"; g.drawString(msg,10,30);

Using the Status Window import java.awt.*; import java.applet.*; /* <applet code="StatusWindow" width=300 height=100> </applet> */ public class StatusWindow extends Applet{ public void init(){ setBackground(Color.cyan); } //Display msg in applet window public void paint(Graphics g){ g.drawString("This is in the applet window",10,30); showStatus("This is shown in the status window");

Graphics Drawing Lines Lines are drawn by means of the drawLine( ) method, shown here: void drawLine(int startX, int startY, int endX, int endY ) drawLine( ) displays a line in the current drawing color that begins at startX, startY and ends at endX, endY. Drawing Rectangles The drawRect( ) and fillRect( ) methods display an outlined and filled rectangle, respectively. They are shown here: void drawRect(int left, int top, int width, int height) void fillRect(int left, int top, int width, int height) The upper-left corner of the rectangle is at left, top. The dimensions of the rectangle are specified by width and height.

To draw a rounded rectangle, use drawRoundRect( ) or fillRoundRect( ), both shown here: void drawRoundRect(int left,int top,int width,int height,int xDiam,int yDiam) void fillRoundRect(int left,int top,int width,int height,int xDiam,int yDiam) Drawing Ellipses and Circles To draw an ellipse, use drawOval( ). To fill an ellipse, use fillOval( ). These methods are shown here: void drawOval(int left, int top, int width, int height) void fillOval(int left, int top, int width, int height)

Drawing Arcs Arcs can be drawn with drawArc( ) and fillArc( ), shown here: void drawArc(int left,int top,int width,int height,int startAngle,int sweepAngle) void fillArc(int left,int top,int width,int height,int startAngle,int sweepAngle)

Graphics Demo import java.applet.*; import java.awt.*; /* <applet code="GraphicsDemo.class" width=350 height=400> </applet> */ public class GraphicsDemo extends Applet{ public void paint(Graphics g){ //draw lines g.drawLine(0,0,100,90); g.drawLine(0,90,100,10); g.drawLine(40,25,250,80);

//Draw Rectangles g.drawRect(10,150,60,50); g.fillRect(100,150,60,50); g.drawRoundRect(190,150,60,50,15,15); g.fillRoundRect(280,150,60,50,30,40); //draw Ellipses and Circles g.drawOval(10,250,50,50); g.fillOval(90,250,75,50); g.drawOval(190,260,100,40); //Draw Arcs g.drawArc(10,350,70,70,0,180); g.fillArc(60,350,70,70,0,35); }