Applet Programming Java
Applet Programming Java
Applet Programming Java
properly and also ensure that either Java appletviewer or a Java enabled
applet are:
• start(): The start() method contains the actual code of the applet
invoked.
• stop(): The stop() method stops the execution of the applet. The stop ()
one tab to another in the browser. When we go back to that page, the start()
• destroy(): The destroy() method destroys the applet after its work is done. It is
invoked when the applet window is closed or when the tab containing the
webpage is closed. It removes the applet object from memory and is executed
• paint(): The paint() method belongs to the Graphics class in Java. It is used to
draw shapes like circle, square, trapezium, etc., in the applet. It is executed
after the start() method and when the browser or applet windows are resized.
Applet Tag
• The <applet>….</applet> tag is used inside the <body>….</body> tag of the
• For example
• <applet
code = helloJava.class
width=400
height=200>
</applet>
This HTML code tells the browser to load the compiled java applet
helloJava.class, which is in the same directory as the HTML file. It specifies the
display are of the applet output as 400 pixels width and 200 pixels height.
• //First.java
• import java.applet.Applet;
• import java.awt.Graphics;
• public class First extends Applet{
•
• public void paint(Graphics g){
• g.drawString("welcome",150,150);
• }
•
• }
myapplet.html
• <html>
• <head>
• <title> Welcome to java applets</title>
• </head>
• <body>
• <applet code="First.class" width="300" height="300">
• </applet>
• </body>
• </html>
Running the applet
• To run an applet, we require one of the following tools:
– Java enabled web browser
– Java appletviewer
• If we use a java enabled web browser, we will be able to see the
entire web page containing the applet. If we use the
appletviewer tool, we will see the applet output. The
appletviewer is available as part of the JDK. We can use it to run
the applet as follows:
appletviewer myapplet.html
Different attributes of the applet tag
Attribute Values Description
• public abstract void drawString(String str, int x, int y): is used to draw the
specified string.
• public void drawRect(int x, int y, int width, int height): draws a rectangle
• public abstract void fillRect(int x, int y, int width, int height): is used to fill
rectangle with the default color and specified width and height.
• public abstract void drawOval(int x, int y, int width, int height): is used to
• public abstract void fillOval(int x, int y, int width, int height): is used to fill
oval with the default color and specified width and height.
• public abstract void drawLine(int x1, int y1, int x2, int y2): is used to draw line between
• public abstract void drawArc(int x, int y, int width, int height, int startAngle, int
• public abstract void fillArc(int x, int y, int width, int height, int startAngle, int
• public abstract void setColor(Color c): is used to set the graphics current color to the
specified color.
• public abstract void setFont(Font font): is used to set the graphics current font to the
specified font.
Example of Graphics in applet:
• import java.applet.Applet;
• import java.awt.*;
•
• public class GraphicsDemo extends Applet{
•
• public void paint(Graphics g){
• g.setColor(Color.red);
• g.drawString("Welcome",50, 50);
• g.drawLine(20,30,20,300);
• g.drawRect(70,100,30,30);
• g.fillRect(170,100,30,30);
• g.drawOval(70,200,30,30);
•
• g.setColor(Color.pink);
• g.fillOval(170,200,30,30);
• g.drawArc(90,150,30,30,30,270);
• g.fillArc(270,150,30,30,0,180);
•
• }
• }
myapplet.html
• <html>
• <body>
• <applet code="GraphicsDemo.class" width="300"
height="300">
• </applet>
• </body>
• </html>