Applets Applet is java program that can be
Applets ¢ Applet is java program that can be embedded into HTML pages. Java applets runs on the java enabled web browsers such as mozilla and internet explorer. ¢ Applet can't access system resources on the local computer. ¢ Applets are used to make the web site more dynamic and entertaining. ¢
Jdk provides us with a special tool called Applet. Viewer. ¢ Applet class is defined in java. applet package. ¢ Applet execution doesnot begin at main. ¢ Applet appear within a Html page & contain GUI controls like buttons , list , boxes etc. ¢ When user interacts with these controls an event is genetated. ¢
¢ ¢ ¢ init(): This method is called to initialized an applet. start(): This method is called after the initialization of the applet. It is called when user returns to the HTMl page. stop(): This method can be called multiple times in the life cycle of an Applet moves out of the HTMl page. destroy(): This method is called only once in the life cycle of the applet when applet is unloaded from the memory. paint(): it is called each time applets o/p is drawn
public void paint(Graphics g){ This method is drawing method. ¢ This method draws anything that is in the applet , to the screen. ¢ An instance(g) of the Class Graphics is passed to it. ¢ It takes a String and an x and a y coordinate as it’s argument. ¢
The repaint() Method The repaint() method is the one invoked by a program to do drawing. ¢ repaint() ==> update() ==> paint() ¢ repaint() does not invoke paint() directly. It schedules a call to an intermediate method, update(). ¢ Finally, update() calls paint(). ¢ update() ¢ Clears screen ¢ Sets back & foreground colors ¢ Invokes paint() method ¢
¢ public void show. Status(String msg) l ¢ URL get. Document. Base() l ¢ Outputs a message string in the "status window". Returns the URL of the HTML file in which the applet is embedded. URL get. Code. Base() l Returns the URL of the class file that contains applet.
The java. awt package “awt” stands for “Abstract Window Toolkit” ¢ The java. awt package includes classes for: ¢ Drawing lines and shapes l Drawing letters l Setting colors l Choosing fonts l ¢ If it’s drawn on the screen, then java. awt is probably involved.
Java’s coordinate system (0, 0) (50, 0) (0, 20) (50, 20) (w-1, h-1) ¢ ¢ ¢ 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
The Graphics class contains drawing methods ¢ draw. Line(x 1, y 1, x 2, y 2) g. draw. Line(100, 420, 312); ¢ draw. Rect(x, y, width, height); ¢ g. draw. Rect(72, 160, 200, 35); ¢ draw. Oval (x, y, width, height) ¢ g. draw. Oval(300, 120, 100, 75); ¢
draw. Arc (int x, int y, int width, int height, int start. Angle, int arc. Angle) l draw. Arc(100, 80, 60, 45, 120); l Draws the outline of a circular or elliptical arc covering the specified rectangle. l fill. Arc() l Fills a circular or elliptical arc covering the specified rectangle l draw. Polyline (int[] x. Points, int[] y. Points, int n. Points) l
l fill. Polygon(int[], int points), l draw. Polyline(int[], int points): Draws a closed polygon defined by arrays of x and y coordinates l draw 3 DRect(x, y, width, height, boolean raised): Draws a 3 -D highlighted outline of the specified rectangle. The edges of the rectangle are highlighted so that they appear to be beveled and lit from the upper left corner.
¢ ¢ ¢ ¢ g. draw. Line( x 1 , y 1 , x 2 , y 2 ); g. draw. Oval( left , top , width , height ); g. fill. Oval( left , top , width , height ); g. draw. Round. Rect( left , top , width , height ); g. fill. Round. Rect( left , top , width , height ); g. draw. Arc( left , top , width , height , start. Angle , arc. Angle ); g. draw. String( string , x , y );
Colors: To use a color, g. set. Color(Color. RED); ¢ ¢ ¢ ¢ The java. awt package defines a class named Color There are 13 predefined Color. BLACK Color. PINK Color. GREEN Color. RED Color. DARK_GRAY Color. CYAN Color. GRAY Color. ORANGE Color. BLUE Color. LIGHT_GRAY Color. YELLOW Color. WHITE Color. MAGENTA Every color is a mix of red, green, and blue To make your own colors: Amounts range from 0 to 255 new Color( red , green , blue ) Black is (0, 0, 0), white is (255, 255)
set. Background (Color c)-sets background color; ¢ set. Foreground (Color c)-sets foreground color; ¢ Color get. Background(); gets Background color; ¢ Color get. Foreground(); gets Foreground color; ¢
¢ ¢ ¢ Font (String name, int style, int size) Creates a new Font from the specified name, style and point size. PLAIN: 0 BOLD: 1 ITALIC: 2 To create a Font. Metrics object, where g is a Graphics object and f is a Font object: Font. Metrics fm = g. get. Font. Metrics(f);
¢ ¢ ¢ int get. Ascent() Determines the font ascent of the Font described by this Font. Metrics object. Int get. Descent() Determines the font descent of the Font described by this Font. Metrics object. Font get. Font() Gets the Font described by this Font. Metrics object. intget. Height() Gets the standard height of a line of text in this font. int get. Leading() Determines the standard leading of the Font described by this Font. Metrics object
- Slides: 18