Java- Basic Applet Example



// A simple Applet
//java.lang.Object
//Object->awt->Component->Container->Panel->applet->Applet
//Object->awt->Component->Container->window->frame
//Object->awt->Graphics
//Object->awt->Font
//Object->awt->Color
//Object->awt->MenuComponent
//Object->awt->BorderLayout
//Object->awt->GridLayout
//Object->awt->Event


//init(),start(),stop() and destroy() are available in Applet class
// paint() method is available in component class //Object->awt->Component

//program starts from here

import java.applet.Applet;
import java.awt.Graphics;
//To run applet in appletviewer AppletEx1.java
/*
<applet  code=AppletEx1  width=200 height=200 >
</applet>
*/

public class AppletEx1 extends Applet
{

String msg;

public void init()       // to initialize
{
setBackground(Color.cyan);//Object->awt->Color
setForeground(Color.red);//whatever we draw on screen

msg="Inside init";
}
public void start()    //to run Applet
{
msg=msg+"   "+"Inside start";
}
public void paint(Graphics g)//Object->awt->Graphics to output on Applet
{
msg=msg+"   "+"Inside paint";
g.drawString(msg,10,30);
}
}

Reactions: