Example of an applet program in java
Introduction to An Applet program in Java
Before Jumping Straight to Writing an Applet program in java lets us first introduce applet. Applet is a special type of program which is embedded in a webpage to generate the dynamic content. An Applet runs inside the browser and works at client side.
Advantages of Applet
Here are the advantages of applet in Java :
–> It works at client side so give response in less time.
–> Its secured
–> It can be executed by browsers running under various platforms, including Linux, Windows, Mac Os etc.
Disadvantages of Applet in Java
Plugin required at client side browser to run applet.
Example of an applet program in java
This program is based on the inter applet communication. In this program , we are selecting the color 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); } } }