Example of an applet program

admin February 18, 2014 0 Comments

applet program

// this program is based on the inter applet communication ....
// in this program , we are selecting the colour of the second applet from the first applet .....

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="FirstApplet" height ="400" width = "200" name="first">
</applet>
<applet code="SecondApplet" height ="400" width = "200" name="second">
</applet>
*/
public class FirstApplet extends Applet implements ActionListener
{

Button b1,b2 ;
public void init()
{
b1 = new Button("red");
b2 = new Button("green");
b1.addActionListener(this);
b2.addActionListener(this);

add(b1);
add(b2);
setBackground(Color.gray);

}

public void paint (Graphics g)
{
g.drawString ("select the colour", 30,50);
g.drawString ("for second applet", 30,70);


}
public void actionPerformed (ActionEvent e)
{

AppletContext ctx = getAppletContext();
SecondApplet sa = (SecondApplet) ctx. getApplet("second");// downcasting is done here .....
if (e.getSource() == b1 )
{
sa.setBackground(Color.red);

}
if(e.getSource()== b2 )
{
sa.setBackground(Color.green);
}

}
}

Leave a Reply