Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document.

Similar presentations


Presentation on theme: "1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document."— Presentation transcript:

1 1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document to host an applet Applet attributes Passing parameters to applets Understand the applet life cycle More Examples

2 2 A Simple Applet

3 3 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 modern Java applets, old browsers need an up-to-date Java plugin appletviewer is a program that can run An application is a Java program that runs all by itself

4 4 Application vs. Applet Application Trusted (i.e., has full access to system resources) Invoked by Java Virtual Machine (JVM, java ), e.g., java HelloWorld Should contain a main method, i.e., public static void main(String[]) Applet Not trusted (i.e., has limited access to system resource to prevent security breaches) Invoked automatically by the web browser Should be a subclass of class java.applet.Applet

5 5 Applets cannot Read or write to the local computer’s file system If this were not the case, applets could potentially read sensitive data (credit card info!) or delete important files Applets cannot find information about the local computer. For example user names and email addresses Applets cannot run a local executable program (for obvious reasons!)

6 6 Applets cannot Communicate with any host except its originating host Applets can only phone home! If this were not the case, applets could access web pages behind corporate firewalls They could then read potentially sensitive company information

7 7 How Applet works

8 8 Building a Java Applet Following piece of code is required: // An applet to print Hello World! // 1. import java.awt.Graphics; 2. import java.applet.Applet; 3. public class HelloWorld extends Applet { 4. public void paint (Graphics g ) { 5. g.drawString("Hello World!" 50, 25); } }

9 9 Building a Java Applet Edit → Save → Compile Edit the code in the same fashion as an Application The name of the applet will be same as the public class, here HelloWorld.java The program can be compiled in the same fashion as an Application is compiled. That is, javac HelloWorld.java After successful compilation, t he javac will produce a file named HelloWorld.class

10 10 Building a Java Applet Execution Edit an HTML file to host the applet just created. The HTML file will look like as: Save this to file giving a file name HelloJava.html Note: The name of the file not necessary be the same as the name of the class; But extension should be same as the.html Now the applet is ready for its execution! To run with appletviewer type the following: appletviewer HelloJava.html

