Vous êtes sur la page 1sur 8

Objectives Session 19

Define Java applets Differentiate between applets and applications Outline the life cycle of an applet Create applets Display images using applets Pass parameters to applets Understand the application of applets in GUI Use Graphics
Core Java / Session 19 / 1 of 16 Core Java / Session 19 / 2 of 16

Applets

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

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( )

Core Java / Session 19 / 3 of 16

Core Java / Session 19 / 4 of 16

Applet Structure (Contd)


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

Applet Compilation & Execution


An applet is compiled using the syntax
javac Applet1.java

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

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.
Core Java / Session 19 / 5 of 16

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

Applet can be executed using an applet viewer


Core Java / Session 19 / 6 of 16

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.
Core Java / Session 19 / 7 of 16

Security Restrictions on Applets


Cannot read or write files on the users 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 readers system Cannot load any programs stored on the users system
Core Java / Session 19 / 8 of 16

Applet Life Cycle

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>

Core Java / Session 19 / 9 of 16

Core Java / Session 19 / 10 of 16

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
Core Java / Session 19 / 11 of 16

Graphical Background
Methods for getting a graphical background:
getGraphics( ) repaint( ) update(Graphics g) paint(Graphics g)

Core Java / Session 19 / 12 of 16

Drawing Strings, Characters and Bytes


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

Drawing Lines and Ovals


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

Method to draw or print characters on the frame


Syntax drawChars(char array[ ], int offset, int length, int xCoor, int yCoor);

Methods used to draw ovals


Syntax drawOval(int xCoor, int yCoor, int width, int height); setColor(Color c); fillOval(int xCoor, int yCoor, int width, int height);

Method to draw or print bytes on the frame


Syntax drawBytes(byte array[ ], int offset, int length, int xCoor, int yCoor);
Core Java / Session 19 / 13 of 16

Core Java / Session 19 / 14 of 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);

Summary
Applet is a Java program that runs with the help of a browser Life cycle of an applet Difference between an applet and an application Security restrictions on applets Graphics class methods

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);

Core Java / Session 19 / 15 of 16

Core Java / Session 19 / 16 of 16

Objectives
Use Graphics, Colors and Fonts with applets:

Session 21

(Contd Contd Applets (Contd)

3D Rectangles PolyLines Polygons Color Control Font Control FontMetrics Class Paint Mode

Core Java / Session 19 / 17 of 16

Core Java / Session 19 / 18 of 16

3D Rectangles & Arcs


Methods used to draw 3D Rectangles and arcs
Syntax
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);

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));

Core Java / Session 19 / 19 of 16

Core Java / Session 19 / 20 of 16

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);

Color Control
Java uses the RGB color model The range of integer values that a color element can hold Element Red Green Blue Range 0-255 0-255 0-255

Syntax of the constructor to create custom color


color(int red, int green, int blue);
Core Java / Session 19 / 21 of 16 Core Java / Session 19 / 22 of 16

Color Control (Contd)


Table shows RGB values of common colors
Color White Light Gray Gray Dark Gray Black Pink Orange Yellow Magenta Red 255 192 128 64 0 255 255 255 255 Green 255 192 128 64 0 175 200 255 0 Blue 255 192 128 64 0 175 0 0 255
Core Java / Session 19 / 23 of 16

Font Control
java.awt package provides the class Font Methods of Font class:
getAllFont( ) getLocalGraphicsEnvironment( ) getFont( ) getFontList( )

Core Java / Session 19 / 24 of 16

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

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

Example
Font f1 = new Font("SansSerif", Font.ITALIC, 16); g.setFont(f1);
Core Java / Session 19 / 25 of 16 Core Java / Session 19 / 26 of 16

FontMetrics Class (Contd)


Methods:
getFontMetrics(f1) getHeight( ) getAscent( ) getDescent( ) getLeading( ) getName( )

Paint mode
Trong ho c hai ch v.
Ch XOR.

xo mt vt th ta v ln vt th gc vt th . Thit lp ch ho dng XOR, ta s dng hm:


setXORMode(Color)

Ch : Khi s dng ch ny ta phi nh ngha li hm update.

Core Java / Session 19 / 27 of 16

Core Java / Session 19 / 28 of 16

Summary
Methods to draw 3D Rectangles and Arcs, Polylines and Polygons Color and Font controls FontMetrics class and Paint mode

Core Java / Session 19 / 29 of 16

Vous aimerez peut-être aussi