Client Side Programming with Applet

27
IADCS Diploma Course Applets U Nyein Oo COO/Director(IT) Myanma Computer Co., Ltd

Transcript of Client Side Programming with Applet

Page 1: Client Side Programming with Applet

IADCS Diploma CourseApplets

U Nyein OoCOO/Director(IT)Myanma Computer Co., Ltd

Page 2: Client Side Programming with Applet

Copyright : MCC Java Applets 2

Java Applets

Is a Java program that runs with the help of a web browser

All applets are sub-classes of the class ‘Applet’ To create an applet, you need to import the following

two packages:– java.applet– java.awt

Page 3: Client Side Programming with Applet

Copyright : MCC Java Applets 3

Applet Structure

An applet defines its structure from four events that take place during the execution

For each event, a method is automatically called Methods

– init( ) – start( ) – stop( ) – destroy( )

Page 4: Client Side Programming with Applet

Copyright : MCC Java Applets 4

Applet Structure (Contd…)

Other related methods– paint( ) – repaint( )– showStatus( ) – getAppletInfo( )

Applet methods init(), start(), stop(), destroy(), and paint() are inherited by an applet

Each of these methods is empty by default. Hence, these methods are overridden

Page 5: Client Side Programming with Applet

Copyright : MCC Java Applets 5

Applet Compilation & Execution

An applet is compiled using the syntaxjavac Applet1.java

To execute an applet, create an HTML file which uses the applet tag – The applet tag has two attributes

Width Height

– To pass parameters to the applet, use the ‘param’ tag, followed by the ‘value’ tag

Applet can be executed using an applet viewer

Page 6: Client Side Programming with Applet

Copyright : MCC Java Applets 6

Example of Applet

Import java.applet.Applet;

Import java.awt.Graphics;

Public class HelloWorldApplet extends Applet{

public void paint(Graphics g){

g.drawString(“Hello World”,50,35);

}

}

Page 7: Client Side Programming with Applet

Copyright : MCC Java Applets 7

Example of Applet (Cont]

<html><head><title>Hello World</title></head><body>This is an applet<p><applet codebase=“classes”

code=“HelloWorldApplet.class” width=200 height=300></applet>

</body></html>

Page 8: Client Side Programming with Applet

Copyright : MCC Java Applets 8

Difference between an Applet and an Application

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

The execution of applications begins with the ‘main()’ method. This is not the case with applets

Applications use the ‘System.out.println()’ to display the output, while applets use the ‘drawstring()’ method to display the output

Page 9: Client Side Programming with Applet

Copyright : MCC Java Applets 9

Security Restrictions on Applets

Cannot read or write files on the user’s file system Cannot communicate with an Internet site, but only

with one that serves the web page, including the applet

Cannot run any programs on the reader’s system Cannot load any programs stored on the user’s

system

Page 10: Client Side Programming with Applet

Copyright : MCC Java Applets 10

Applet Life Cycle

Page 11: Client Side Programming with Applet

Copyright : MCC Java Applets 11

Passing parameters to an Applet

To pass parameter, the PARAM parameter is to be included in HTML tag

Example

<applet code = "Mybutton1" width = “100” height = “100”><PARAM NAME = “Mybutton” value = “Display Dialog”></applet>

Page 12: Client Side Programming with Applet

Copyright : MCC Java Applets 12

Graphics Class

Provided by AWT package Provides a collection of methods to draw the

following graphical figures– Oval – Rectangle– Square– Circle – Lines– Text in different fonts

Page 13: Client Side Programming with Applet

Copyright : MCC Java Applets 13

Graphical Background

Methods for getting a graphical background – getGraphics( ) – repaint( )– update(Graphics g)– paint(Graphics g)

Page 14: Client Side Programming with Applet

Copyright : MCC Java Applets 14

Drawing Strings, Characters and Bytes

Method to draw or print a string on the frameSyntax– drawString(String str, int xCoor, int yCoor);

Method to draw or print characters on the frame Syntax– drawChars(char array[ ], int offset, int length, int xCoor, int

yCoor);

Method to draw or print bytes on the frame Syntax– drawBytes(byte array[ ], int offset, int length, int xCoor, int

yCoor);

Page 15: Client Side Programming with Applet

Copyright : MCC Java Applets 15

Drawing Lines and Ovals

Method used to draw lines Syntax– drawLine(int x1, int y1, int x2, int y2);

Methods used to draw ovalsSyntax– drawOval(int xCoor, int yCoor, int width, int height);– setColor(Color c);– fillOval(int xCoor, int yCoor, int width, int height);

