0% found this document useful (0 votes)
56 views25 pages

Chapter Seven Applet

Applets are small programs that run in web browsers and are transmitted over the Internet. Applets do not have a main method and instead use the interface provided by the Abstract Window Toolkit (AWT). Applets are not trusted like applications and have limited access to system resources for security. A simple applet subclassses Applet and overrides the paint method to draw output. Applets go through an initialization lifecycle of init, start, paint methods and termination with stop and destroy methods. The AWT provides graphics methods for drawing primitives like lines, rectangles, and ovals.

Uploaded by

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

Chapter Seven Applet

Applets are small programs that run in web browsers and are transmitted over the Internet. Applets do not have a main method and instead use the interface provided by the Abstract Window Toolkit (AWT). Applets are not trusted like applications and have limited access to system resources for security. A simple applet subclassses Applet and overrides the paint method to draw output. Applets go through an initialization lifecycle of init, start, paint methods and termination with stop and destroy methods. The AWT provides graphics methods for drawing primitives like lines, rectangles, and ovals.

Uploaded by

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

Chapter seven

Applets
Applet Basics
 Applets are small programs that are designed for
transmission over the Internet and run within a browser
or Applet Viewer tool(JDK)
 Applet does not have a main( ) method.
 applets use the interface provided by the AWT.(not I/O
console)
Application vs. Applet
 Application
 Trusted (i.e., has full access to system resources)
 Invoked by Java Virtual Machine (JVM, java), e.g.,
 Should contain a main method, i.e.,
public static void main(String[])

 Applet
 Not trusted (i.e., has limited access to system resource to prevent
security breaches)
 Invoked automatically by the web browser
 Should be a subclass of class java.applet.Applet
Examining a simple applet
 // A minimal applet.
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet {
public void paint(Graphics g) {
g.drawString("Java makes applets easy.", 20, 20);
}
}
Cont…
 This applet begins with two import statements:
 first imports the Abstract Window Toolkit (AWT) classes.
 Import statement imports the applet package.(This package contains
the class Applet. Every applet that you create must be a subclass of
Applet.)
 Inside SimpleApplet, paint( )is declared.
 defined by the AWT Component class (which is a superclass of Applet)
 must be overridden by the applet.
 It is called or invoked when :
 Each time the applet must redisplay its output.
 The applet begins execution
 Redraw its output
 The paint( )method has one parameter of type Graphics.
Cont….
 Inside paint( ), there is a call to drawString( ), which is a member
of the Graphics class.
 It has the following general form:
void drawString(String message, int x, int y)
 Message is the string to be output beginning at x,y.
 Graphics Coordinate
(0,0) x

height

y
width
Embedding Applet into HTML
 To execute an applet (in either a Web browser or the
appletviewer), you need to write a short HTML text file that
contains the appropriate APPLET
 For example :for the above applet code, HTML file that will
execute SimpleApplet is
<applet code="SimpleApplet" width=200 height=60>
</applet>
And you have to save it with SimpleApplet.html
 We can use stand-alone HTML file to execute an applet(both
the code and the html code).
Compiling and Running
 To compile
javac SimpleApplet.java
Produces SimpleApplet.class
 To run
 Open page SimpleApplet.htmlfrom web browser or
 Use appletviewer of JDK
appletviewer SimpleApplet.html
 The window produced by SimpleApplet, as displayed by
applet viewer, is shown in the following illustration:
A Complete Applet Skeleton
 applets override a set of methods form Applet class // start or resume execution
