Applet Notes PDF
Applet Notes PDF
• Applet-
– These applets use the Abstract Window Toolkit (AWT) to
Applets , AWT Graphics, Color provide the graphic user interface (or use no GUI at all). This
style of applet has been available since Java was first created.
and Font Classes • JApplet-
– These are Swing applets and uses the Swing classes to
provide the GUI. Swing offers a richer and often easier-to-
use user interface than does the AWT. Thus, Swing-based
Sunita Tiwari applets are now the most popular. However, traditional
AWT-based applets are still used, especially when only a
very simple user interface is required. Thus, both AWT- and
Swing-based applets are valid
1
The simplest possible applet The simplest reasonable applet
/*<applet
code=“HelloWorld.class” width=150
height=100> It is member of
TrivialApplet.java </applet> */ the Graphics
import java.applet.Applet; class. Used to
import java.awt.*; write a message
public class TrivialApplet extends Applet { import java.applet.Applet; At x, y
} coordinate
public class HelloWorld extends Applet {
TrivialApplet.html public void paint( Graphics g ) {
g.drawString( "Hello World!", 30, 30 );
<applet }}
code="TrivialApplet.class”
width=150 height=100>
</applet>
2
Why an applet works Applet Skeleton
// An Applet skeleton.
import java.awt.*;
import java.applet.*;
/* <applet code="AppletSkel" width=300 height=100> </applet> */
• You write an applet by extending the class Applet public class AppletSkel extends Applet {
// Called first.
• Applet defines methods init( ), start( ), stop( ), public void init() { // initialization
paint(Graphics), destroy( ) }
/* Called second, after init(). Also called whenever the applet is restarted. */
• These methods do nothing--they are stubs public void start() { // start or resume execution
}
• You make the applet do something by overriding // Called when the applet is stopped.
these methods. public void stop() { // suspends execution
}
/* Called when applet is terminated. This is the last method executed. */
public void destroy() { // perform shutdown activities
}
// Called when an applet's window must be restored.
public void paint(Graphics g) { // redisplay contents of window
}}
3
Methods are called in this order public void paint(Graphics g)
• init and destroy are only called
init() once each • Needed if you do any drawing or painting other than
• start and stop are called just using standard GUI Components
start() whenever the browser enters and • Any painting you want to do should be done here, or
leaves the page in a method you call from here
do some work • do some work is code called by • Painting that you do in other methods may or may not
your listeners happen
stop() • paint is called when the applet • Never call paint(Graphics), call repaint( )
needs to be repainted
destroy()
repaint( ) update( )
4
Simple Applet Display Methods Using Status Window
import java.awt.*;
• void setBackground(Color newColor) import java.applet.*;
/* <applet code="StatusWindow" width=300
• void setForeground(Color newColor) height=50>
• You can use the constants defined by Color class </applet> */
to specify colors: public class StatusWindow extends Applet{
public void init() {
• Example: Color.cyan , Color.pink Color.red etc setBackground(Color.cyan);
here. }
• setBackground(Color.green); // Display msg in applet window.
public void paint(Graphics g) {
• setForeground(Color.red); g.drawString("This is in the applet window.",
10, 20);
showStatus("This is shown in the status
window.");
}}
5
Output Sample Graphics methods
• A Graphics is something you can paint on
1 // WelcomeLines.java
2 // Displaying text and lines
3
4
5
// Java packages
import java.awt.*; // import class Graphics
Rectangles
6 import java.applet.*; // import class JApplet
7
8 public class WelcomeLines extends Applet {
import java.awt.*;
9 import java.applet.*;
10 // draw lines and a string on applet’s background
11 public void paint( Graphics g ) /*
12 {
<applet code="Rectangles" width=300
13
14 height=200>
15
</applet>
16 // draw horizontal line from (15, 10) to (210, 10)
17 g.drawLine( 15, 10, 210, 10 ); */
18
19 // draw horizontal line from (15, 30) to (210, 30) public class Rectangles extends Applet {
20 g.drawLine( 15, 30, 210, 30 ); Draw horizontal lines with public void paint(Graphics g) {
21 drawLine (endpoints have same
22 // draw String between lines at location (25, 25)
y coordinate). g.drawRect(10, 10, 60, 50);
23 g.drawString( "Welcome to Java Programming!", 25, 25 );
24
g.fillRect(100, 10, 60, 50);
25 } // end method paint g.drawRoundRect(190, 10, 60, 50, 15,
26
27 } // end class WelcomeLines
15);
g.fillRoundRect(70, 90, 140, 100, 30, 40);
}}
6
Polygons Working with Color
import java.awt.*;
import java.awt.*; import java.applet.*;
g.setColor(c3);
g.drawLine(20, 150, 400, 40);
import java.applet.*; /* g.drawLine(5, 290, 80, 19);
<applet code="ColorDemo" width=300 g.setColor(Color.red);
/* height=200> g.drawOval(10, 10, 50, 50);
<applet code="HourGlass" width=230 </applet> g.fillOval(70, 90, 140, 100);
height=210> */ g.setColor(Color.blue);
public class ColorDemo extends Applet { g.drawOval(190, 10, 90, 30);
</applet> // draw lines g.drawRect(10, 10, 60, 50);
*/ public void paint(Graphics g) { g.setColor(Color.cyan);
Color c1 = new Color(255, 100, 100); g.fillRect(100, 10, 60, 50);
public class HourGlass extends Applet {
Color c2 = new Color(100, 255, 100); g.drawRoundRect(190, 10, 60, 50, 15, 15);
public void paint(Graphics g) { Color c3 = new Color(100, 100, 255); }
int xpoints[] = {30, 200, 30, 200, 30}; g.setColor(c1); }
g.drawLine(0, 0, 100, 100);
int ypoints[] = {30, 30, 200, 200, 30}; g.drawLine(0, 100, 100, 0);
int num = 5; g.setColor(c2);
g.drawLine(40, 25, 250, 180);
g.drawPolygon(xpoints, ypoints, num); g.drawLine(75, 90, 400, 400);
}
}
7
Applets are not magic!
• Anything you can do in an applet, you can do in
an application.
• You can do some things in an application that you
can’t do in an applet.
• If you want to access files from an applet, it must
be a “trusted” applet.
• Trusted applets are beyond the scope of this
subject.