Presentation is loading. Please wait.

Presentation is loading. Please wait.

APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can.

Similar presentations


Presentation on theme: "APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can."— Presentation transcript:

1 APPLET

2 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can be placed on a web page ◦ will be executed by the web browser ◦ give web pages “dynamic content”

3 Advantages of Applet There are many advantages of applet. They are as follows: It works at client side so less response time. Secured It can be executed by browsers running under many plateforms, including Linux, Windows, etc

4 4 Java Applets Java applets are usually graphical ◦ Draw graphics in a defined screen area ◦ Enable user interaction with GUI elements Applet class is one of the Abstract Windowing Toolkit (AWT) components

5 5 Java Applet Classes Abstract Windowing Toolkit AWT ◦ Earlier versions of Java ◦ Applet class is one of the AWT components

6 6 Java Applets Applets are Java programs that can be embedded in HTML documents ◦ To run an applet you must create a.html file which references the applet ◦ Ready to Program also will run an applet When browser loads Web page containing applet ◦ Applet downloads into Web browser ◦ begins execution Can be tested using appletviewer program

7 7 Applets Applet ◦ Program that runs in  appletviewer (test utility for applets)  Web browser (IE, Communicator) ◦ Executes when HTML (Hypertext Markup Language) document containing applet is opened ◦ Applications run in command windows

8 8 Applets and Web Pages – HTML Applets embedded in a web page ◦ Executed when web page loaded by browser Web pages structured with HTML codes ◦ H yper T ext M ark-up L anguage Syntax... Turns format on Turns the format off

9 9 Applets and Web Pages – HTML Embedding Java applets ◦ Insert applet tags Call the specific applet by its file name Where nnn and mmm are specific pixel sizes

10 10 Applets and Web Pages – HTML Create the web page code using a text editor Save it with an.html suffix Open this file with appletviewer or with a web browser that supports Java Java Plug-in must be installed (part of J2SDK 1.4.1 from Sun)

11 11 Applets and Web Pages – HTML Client Web browser anywhere can access this web page from its host server Embedded Java applet runs on client browser (of any type platform) This means a client anywhere on any type of platform can run a piece of software developed on any other type of platform Platform Independence

