5. 1.Write an applet to draw and fill rectangle
import java.awt.*;
import java.applet.*;
/*
<applet code = MyApplet width= 200 height=150>
</applet>*/
public class MyApplet extends Applet {
public void paint(Graphics g) {
g.drawRect(50,50,100,100);
g.fillRect(200,200,300,300);
//draw filled in rectangle of default color
}
}
5
6. 2. Write an applet to draw a string
import java.awt.*;
import java.applet.*;
/*
<applet code = SimpleApplet width = 400 height = 400 >
</applet>
*/
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString ("Welcome to the world of applet
programming", 20, 20);
}
}
6
7. 3. Program to test all methods of applet
import java.awt.*;
import java.applet.*;
/*
<applet code = Allmethods width = 400 height= 400>
</applet>
*/
public class Allmethods extends Applet {
String s1, s2, s3, s4;
public void init() {
s1= "message from init method"; }
public void start() {
s2= "message from start method"; }
public void stop() {
s3= " message from stop method"; }
7
8. 3. Program to test all methods of applet
contd..
public void destroy() {
s4= " message from destroying"; }
public void paint(Graphics g) {
g.drawString("Demo for testing all methods of applet", 20,40);
if(s1!=null)
g.drawString(s1,20,80);
if(s2!=null)
g.drawString(s2,20,100);
if(s3!=null)
g.drawString(s3,20,120);
if(s4!=null)
g.drawString(s4,20,140);
}
}
8
9. 4. Applet program to change the foreground
and back ground colors
import java.awt.*;
import java.applet.*;
import java.awt.Graphics;
import java.awt.Color;
/*
<applet code="Sample" width=300 height=50>
</applet> */
public class Sample extends Applet{
String msg;
// set the foreground and background colors.
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
msg = "Inside init( ) --"; } 9
10. Applet program to change the foreground and
back ground colors contd..
// Initialize the string to be displayed.
public void start() {
msg += " Inside start( ) --";
}
// Display msg in applet window.
public void paint(Graphics g) {
msg += " Inside paint( ).";
g.drawString(msg, 10, 30);
}
}
10
11. Summary
• We have written several applet programs
• Covering the different aspects of applets
11
12. Quiz
1. Where is the JVM available to run an applet
when an applet is downloaded from an
internet into our local machine where java
was not installed
a) Local
b) Remote
12