These five methods can be assembled into the skeleton shown }
here: // Called when the applet is stopped.
// An Applet skeleton. public void stop() {
import java.awt.*; // suspends execution
import java.applet.*; }
/* /* Called when applet is terminated. This is the last
<applet code="AppletSkel" width=300 height=100> method executed. */
</applet> public void destroy() {
*/ // perform shutdown activities
public class AppletSkel extends Applet { }
// Called first. // Called when an applet's window must be restored.
public void init() { public void paint(Graphics g) {
// initialization // redisplay contents of window
} }
/* Called second, after init(). Also called whenever }
the applet is restarted. */
public void start() {
Applet Initialization and Termination
 When an applet begins, the following methods are called in
this sequence:
1. init( )
2. Start( )
3. Paint ( )
 When an applet is terminated the following methods are
called in this sequence :
1. Stop ( )
2. destroy( )
The Life-Cycle of Applet
 init()
 Called exactly once in an applet’s life.
 Called when applet is first loaded,e.g., when the
browser visits the web page for the first time.
 Used to initialize variables, read applet parameters,
start downloading any other images or media files, etc.
 start( )
 The start( )method is called after init( ).
 It is also called to restart an applet after it has been
stopped.
 start( ) is called each time an applet’s HTML document
is displayed onscreen.
The Life-Cycle cont….
 paint( )
 is called each time your applet’s output must be redrawn.
 has one parameter of type Graphics.
 This context is used whenever output to the applet is
required.
 stop( )
 is called when a web browser leaves the HTML document
containing the applet
• destroy()
 Called exactly once.
 Called when the browser unloads the applet.
 Used to perform any final clean-up.
 The stop( )method is always called before destroy( ).
The Life-Cycle cont….

init

start destroy
stop

start
Code Example …
/* A simple applet that sets the foreground and msg = "Inside init( ) --";
background colors and outputs a string. */ }
import java.awt.*; // Initialize the string to be displayed.
import java.applet.*; public void start() {
/* msg += " Inside start( ) --";
<applet code="Sample" width=300 }
height=50> // Display msg in applet window.
</applet> public void paint(Graphics g) {
*/ msg += " Inside paint( ).";
public class Sample extends Applet{ g.drawString(msg, 10, 30);
String msg; }
// set the foreground and background colors. }
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
The output
Color Control

Exaple : setColor(Color.RED); or
setColor(new Color(255,0,0));
Font Class
 Fonts are specified with three attributes:
 font name:
Serif Sans-serif Monospaced Dialog DialogInput
TimesRoman Helvetica Courier
 font style:
PLAIN BOLD ITALIC
Styles can be combined: Font.BOLD|Font.ITALIC
 font size: a positive integer

 A font can be created as follows:


new Font(name, style, size)
Drawing Lines, Rectangles and Ovals
 public void drawLine( int x1, int y1, int x2, int y2 )
Draws a line between the point (x1, y1) and the point (x2, y2).
 public void drawRect( int x, int y, int width, int height)
Draws a rectangle of the specified width and height.
The top-left corner of the rectangle has the coordinates (x, y).
 public void fillRect( int x, int y, int width, int height )
Draws a rectangle of the specified width and height.
The top-left corner of the rectangle has the coordinates (x, y).
The rectangle is filled with the Graphics object's color.
 public void clearRect( int x, int y, int width, int height )
Draws a rectangle of the specified width and height.
The top-left corner of the rectangle has the coordinates (x, y).
In the current background color
Drawing Lines, Rectangles and Ovals
 public void drawRoundRect( int x, int y, int width, int
height, int arcWidth, int arcHeight )
 public void fillRoundRect( int x, int y, int width, int height,
int arcWidth, int arcHeight )
 public void drawOval( int x, int y, int width, int height )
 public void fillOval( int x, int y, int width, int height )
Example
import java.awt.*;
import java.applet.*;
/* g.setColor( Color.CYAN );
<applet code="Sample" width=300 height=50> g.fillRoundRect( 195, 40, 90, 55, 50, 50 );
</applet> g.drawRoundRect( 290, 40, 90, 55, 20, 20 );
*/
public class Sample extends Applet{ g.setColor( Color.YELLOW );
public void paint(Graphics g) { g.draw3DRect( 5, 100, 90, 55, true );
this.setBackground( Color.WHITE ); g.fill3DRect( 100, 100, 90, 55, false );

g.setColor( Color.RED ); g.setColor( Color.MAGENTA );


g.drawLine( 5, 30, 380, 30 ); g.drawOval( 195, 100, 90, 55 );
g.fillOval( 290, 100, 90, 55 );
g.setColor( Color.BLUE ); }
g.drawRect( 5, 40, 90, 55 ); }
g.fillRect( 100, 40, 90, 55 );
The output
Swing and AWT
Swing and AWT

 GUI :Windowing systems that interact with the user are


often called GUIs. GUI is stands for graphical user
interface
 There are actually two sets of GUI components in Java:
1. The Abstract Windowing Toolkit (AWT)
in package java.awt
2. Swing
it is extension of awt
in package javax.swing
Swing Vs. AWT
 Swing provides a richer set of components than AWT
 Swing provides additional components like JTable, JTree etc.
 Swing added functionality to AWT-replacement components.
 Swing components can change their appearance based on the
current “look and feel”.
 Swing do not rely on the underlying native GUIs.
 Swing components are lightweight (less resource intensive
than AWT).
 Swing is faster than awt.

You might also like