11 21-Dec-15 Java Applet Skeleton /* Program MyFirstApplet An applet that displays the text "I Love Java" and a rectangle around the text. */ import java.applet.*; import java.awt.*; public class HelloWorld extends Applet { public void paint( Graphics g) { g.drawString(“HelloWorld",50,25); } Comment Import Statements Class Name Method Body

12 12 The Applet class To create an applet, you must import the Applet class This class is in the java.applet package The Applet class contains code that works with a browser to create a display window Capitalization matters! applet and Applet are different names

13 13 The java.awt package “awt” stands for “Abstract Window Toolkit” The java.awt package includes classes for: Drawing lines and shapes Drawing letters Setting colors Choosing fonts If it’s drawn on the screen, then java.awt is probably involved!

14 14 The paint method Our applet is going to have a method to paint some colored rectangles on the screen This method must be named paint paint needs to be told where on the screen it can draw This will be the only parameter it needs paint doesn’t return any result

15 15 The paint method, part 2 public void paint(Graphics g) { … } public says that anyone can use this method void says that it does not return a result A Graphics (short for “Graphics context”) is an object that holds information about a painting It remembers what color you are using It remembers what font you are using You can “paint” on it (but it doesn’t remember what you have painted)

16 16 Colors The java.awt package defines a class named Color There are 13 predefined colors—here are their fully- qualified names: For compatibility with older programs (before the naming conventions were established), Java also allows color names in lowercase: Color.black, Color.darkGray, etc. Color.BLACKColor.PINK Color.GREEN Color.DARK_GRAYColor.REDColor.CYAN Color.GRAYColor.ORANGEColor.BLUE Color.LIGHT_GRAYColor.YELLOW Color.WHITE Color.MAGENTA

17 17 New colors Every color is a mix of red, green, and blue You can make your own colors: new Color( red, green, blue ) Amounts range from 0 to 255 Black is (0, 0, 0), white is (255, 255, 255) We are mixing lights, not pigments Yellow is red + green, or (255, 255, 0)

18 18 Setting a color To use a color, we tell our Graphics g what color we want: g.setColor(Color.RED); g will remember this color and use it for everything until we tell it some different color

19 19 Pixels A pixel is a picture (pix) element one pixel is one dot on your screen there are typically 72 to 90 pixels per inch java.awt measures everything in pixels

20 20

21 21 Java’s coordinate system Java uses an (x, y) coordinate system (0, 0) is the top left corner (50, 0) is 50 pixels to the right of (0, 0) (0, 20) is 20 pixels down from (0, 0) (w - 1, h - 1) is just inside the bottom right corner, where w is the width of the window and h is its height (0, 0) (0, 20) (50, 0) (50, 20) (w-1, h-1)

22 22 Drawing rectangles There are two ways to draw rectangles: g.drawRect( left, top, width, height ); g.fillRect( left, top, width, height );

23 23 Some more java.awt methods g.drawLine( x1, y1, x2, y2 ); g.drawOval( left, top, width, height ); g.fillOval( left, top, width, height ); g.drawRoundRect( left, top, width, height ); g.fillRoundRect( left, top, width, height ); g.drawArc( left, top, width, height, startAngle, arcAngle ); g.drawString( string, x, y );

24 24 The complete applet import java.applet.Applet; import java.awt.*; // Applet example public class Drawing extends Applet { public void paint(Graphics g) { g.setColor(Color.BLUE); g.fillRect(20, 20, 50, 30); g.setColor(Color.RED); g.fillRect(50, 30, 50, 30); } }

25 25 The HTML page You can only run an applet in an HTML page The HTML looks something like this: DrawingApplet Applet

26 26 Passing Parameter to Applet Corresponding HTML document containing this applet and providing parameter values will be :

27 27 Passing Parameter to Applet // Use of init( ) to pass value through HTML to applet // import java.awt. *; import java.applet. * ; public class RectangleTest extends applet { int x, y, w, h; public void init ( ) { x = Integer.parseInt(getParameter (" xValue" )); y = Integer.parseInt(getParameter (" yValue" )); w = Integer.parseInt(getParameter (" wValue" )); h = Integer.parseInt(getParameter (" hValue" )); } public void paint ( Graphics g ) { g.drawRect (x, y, w, h ); }

28 28 import java.awt.Graphics; import java.awt.Font; import java.awt.Color; import java.applet.Applet; public class HelloWorldApplet1 extends Applet { Font f = new Font("TimesRoman", Font.BOLD, 36); String name,greeting; public void init() { name = ”Peter Norton"; greeting = new String("Hello " + name + "!"); } public void paint(Graphics g) { g.setFont(f); g.setColor(Color.red); g.drawString(greeting, 5, 40); } } Font

29 29 The Life-Cycle of Applet init() Called exactly once in an applet ’ s life. Called when applet is first loaded, which is after object creation, e.g., when the browser visits the web page for the first time. Used to read applet parameters, start downloading any other images or media files, etc.

30 30 Applet Life-Cycle (Cont.) start() Called at least once. Called when an applet is started or restarted, i.e., whenever the browser visits the web page. stop() Called at least once. Called when the browser leaves the web page.

31 31 Applet Life-Cycle (Cont.) destroy() Called exactly once. Called when the browser unloads the applet. Used to perform any final clean-up. init destroy start stop start

32 32 Browser Calling Applet Methods

33 33 Applet Methods public void init() public void start() public void stop() public void destroy() public URL getCodeBase() public URL getDocumentBase() public AudioClip getAudioClip(URL url, String name) public Image getImage(URL url, String name)

34 34 Summary An applet is a Java class Its code is downloaded from a web server It runs in the browser’s environment on the client host It is invoked by a browser when it scans a web page and encounters a class specified with the APPLET tag For security reason, the execution of an applet is normally subject to restrictions: applets cannot access files in the file system on the client host Applets cannot make network connection exception to the server host from which it originated

35 35 The End

36 36 The Applet Class (Paint)

37 37 The Applet Class (Repaint) The program determines that either part or all of a component needs to be repainted in response to some internal state change. The program invokes repaint() on the component, which registers an asynchronous request to the AWT that this component needs to be repainted.

38 38 The Applet Class (Update) When the applet invokes repaint(), to request its drawing area be repainted, AWT (Abstract Window Toolkit) invokes update(). The inherited update() method clears the applet's drawing area to its background color before invoking paint().

39 39 Flickering Flickering invariably occurs with animation. The reason behind flickering is the way Java paints and repaints each single frame of the applet. Calling repaint() results in a call to update() which in turn results in a call to paint(). Flickering is caused by update().

40 40 update() and Flickering update() performs two functions: It fills the screen with the current background color of the applet. This is what is normally termed as “clearing” the screen. It calls paint() which then draws the contents of the current frame onto the screen. Flickering is a result of the quick alternation between the above two functions.

41 41 Quick Alternation of Functions The quick alternation of the parts of the frame that do not change between clearing the screen and drawing the current contents of the frame, will cause flickering. In essence, what we see in quick successions are the screen background color and the current contents of the frame (being displayed several times per seconds).

42 42 Avoiding Flickering The major two ways of avoiding (reducing) flickering in Java applets are: Overriding update() so as not to clear the screen at all or to clear only the parts of the screen that have been changed since the last alternation. Double-buffering which is achieved by overriding both: update() and paint(). It consists in drawing the current contents on a graphics surface that is not on the applet screen and then copying the surface on the applet screen.

43 43 Overriding update() update() is a method in the Component class Recall that update() clears the screen and calls paint(). To avoid clearing the screen, we simply override update() by adding update() to the “flickering program”, in which we only have a call to paint(). Thus the clearing task of the original update() is eliminated.

44 44 Overriding update() (cont.) Thus instead of the update() method of Component in which we have: public void update(Graphics g) { // code for clearing the screen paint(g); } we’ll simply have: public void update(Graphics g) { paint(g); }

45 45 Double-Buffering Double buffering consists: in creating a second surface (an offscreen buffer) on which we perform all the drawing (and clearing) and then the contents of the offscreen buffer are transferred (“blitted”: bit block transferred) onto the applet’s surface. It is called double buffering because we are switching between two drawing buffers.

46 46 Five Steps of Double-Buffering The five steps of double buffering are: 1) Create an offscreen buffer by declaring an Image object (offScreenImage) to hold the image and a Graphics object (offScreenGraphics) to hold the graphics context. 2) Create an image and a graphics context for the offscreen buffer (usually done by overriding init()).

47 47 Five Steps of Double-Buffering 3) Perform all the painting on the offscreen buffer instead of the applet’s surface (i.e., instead of the main graphics buffer). It is usually done by overriding paint(). 4) Copy the contents of the offscreen buffer to the applet’s surface.

48 48 Five Steps of Double-Buffering 5) Use dispose() to clean up the graphics context that was created for the offscreen buffer. It is usually done by overriding destroy(): public void destroy() { offScreenGraphics.dispose(); }


Download ppt "1 Contents Introduction Applet Vs Application Security Restrictions on Applet A simple example “Hello World!” applet Compiling & Running Applet HTML document."

Similar presentations


Ads by Google