JAVA APPLETS
Applet
• An applet is a small java program that must be
run from another program called the host
program
• Applets are usually run from web browsers such
as Microsoft’s Internet Explorer
• The applet class must be an extension of the
[Link] class
• The newer [Link] is such an
extension, so more functionality is inherited by
extending the JApplet class instead
HELLOWORLD
EXAMPLE
Using [Link] Class
// tell the compiler where to find the methods you
will use.
// required when you create an applet
import [Link].*;
// required to paint on screen
import [Link].*;
// the start of an applet - HelloWorld will be the executable
class
// Extends applet means that you will build the code on the
standard Applet class
public class HelloWorld extends Applet
{
// The method that will be automatically called when the applet
is started
public void init()
{
// It is required but does not need anything.
}
// This method gets called when the applet is terminated
// That's when the user goes to another page or exits the
browser.
public void stop()
{
// no actions needed here now.
}
// The standard method that you have to use to
paint things on screen
// This overrides the empty Applet method, you
can't called it "display" for example.
public void paint(Graphics g)
{
//method to draw text on screen
// String first, then x and y coordinate.
[Link]("Hey hey hey",20,20);
[Link]("Hellooow World",20,40);
}
}
Using [Link] Class
import [Link].*;
import [Link].*;
public class Hello extends JApplet {
public void init() {
Container contentPane = getContentPane();
[Link](new FlowLayout());
JLabel label = new JLabel("Hello, World");
[Link](label);
}
}
Executing An Applet
• An applet can be executed only from another program
• For testing purposes, an applet viewer is most convenient
• The Java Software Development Kit (JDK) includes an
applet viewer, named “[Link]”
[Link]
• First you have to prepare an html page which to run the applet
<HTML>
<HEAD>
<TITLE>Hello</TITLE>
</HEAD>
<BODY>
Hi, y'all...<br>
<hr>
<applet code="[Link]" width=300
height=50>
</applet>
</hr>
</BODY>
</HTML>
Appletviewer or Web Browser
• From the dos prompt write the following command:
Appletviewer [Link]
• You can also put these two files, [Link] and
[Link] in the local web site folder and open the
[Link] page from the browser
DRAWING SHAPES AND
USING COLORS
/*
Applet will paint special shapes and use colors and fonts
Only new methods are explained
*/
import [Link].*;
import [Link].*;
public class DrawExample extends Applet
{
// Specify variables that will be needed everywhere, anytime here
// The font variable
Font bigFont;
// The colors you will use
Color redColor;
Color weirdColor;
Color bgColor;
public void init()
{
// Here we will define the varibles further
// Will use Arial as type, 16 as size and bold as style
// Italic and Plain are also available
bigFont = new Font("Arial",[Link],16);
// Standard colors can be named like this
redColor = [Link];
// lesser known colors can be made with R(ed)G(reen)B(lue).
weirdColor = new Color(60,60,122);
bgColor = [Link];
// this will set the backgroundcolor of the applet
setBackground(bgColor);
}
public void stop() { }
// now lets draw things on screen
public void paint(Graphics g)
{
// tell g to use your font
[Link](bigFont);
[Link]("Shapes and Colors",80,20);
// Now we tell g to change the color
[Link](redColor);
// This will draw a rectangle (xco,yco,xwidth,height);
[Link](100,100,100,100);
// This will fill a rectangle
[Link](110,110,80,80);
// change colors again
[Link](weirdColor);
// a circle (int x, int y, int width, int height,int startAngle, int arcAngle);
// ovals are also possible this way.
[Link](120,120,60,60,0,360);
[Link]([Link]);
// Draw a line (int x1, int y1, int x2, int y2)
[Link](140,140,160,160);
// reset the color to the standard color for the next time the applets
paints
// an applet is repainted when a part was'nt visible anymore
// happens most often because of browser minimizing or scrolling.
[Link]([Link]);
}
}