12 The following is a simple applet named HelloWorldApplet.java: import java.applet.*; import java.awt.*; public class HelloWorldApplet extends Applet { public void paint (Graphics g) { g.drawString ("Hello World", 25, 50); }

13 These import statements bring the classes into the scope of our applet class: java.applet.Applet. java.awt.Graphics. Without those import statements, the Java compiler would not recognize the classes Applet and Graphics, which the applet class refers to.

14 Invoking an Applet: An applet may be invoked by embedding directives in an HTML file and viewing the file through an applet viewer or Java- enabled browser. The tag is the basis for embedding an applet in an HTML file.

15 Below is an example that invokes the "Hello, World" applet: The Hello, World Applet If your browser was Java-enabled, a "Hello, World" message would appear here.

16 The code attribute of the tag is required. It specifies the Applet class to run. Width and height are also required to specify the initial size of the panel in which an applet runs. The applet directive must be closed with a tag.

17 Non-Java-enabled browsers do not process and. Therefore, anything that appears between the tags, not related to the applet, is visible in non-Java-enabled browsers.

18 18 Thinking About Objects Java an object-oriented language ◦ However, Java has constructs from structured programming Object orientation ◦ Natural way to think about world and writing computer programs  Object-oriented programming models the real world ◦ Attributes - properties of objects  Size, shape, color, weight, etc. ◦ Behaviors - actions that objects can perform  A ball rolls, bounces, inflates and deflates

19 19 Thinking About Objects Object orientation (continued) ◦ Inheritance  New classes of objects absorb characteristics of existing classes ◦ Information hiding  Objects usually do not know how other objects are implemented  We can drive cars without knowing how every part works internally

20 20 Thinking About Objects Class - unit of Java programming Java focuses on nouns (classes) ◦ C focuses on verbs and is action oriented Contain methods ◦ Implement behaviors Contain data ◦ Implement attributes Classes are reusable ◦ Standardized, interchangeable parts

21 21 A Simple Java Applet: Drawing a String a welcome message applet The.html code to run the applet in a browser The program output shown in the Applet Viewer

22 22 Running the Applet Compile ◦ Use Ready to Program ◦ If no errors, bytecodes stored in WelcomeApplet.class We must create an HTML file ◦ Loads the applet into appletviewer or a browser ◦ Ends in.htm or.html To execute an applet ◦ Create an HTML file indicating which applet the browser (or appletviewer ) should load and execute

23 23 Running the Applet - Alternatively Run from within Ready to Program Prompt for applet window size appears Applet window runs

24 24 import allows us to use predefined classes (allowing us to use applets and graphics, in this case). extends allows us to inherit the capabilities of class JApplet. Method paint is guaranteed to be called in all applets. Its first line must be defined as above.

25 25 Running An Applet import java.applet.Applet; import java.awt.Graphics; public class HelloApplet extends Applet { public void paint (Graphics g) { g.drawString ("Hello. Welcome to",25,25); g.drawString ("Java Programming",25,40); } import java.applet.Applet; import java.awt.Graphics; public class HelloApplet extends Applet { public void paint (Graphics g) { g.drawString ("Hello. Welcome to",25,25); g.drawString ("Java Programming",25,40); } Enter this text into your Ready to Program editor Compile the Java code Enter this text into your Ready to Program editor Compile the Java code

26 26 Running An Applet Now create an. html file to run the applet Save it as HelloApplet.html HelloApplet.html Make sure you save it in the same directory as the. java file

27 Lifecycle of Java Applet Applet is initialized. Applet is started. Applet is painted. Applet is stopped. Applet is destroyed.

28 Lifecycle methods for Applet: The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet. java.applet.Applet class For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet. public void init(): is used to initialized the Applet. It is invoked only once. public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet. public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized. public void destroy(): is used to destroy the Applet. It is invoked only once.

29 java.awt.Component class The Component class provides 1 life cycle method of applet. public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.

30 Passing parameters to an applet To pass parameters to an applet, two things are required- a special parameter tag in HTML file and the code in the applet to parse those parameters.

31 Import java.awt.*; Import java.applet.*; Public class ParamtestApplet extends Applet{ Font f=new Font(“Times Roamn”,Font.BOLD,40); String name; Public void init(){ name= getParameter(“name”); If(name==null) { name=“friend”; } Name=“Have a nice day”+name; } Public void Paint(Graphics g){ g.setFont(f); g.setColor(Color.darkGray); g.drawString(name,50,50); }

32 ParamtestApplet.html

33 Example of Applet by appletviewer tool: //First.java import java.applet.Applet; import java.awt.Graphics; public class First extends Applet { public void paint(Graphics g) { g.drawString("welcome to applet",150,150); } } /* */

34 To execute the applet by appletviewer tool, write in command prompt: c:\>javac First.java c:\>appletviewer First.java

35 import java.applet.Applet; import java.awt.*; public class GraphicsDemo extends Applet{ public void paint(Graphics g){ g.setColor(Color.red); g.drawString("Welcome",50, 50); g.drawLine(20,30,20,300); g.drawRect(70,100,30,30); g.fillRect(170,100,30,30); g.drawOval(70,200,30,30); g.setColor(Color.pink); g.fillOval(170,200,30,30); g.drawArc(90,150,30,30,30,270); g.fillArc(270,150,30,30,0,180); }

36 Methods used to load and display image in an applet drawImage(Image, int x, int y, ImageObserver): is used draw the specified image at the given coordinates.Image referes to picture to be drawn.x,y are the coordinates where the image is to drawn.Image observer is usually the applet itself. getImage(URL, String ):Loads an image from an URL.URL specifies the location from which the image is to be loaded.String is usually the name of the image file.

37 Helper functions in getImage() method getDocumentBase(): is used to return the URL of the HTML document in which applet is embedded. getCodeBase(): is used to return the path( URL) of the.class file.

38 import java.awt.*; import java.applet.*; public class DisplayImage extends Applet { Image picture; public void init() { picture = getImage(getCodeBase(),“rabbit.gif"); } public void paint(Graphics g) { g.drawImage(picture, 30,30, this); } }

39

40 getImage(getCodeBase(),“rabbit.gif"); Loads the file rabbit.gif from the directory where the.class file is located and stores it in the instance variable img. The paint() method of the applet,displays the image at the coordinates mentioned using drawImage() method. The parameter this(the applet itself) passed in the drawImage() function calls the image observer which monitors the drawing of the image.

41 Clippling Clipping is a technique by which the drawing area can be restricted to a small portion of the screen.A clipping region is an area where drwaing is allowed.

42 Clipper.java Import java.awt.*; Import java.applet.*; public class clipper extends Applet{ public void paint(Graphics g){ g.clipRect(10,10,150,100); G.setFont(new Font(“TimesRoman”,Font.ITALIC,28); g.fillOval(00,60,80); g.drawString(“Happy New Year”,50,30)

43 Clipper.html


Download ppt "APPLET. 2 Introduction to Java Applet Programs Applications are stand alone programs ◦ executed with Java interpreter Applet is a small program ◦ can."

Similar presentations


Ads by Google