Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations

Presentation on theme: "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."— Presentation transcript:

1 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 placed on applets by the browser’s JVM  What is JVM?  the fundamentals of HTML and how to create a simple Web page  how to write a simple applet and execute it from a Web page or with the applet viewer program

2 2 Introduction to Applets  There are two types of programs you can create with Java:  applications  applets.  An application is a stand-alone program that runs on your computer.  Applets are Java programs that are usually part of a Web site.  If a user opens the Web site with a Java- enabled browser, the applet is executed inside the browser window.

3 3 Introduction to Applets  It appears to the user that the applet is part of the Web site.  Applets are stored on a Web server along with the site’s Web pages.  Applets associated with a viewed web page are transmitted to the user’s system.  Once the applets are transmitted, the user’s system executes them.  Applets can be used to extend the capabilities of a Web page.

4 4 Introduction to Applets  Web pages are normally written in Hypertext Markup Language (HTML).  HTML is static content; whereas, applets are dynamic.  An applet does not have to be on a web server in order to be executed.  They can be stored on the local computer.

5 5 Applet Limitations  Applets run on the user’s system, NOT the server.  For security purposes, applets can not:  access the local computer file system,  run any other program on the user’s system.  execute operating system procedures.  retrieve information about the user or their system.  make network connections with any system except the server from which the applet was transmitted.  run anonymously.  If an applet displays a window, it will automatically have a message such as “Warning: Applet Window” displayed in it.

6 6 Introduction to HTML  Hypertext Markup Language (HTML) is the language that Web pages are written in.  Hypertext can contain a link to other content on the web page, or another web page.  A Markup Language allows you to “mark up” a text file by inserting special instructions.  These instructions tell the browser how to format the text and create any hypertext links.  To make a web page, create a text file:  that contains HTML instructions (known as tags),  the text that should be displayed on the Web page, and  typically has a.htm or.html file extension.

7 7 to HTML Introduction to HTML  This document is called an HTML document.  The tags instruct the browser:  how to format the text,  where to place images,  what to do when the user clicks on a link, etc.  Most HTML tags have an opening tag and a closing tag.  Text  Text  The tags are enclosed in angle brackets ( ).  The closing tag is preceded by a forward slash (/).

8 8 Document Structure Tags  The tag marks the beginning and ending of an HTML document.  The tag marks the document head, a section containing information about the document.  The document head contains the tag, which is contains the title of the document.

9 9 Document Structure Tags  After the document head comes the tag.  The document body contains all of the tags and text that produce output in the browser.

10 10 Creating Applets With Swing  Applets are very similar to the GUI applications.  Hmmmm, what’s a GUI???  Instead of displaying its own window, an applet appears in the browser’s window.  The applet code are:  The differences applet code are:  A GUI application class is derived from JFrame.  An applet class is derived from JApplet.  The JApplet class is part of the javax.swing package.

11 11 Creating Applets With Swing  A GUI application class has a constructor that creates other components and sets up the GUI.  An applet class does not normally have a constructor.  Instead, it has a method named init that performs the same operations as a constructor.  I know this is confusing, just remember that the two are different.  GUI Application class ≠ to Applet class

12 12 Creating Applets With Creating Applets With Swing  The differences applet code are (continued):  The following methods are not called in an applet:  super  setSize  setDefaultCloseOperation  pack  setVisible  No main method is needed to create an Applet object.  The browser creates an instance of the class automatically.  Example:   SimpleApplet.html Here are the print outs. SimpleApplet.html

13 13 Running an Applet  The process of running an applet is different from that of running an application.  To run an applet, create an HTML document with an APPLET tag, which has the following general format: <APPLET CODE="Filename.class" CODE="Filename.class" WIDTH=“width_value” WIDTH=“width_value” HEIGHT=“height_value”> HEIGHT=“height_value”>  Don’t forget the closing angle bracket.  Attributes should be enclosed in quotes.

14 14 Running an Applet  Filename.class is the compiled bytecode of the applet, not file.  You can optionally specify a path along with the file name.  If you specify only the file name, it is assumed that the file is in the same directory as the HTML  The browser:  loads specified byte code, and  executes it in an area that is the size specified by the width_value and height_value.

15 15 Using Appletviewer  The appletviewer program loads and executes an applet without the need for a Web browser.  When running the program, specify the name of an HTML document as a command line argument. appletviewer SimpleApplet.html  This command executes any applet referenced by an APPLET tag in the file SimpleApplet.html.  If the document has more than one APPLET tag, it will execute each applet in a separate window.  Let’s look at an example…..

16 16 Drawing on Panels  The paintComponent method serves the same purpose as the paint method.  When it is called, the component’s Graphics object is passed as an argument. public void paintComponent(Graphics g)  When overriding this method, first call the base class’s paintComponent method. super.paintComponent(g);

17 17 A Sample Graphics Applet  View sample program Listing 1.2 sample program sample program  class HappyFace (page 33) Sample screen output

18 18 A Sample Graphics Applet  The paint method specifies what is drawn in the applet.  The paint method is invoked automatically when the applet is run.

19 19 Screen Coordinate System  Figure 1.6

20 20 Screen Coordinate System  The x-coordinate is the number of pixels from the left.  The y-coordinate is the number of pixels from the top (not from the bottom).

21 21 Drawing Ovals and Circles  The drawOval method draws only the outline of the oval. canvas.drawOval(100, 50, 90, 50); canvas.drawOval(100, 50, 90, 50);  The fillOval method draws a filled-in oval. canvas.fillOval(100, 50, 90, 50); canvas.fillOval(100, 50, 90, 50);

22 22 Drawing Ovals & Circles  The drawOval and fillOval methods take four arguments.  The first two arguments indicate the upper-left corner of an invisible rectangle around the oval.  The last two arguments indicate the width and height of the oval.  A circle is just an oval whose height is the same as its width.

23 23 Drawing Ovals and Circles  Figure1.7 The Oval Drawn by canvas.drawOval(100, 50, 90, 50)

24 24 Size and Positions of Figures  Sizes and positions in a Java applet are given in pixels.  Think of the display surface for the applet as being a two-dimensional grid of individual pixels.

25 25 Drawing Arcs  The drawArc method draws an arc. drawArc(100, 50, 200, 200, 180, 180); drawArc(100, 50, 200, 200, 180, 180);  The drawArc method takes six arguments.  The first four arguments are the same as the four arguments needed by the drawOval method.  The last two arguments indicate where the arc starts, and the number of degrees through which is sweeps.  0 degrees is horizontal and to the right.

26 26 Specifying an Arc  Figure 1.8a

27 27 Specifying an Arc  Figure 1.8b

28 28 Running and Closing an Applet  There are two ways to run an applet:  Embed the applet in a Web page and run it  Use an applet viewer from the IDE .  There are two corresponding ways to end an applet:  If you are running the applet from a web site, close the page or navigate away from the page  If you are using an applet viewer, use the mouse to click the close-window button.

29 29 Your Turn  You will have your turn.  First, making the Happy face.  Then, Page 39 Exercise 8.  Now, for starting Chapter 2.

Download ppt "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."

Similar presentations

Ads by Google