Client Side Programming with Applet

5,263 views
5,168 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
5,263
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
129
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Client Side Programming with Applet

  1. 1. IADCS Diploma Course Applets U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd
  2. 2. Java Applets <ul><li>Is a Java program that runs with the help of a web browser </li></ul><ul><li>All applets are sub-classes of the class ‘Applet’ </li></ul><ul><li>To create an applet, you need to import the following two packages: </li></ul><ul><ul><li>java.applet </li></ul></ul><ul><ul><li>java.awt </li></ul></ul>
  3. 3. Applet Structure <ul><li>An applet defines its structure from four events that take place during the execution </li></ul><ul><li>For each event, a method is automatically called </li></ul><ul><li>Methods </li></ul><ul><ul><li>init( ) </li></ul></ul><ul><ul><li>start( ) </li></ul></ul><ul><ul><li>stop( ) </li></ul></ul><ul><ul><li>destroy( ) </li></ul></ul>
  4. 4. Applet Structure (Contd…) <ul><li>Other related methods </li></ul><ul><ul><li>paint( ) </li></ul></ul><ul><ul><li>repaint( ) </li></ul></ul><ul><ul><li>showStatus( ) </li></ul></ul><ul><ul><li>getAppletInfo( ) </li></ul></ul><ul><li>Applet methods init(), start(), stop(), destroy(), and paint() are inherited by an applet </li></ul><ul><li>Each of these methods is empty by default. Hence, these methods are overridden </li></ul>
  5. 5. Applet Compilation & Execution <ul><li>An applet is compiled using the syntax </li></ul><ul><ul><li>javac Applet1.java </li></ul></ul><ul><li>To execute an applet, create an HTML file which uses the applet tag </li></ul><ul><ul><li>The applet tag has two attributes </li></ul></ul><ul><ul><ul><li>Width </li></ul></ul></ul><ul><ul><ul><li>Height </li></ul></ul></ul><ul><ul><li>To pass parameters to the applet, use the ‘param’ tag, followed by the ‘value’ tag </li></ul></ul><ul><li>Applet can be executed using an applet viewer </li></ul>
  6. 6. Example of Applet <ul><li>Import java.applet.Applet; </li></ul><ul><li>Import java.awt.Graphics; </li></ul><ul><li>Public class HelloWorldApplet extends Applet{ </li></ul><ul><li>public void paint(Graphics g){ </li></ul><ul><li>g.drawString(“Hello World”,50,35); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  7. 7. Example of Applet (Cont] <ul><li><html> </li></ul><ul><li><head> </li></ul><ul><li><title>Hello World</title> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li>This is an applet<p> </li></ul><ul><li><applet codebase=“classes” code=“HelloWorldApplet.class” width=200 height=300></applet> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  8. 8. Difference between an Applet and an Application <ul><li>Applications run using the Java interpreter, while applets run on any browser that supports Java, or use an ‘AppletViewer’ that is included in the JDK </li></ul><ul><li>The execution of applications begins with the ‘main()’ method. This is not the case with applets </li></ul><ul><li>Applications use the ‘System.out.println()’ to display the output, while applets use the ‘drawstring()’ method to display the output </li></ul>
  9. 9. Security Restrictions on Applets <ul><li>Cannot read or write files on the user’s file system </li></ul><ul><li>Cannot communicate with an Internet site, but only with one that serves the web page, including the applet </li></ul><ul><li>Cannot run any programs on the reader’s system </li></ul><ul><li>Cannot load any programs stored on the user’s system </li></ul>
  10. 10. Applet Life Cycle
  11. 11. Passing parameters to an Applet <ul><li>To pass parameter, the PARAM parameter is to be included in HTML tag </li></ul><ul><li>Example </li></ul><applet code = &quot;Mybutton1&quot; width = “100” height = “100”> <PARAM NAME = “Mybutton” value = “Display Dialog”> </applet>
  12. 12. Graphics Class <ul><li>Provided by AWT package </li></ul><ul><li>Provides a collection of methods to draw the following graphical figures </li></ul><ul><ul><li>Oval </li></ul></ul><ul><ul><li>Rectangle </li></ul></ul><ul><ul><li>Square </li></ul></ul><ul><ul><li>Circle </li></ul></ul><ul><ul><li>Lines </li></ul></ul><ul><ul><li>Text in different fonts </li></ul></ul>
  13. 13. Graphical Background <ul><li>Methods for getting a graphical background </li></ul><ul><ul><li>getGraphics( ) </li></ul></ul><ul><ul><li>repaint( ) </li></ul></ul><ul><ul><li>update(Graphics g) </li></ul></ul><ul><ul><li>paint(Graphics g) </li></ul></ul>
  14. 14. Drawing Strings, Characters and Bytes <ul><li>Method to draw or print a string on the frame </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawString(String str, int xCoor, int yCoor); </li></ul></ul><ul><li>Method to draw or print characters on the frame </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawChars(char array[ ], int offset, int length, int xCoor, int yCoor); </li></ul></ul><ul><li>Method to draw or print bytes on the frame </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawBytes(byte array[ ], int offset, int length, int xCoor, int yCoor); </li></ul></ul>
  15. 15. Drawing Lines and Ovals <ul><li>Method used to draw lines </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawLine(int x1, int y1, int x2, int y2); </li></ul></ul><ul><li>Methods used to draw ovals </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawOval(int xCoor, int yCoor, int width, int height); </li></ul></ul><ul><ul><li>setColor(Color c); </li></ul></ul><ul><ul><li>fillOval(int xCoor, int yCoor, int width, int height); </li></ul></ul>
  16. 16. Drawing Rectangles and Rounded Rectangles <ul><li>Methods used to draw rectangles </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawRect(int xCoor, int yCoor, int width, int height); </li></ul></ul><ul><ul><li>fillRect(int xCoor, int yCoor, int width, int height); </li></ul></ul><ul><li>Methods used to draw rounded rectangles </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawRoundRect(int xCoor, int yCoor, int width, int height, int arcWidth, int arcHeight); </li></ul></ul><ul><ul><li>fillRoundRect (int xCoor, int yCoor, int width, int height, int arcWidth, int arcHeight); </li></ul></ul>
  17. 17. 3D Rectangles & Arcs <ul><li>Methods used to draw 3D Rectangles and arcs </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>draw3DRect(int xCoord, int yCoord, int width, int height, boolean raised); </li></ul></ul><ul><ul><li>drawArc(int xCoord, int yCoord, int width, int height, int arcwidth, int archeight); </li></ul></ul><ul><ul><li>fillArc(int xCoord, int yCoord, int width, int height, int arcwidth, int archeight); </li></ul></ul>
  18. 18. Drawing PolyLines <ul><li>Methods used to draw a series of lines </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawPolyline(int xArray[ ], int yArray[ ], int totalPoints); </li></ul></ul><ul><ul><li>g.setFont(new Font(&quot;Times Roman&quot;, Font.BOLD,15)); </li></ul></ul>
  19. 19. Drawing and Filling Polygons <ul><li>Methods to draw and fill polygons </li></ul><ul><ul><li>Syntax </li></ul></ul><ul><ul><li>drawPolygon(int x[ ], int y[ ], int numPoints); </li></ul></ul><ul><ul><li>fillPolygon(int x[ ], int y[ ], int numPoints); </li></ul></ul>
  20. 20. Color Control <ul><li>Java uses the RGB color model </li></ul><ul><li>The range of integer values that a color element can hold </li></ul><ul><ul><li>Syntax of the constructor to create custom color </li></ul></ul><ul><ul><ul><li>  color(int red, int green, int blue); </li></ul></ul></ul>0-255 Green 0-255 Blue 0-255 Red Range Element
  21. 21. Color Control (Contd…) <ul><li>Table shows RGB values of common colors </li></ul>255 0 255 Magenta 0 255 255 Yellow 0 200 255 Orange 175 175 255 Pink 0 0 0 Black 64 64 64 Dark Gray 128 128 128 Gray 192 192 192 Light Gray 255 255 255 White Blue Green Red Color
  22. 22. Font Control <ul><li>java.awt package provides the class ‘Font’ </li></ul><ul><li>Methods of Font class </li></ul><ul><ul><li>getAllFont( ) </li></ul></ul><ul><ul><li>getLocalGraphicsEnvironment( ) </li></ul></ul><ul><ul><li>getFont( ) </li></ul></ul><ul><ul><li>getFontList( ) </li></ul></ul>
  23. 23. Font Control (Contd…) <ul><li>Font constructor takes the following three parameters </li></ul><ul><ul><li>Name of the font in string format. This can be obtained from the getFontList( ) method </li></ul></ul><ul><ul><li>Style of the font. For example, Font.BOLD, Font.PLAIN, Font.ITALIC </li></ul></ul><ul><ul><li>Size in int form </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>Font f1 = new Font(&quot;SansSerif&quot;, Font.ITALIC, 16); </li></ul></ul><ul><ul><li>g.setFont(f1); </li></ul></ul>
  24. 24. FontMetrics Class <ul><li>Measures various characters present in different fonts </li></ul><ul><li>Measurement includes the ‘height’, ‘baseline’, ‘ascent’, ‘descent’ and ‘leading’ of a font </li></ul><ul><li>It cannot be instantiated as it is an abstract class </li></ul><ul><li>Methods </li></ul><ul><ul><li>getFontMetrics(f1) </li></ul></ul><ul><ul><li>getHeight( ) </li></ul></ul><ul><ul><li>getAscent( ) </li></ul></ul><ul><ul><li>getDescent( ) </li></ul></ul><ul><ul><li>getLeading( ) </li></ul></ul><ul><ul><li>getName( ) </li></ul></ul>
  25. 25. Paint mode <ul><li>Objects are drawn using the paint mode set </li></ul><ul><li>Method used to make old and new contents visible on the screen </li></ul><ul><ul><li>setXORMode(Color c) </li></ul></ul><ul><li>Method used t o revert to the overwrite mode </li></ul><ul><ul><li>setPaintMode( ) </li></ul></ul>
  26. 26. Exercise 1 <ul><li>//Applet Exercise One </li></ul><ul><li>public class Applet1 extends Applet{ </li></ul><ul><li>int num; </li></ul><ul><li>public void init(){ </li></ul><ul><li>num=6; </li></ul><ul><li>} </li></ul><ul><li>public void paint(Graphics g){ </li></ul><ul><li>g.drawString(“Hello to Applets. Chapter “+ num, 70, 80); </li></ul><ul><li> showStatus(getAppletInfo()); </li></ul><ul><li>} </li></ul><ul><li>public String getAppletInfo() {// user overrides </li></ul><ul><li> return “Created by Aptech”; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  27. 27. Exercise 2 <ul><li>import java.applet.Applet; </li></ul><ul><li>import java.awt.*; </li></ul><ul><li>/* </li></ul><ul><li><applet code = “both” width = 200 height = 100 > </li></ul><ul><li></applet> </li></ul><ul><li>*/ </li></ul><ul><li>public class both extends Applet { </li></ul><ul><li>Button btn; </li></ul><ul><li>public void init() { </li></ul><ul><li> btn = new Button(“Click”); </li></ul><ul><li>} </li></ul><ul><li>public void paint(Graphics g) { </li></ul><ul><li>g.drawString(“Applet”, 70, 50 ); </li></ul><ul><li>} </li></ul><ul><li>public static void main(String args[]) { </li></ul><ul><li>both app=new both(); </li></ul><ul><li>app.init(); </li></ul><ul><li>System.out.println(“Application Main”); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>

×