Download Applets in java programming and more Slides Java Programming in PDF only on Docsity!
Java Applet
Java Applet
- (^) Applet is a special type of program that is embedded in the webpage to generate the dynamic content.
- (^) It runs inside the browser and works at client side.
- (^) An applet is a Java program that runs in a Web browser.
Drawback of Applet:
- (^) Plugin is required at client browser to execute applet.
- (^) Java Plug-in extends the functionality of a web browser , allowing applets or Java Beans.
Lifecycle of Java Applet /Architecture/Skeleton
- Applet is initialized.
- Applet is started.
- Applet is painted.
- Applet is stopped.
- Applet is destroyed.
java.awt.Component class :
- (^) The Component class provides 1 life cycle method of applet.
- (^) public void paint(Graphics g): is used to paint the Applet.
- (^) It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.
Life cycle program:
- (^) import java.awt.*;
- (^) import java.applet.*;
- (^) /*
- */
- public class AppletTest extends Applet
- {
- public void init() { //initialization }
- (^) public void start () { //start or resume execution }
- (^) public void stop() { //suspend execution }
- (^) public void destroy() { //perform shutdown activity }
- (^) public void paint (Graphics g) { //display the content of window }
- (^) }
Program:
- (^) import java.applet.Applet;
- (^) import java.awt.Graphics;
- (^) /*
- (^)
- (^) */
- (^) public class First extends Applet{
- (^) public void paint(Graphics g){
- (^) g.drawString("welcome",150,150);
- (^) }
- (^) }
APPLET DISPLAY METHODS:
- void drawstring(String message, int x, int y)
- void setBackground(Color newColor)
- void setForeground(Color newColor)
- (^) To set the background color of an applet’s window, we use setBackground( ).
- (^) To set the foreground color, we use setForeground().
- (^) These methods are defined by Component, and have the general forms: void setBackground(Color newColor) void setForeground(Color newColor) Here, newColor specifies the new color.
- (^) The class Color defines the following values that can be used to specify colors:
- (^) Color.black Color.magenta Color.blue Color.orange Color.cyan Color.pink Color.darkGray Color. red Color.gray Color.white Color. green Color.yellow Color.lightGray
- (^) For example, this sets the background color to blue and the text color to yellow: 2. setBackground(Color.blue);
- (^) 3. setForeground(Color.yellow);
Requesting Repaint:
- (^) The repaint( ) method is defined by the AWT.
- (^) whenever your applet needs to update the information displayed in its window, it simply calls repaint().
- (^) It has defined as follows:
- void repaint( )
- void repaint(int left , int top , int width , int height )
Repaint() program:
import java.awt.; import java.applet.; /* */ public class SampleBanner extends Applet implements Runnable{ String msg = "A Simple Moving Banner."; Thread t = null; int state; boolean stopFlag; public void init() { setBackground(Color.cyan); setForeground(Color.red); }
public void stop() { stopFlag = true; t = null; } public void paint(Graphics g) { g.drawString(msg, 50, 30); showStatus("This is shown in the status window."); } }
Passing Parameters to Applets :
- (^) We can get any information from the HTML file as a parameter. For this purpose, Applet class provides a method named getParameter(). Syntax: public String getParameter(String parameter Name) The get Parameter() method of the Applet class can be used to retrieve the parameters passed from the HTML page