330Java™2:Thecompleterefer Ence
330Java™2:Thecompleterefer Ence
330Java™2:Thecompleterefer Ence
*;
importjava.applet.*;
publicclassSimpleAppletextends
Applet{
publicvoidpaint(Graphicsg){
g.drawString("ASimpleApplet",20,
20);
}
}
importjava.awt.*;
importjava.applet.*;
/*
<appletcode="SimpleApplet"width=200
height=60>
</applet>
*/
publicclassSimpleAppletextends
Applet{
publicvoidpaint(Graphicsg){
330 J a v a 2 : T h e C o m p l e t e R e f e r
ence
THE JAVA LANGUAGE
g.drawString("ASimpleApplet",20,
20);
}
}
Method:
void setBackground(Color newColor)
void setForeground(Color newColor)
Here, newColor specifies the new color. The
class Color defines the constants shown
here that can be used to specify colors:
Color.black Color.magenta
Color.blue Color.orange
Color.cyan Color.pink
Color.darkGray Color.red
Color.gray Color.white
Color.green Color.yellow
Color.lightGray
importjava.awt.*;
importjava.applet.*;
/*
<appletcode="Sample"width=300
height=50>
</applet>
*/
publicclassSampleextendsApplet{
Stringmsg;
//settheforegroundandbackground
colors.
publicvoidinit(){
setBackground(Color.cyan);
setForeground(Color.red);
msg="Insideinit()";
}
//Initializethestringtobe
displayed.
publicvoidstart(){
msg+="Insidestart()";
}
//Displaymsginappletwindow.
publicvoidpaint(Graphicsg){
msg+="Insidepaint().";
g.drawString(msg,10,30);
}
}
importjava.awt.*;
importjava.applet.*;
/*
<appletcode="SimpleBanner"width=300
height=50>
</applet>
*/
publicclassSimpleBannerextends
AppletimplementsRunnable{
Stringmsg="ASimpleMoving
Banner.";
Threadt=null;
intstate;
booleanstopFlag;
//Setcolorsandinitializethread.
publicvoidinit(){
setBackground(Color.cyan);
setForeground(Color.red);
}
//Startthread
publicvoidstart(){
t=newThread(this);
stopFlag=false;
t.start();
}
//Entrypointforthethreadthatruns
thebanner.
publicvoidrun(){
charch;
//Displaybanner
for(;;){
try{
repaint();
Thread.sleep(250);
ch=msg.charAt(0);
msg=msg.substring(1,msg.length());
msg+=ch;
if(stopFlag)
break;
}catch(InterruptedExceptione){}
}
}
//Pausethebanner.
publicvoidstop(){
stopFlag=true;
t=null;
}
//Displaythebanner.
publicvoidpaint(Graphicsg){
g.drawString(msg,50,30);
}
}