0% found this document useful (0 votes)
3 views5 pages

Java Applet Examples and Code

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)
3 views5 pages

Java Applet Examples and Code

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

[Link].

*;
[Link].*;
publicclassSimpleAppletextends
Applet{
publicvoidpaint(Graphicsg){
[Link]("ASimpleApplet",20,
20);
}
}

[Link].*;
[Link].*;
/*
<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
[Link]("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:
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link] [Link]
[Link]

[Link].*;
[Link].*;

/*
<appletcode="Sample"width=300
height=50>
</applet>
*/
publicclassSampleextendsApplet{
Stringmsg;
//settheforegroundandbackground
colors.
publicvoidinit(){
setBackground([Link]);
setForeground([Link]);
msg="Insideinit()";
}
//Initializethestringtobe
displayed.
publicvoidstart(){
msg+="Insidestart()";
}
//Displaymsginappletwindow.
publicvoidpaint(Graphicsg){
msg+="Insidepaint().";
[Link](msg,10,30);
}
}

[Link].*;
[Link].*;
/*
<appletcode="SimpleBanner"width=300
height=50>
</applet>
*/
publicclassSimpleBannerextends
AppletimplementsRunnable{
Stringmsg="ASimpleMoving
Banner.";
Threadt=null;
intstate;
booleanstopFlag;
//Setcolorsandinitializethread.
publicvoidinit(){
setBackground([Link]);
setForeground([Link]);
}
//Startthread
publicvoidstart(){
t=newThread(this);
stopFlag=false;
[Link]();
}
//Entrypointforthethreadthatruns
thebanner.

publicvoidrun(){
charch;
//Displaybanner
for(;;){
try{
repaint();
[Link](250);
ch=[Link](0);
msg=[Link](1,[Link]());
msg+=ch;
if(stopFlag)
break;
}catch(InterruptedExceptione){}
}
}
//Pausethebanner.
publicvoidstop(){
stopFlag=true;
t=null;
}
//Displaythebanner.
publicvoidpaint(Graphicsg){
[Link](msg,50,30);
}
}

You might also like