Académique Documents
Professionnel Documents
Culture Documents
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( )
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
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)
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
Objectives
Use Graphics, Colors and Fonts with applets:
Session 21
3D Rectangles PolyLines Polygons Color Control Font Control FontMetrics Class Paint Mode
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));
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
Font Control
java.awt package provides the class Font Methods of Font class:
getAllFont( ) getLocalGraphicsEnvironment( ) getFont( ) getFontList( )
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
Paint mode
Trong ho c hai ch v.
Ch XOR.
Summary
Methods to draw 3D Rectangles and Arcs, Polylines and Polygons Color and Font controls FontMetrics class and Paint mode