Page 16: Client Side Programming with Applet

Copyright : MCC Java Applets 16

Drawing Rectangles and Rounded Rectangles

Methods used to draw rectangles Syntax– drawRect(int xCoor, int yCoor, int width, int height);– fillRect(int xCoor, int yCoor, int width, int height);

Methods used to draw rounded rectangles Syntax– drawRoundRect(int xCoor, int yCoor, int width, int

height, int arcWidth, int arcHeight);– fillRoundRect (int xCoor, int yCoor, int width, int height,

int arcWidth, int arcHeight);

Page 17: Client Side Programming with Applet

Copyright : MCC Java Applets 17

3D Rectangles & Arcs

Methods used to draw 3D Rectangles and arcsSyntax

– draw3DRect(int xCoord, int yCoord, int width, int height, boolean raised);

– drawArc(int xCoord, int yCoord, int width, int height, int arcwidth, int archeight);

– fillArc(int xCoord, int yCoord, int width, int height, int arcwidth, int archeight);

Page 18: Client Side Programming with Applet

Copyright : MCC Java Applets 18

Drawing PolyLines

Methods used to draw a series of lines Syntax

– drawPolyline(int xArray[ ], int yArray[ ], int totalPoints);

– g.setFont(new Font("Times Roman", Font.BOLD,15));

Page 19: Client Side Programming with Applet

Copyright : MCC Java Applets 19

Drawing and Filling Polygons

Methods to draw and fill polygons Syntax

– drawPolygon(int x[ ], int y[ ], int numPoints);

– fillPolygon(int x[ ], int y[ ], int numPoints);

Page 20: Client Side Programming with Applet

Copyright : MCC Java Applets 20

Color Control

Java uses the RGB color model The range of integer values that a color element can

hold Element Range

Red 0-255

Green 0-255

Blue 0-255

Syntax of the constructor to create custom color color(int red, int green, int blue);

Page 21: Client Side Programming with Applet

Copyright : MCC Java Applets 21

Color Control (Contd…) Table shows RGB values of common colors

Color Red Green Blue

White 255 255 255

Light Gray 192 192 192

Gray 128 128 128

Dark Gray 64 64 64

Black 0 0 0

Pink 255 175 175

Orange 255 200 0

Yellow 255 255 0

Magenta 255 0 255

Page 22: Client Side Programming with Applet

Copyright : MCC Java Applets 22

Font Control

java.awt package provides the class ‘Font’ Methods of Font class

– getAllFont( ) – getLocalGraphicsEnvironment( ) – getFont( )– getFontList( )

Page 23: Client Side Programming with Applet

Copyright : MCC Java Applets 23

Font Control (Contd…)

Font constructor takes the following three parameters – Name of the font in string format. This can be

obtained from the getFontList( ) method– Style of the font. For example, Font.BOLD,

Font.PLAIN, Font.ITALIC– Size in int form

Example

Font f1 = new Font("SansSerif", Font.ITALIC, 16);g.setFont(f1);

Page 24: Client Side Programming with Applet

Copyright : MCC Java Applets 24

FontMetrics Class

Measures various characters present in different fonts

Measurement includes the ‘height’, ‘baseline’, ‘ascent’, ‘descent’ and ‘leading’ of a font

It cannot be instantiated as it is an abstract class Methods

– getFontMetrics(f1) – getHeight( ) – getAscent( ) – getDescent( ) – getLeading( ) – getName( )

Page 25: Client Side Programming with Applet

Copyright : MCC Java Applets 25

Paint mode

Objects are drawn using the paint mode set Method used to make old and new contents visible

on the screensetXORMode(Color c)

Method used to revert to the overwrite modesetPaintMode( )

Page 26: Client Side Programming with Applet

Copyright : MCC Java Applets 26

Exercise 1

//Applet Exercise Onepublic class Applet1 extends Applet{ int num;

public void init(){num=6;

}public void paint(Graphics g){

g.drawString(“Hello to Applets. Chapter “+ num, 70, 80); showStatus(getAppletInfo());

}public String getAppletInfo() {// user overrides

return “Created by Aptech”;}

}

Page 27: Client Side Programming with Applet

Copyright : MCC Java Applets 27

Exercise 2

import java.applet.Applet;import java.awt.*;/* <applet code = “both” width = 200 height = 100 ></applet>*/public class both extends Applet { Button btn; public void init() { btn = new Button(“Click”); } public void paint(Graphics g) { g.drawString(“Applet”, 70, 50 ); } public static void main(String args[]) { both app=new both(); app.init(); System.out.println(“Application Main”); }}