0% found this document useful (0 votes)
19 views

330Java™2:Thecompleterefer Ence

This document contains code for three Java applets: 1) A simple applet that draws a string to the screen 2) An applet that sets foreground and background colors and draws a string in multiple methods 3) An applet that implements threading to continuously scroll a banner message across the screen.

Uploaded by

Mahesh Prabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

330Java™2:Thecompleterefer Ence

This document contains code for three Java applets: 1) A simple applet that draws a string to the screen 2) An applet that sets foreground and background colors and draws a string in multiple methods 3) An applet that implements threading to continuously scroll a banner message across the screen.

Uploaded by

Mahesh Prabu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

importjava.awt.

*;
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);
}
}

You might also like