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

AOOP-Chapter 7- Java Applet

Uploaded by

school exam
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)
22 views

AOOP-Chapter 7- Java Applet

Uploaded by

school exam
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/ 20

CHAPTER FOUR

APPLET AND GRAPHICAL USER INTERFACE

1
APPLET
 An Applet is a small Internet-based program that
has the Graphical User Interface (GUI), written in the
Java programming language.
 Applets are designed to run inside a web browser or in
applet viewer to facilitate the user to animate the
graphics, play sound, and design the GUI components
such as text box, button, and radio button.
 An applet represents byte code embedded in a html
page. (applet = bytecode + html) and run with the help
of Java enabled browsers such as Internet Explorer.
 Applets are compiled using javac compiler and it can
be executed by using an appletviewer or by embedding
the class file in the HTML (Hyper Text Markup
Languege) file. 2
 Unlike Java applications applets do not have a main
() method.
 To create applet we can use java.applet.Applet or
javax.swing.JApplet class
 All applets inherit the super class ‘Applet’. An
Applet class contains several methods that helps to
control the execution of an applet.
APPLET LIFECYCLE
 when an applet is loaded , it undergoes a series of
changes in its state.
 Following are the states in applets lifecycle.

1) Born or Initialization state:

 An applet begins its life when the web browser loads


3
its classes and calls its init() method
 This method is called exactly once in Applets lifecycle
and is used to read applet parameters
 Thus, in the init() method one should provide
initialization code such as the initialization of
variables.
public void init()
{
//initialisation
}
2) Running State(Start):
 Once the initialization is complete, the web browser
will call the start() method in the applet
 This method must called at least once in the Applets
lifecycle.
4
 Start method is executed as long as applet gains
focus.
public void start()
{
//Code
}
3) Stopped State:
 The web browser will call the Applets stop()
method, if the user moved to another web page
while the applet was executing. When applet
loses focus.
 The stop() method is called at least once in
Applets Lifecycle.
public void stop()
{
//Code 5

}
4) Dead State:
 Finally, if the user decides to quit the web browser,
the web browser will free up system resources by
killing the applet before it closes.
 To do so, it will call the applets destroy() method
public void destroy()
{
// Code
}
Display State :
 Applet moves to the display state whenever it has to
perform the output operations on the screen
6
paint(): This method takes a
java.awt.Graphics object as parameter. This
class includes many methods of drawing
necessary to draw on the applet window.
• This is equivalent to runnable state of
thread.

7
 The paint() method is called to accomplish this task.
public void paint(Graphics g)
{
//Display Statements
}

Init() Initialization
Start State

Start()
Stop()
Running Stopped
State state
Start()
Destroy()

Dead state 8
Example:
import java.awt.*;
import java.applet.*;
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("My First Applet",40,40);
}
}
 Save the file as SimpleApplet.java
9
 Compile the file using javac SimpleApplet.java
How to run an Applet?
 There are two ways in which one can run an applet,
as follows
1) Executing the applet within a java-compatible web
browser.
2) Using an applet viewer, such as the standard JDK
tool, “appletviewer”. An applet viewer executes your
applet in a window.
 To execute an applet in a web browser, you need to
write a short HTML text file that contains the
appropriate APPLET tag.
10
For above example it is
<html>
<body>
<applet code="SimpleApplet.class" width=200 height=100>
</applet>
</body>
</html>
 Save this code in text file with extension .html say
Myapplet.html.
 Compile the file using javac SimpleApplet.java

11
 On successful compilation of SimpleApplet.java file,
execute the this file using appletviewer
Myapplet.html or just open this html file directly.
Building an applet code:
 Applet code uses the series of two classes, namely
Applet and Graphics from java class library.
 Applet class which is contained in the java.applet
package provides life and behavior to the applet
through its methods such as init(), start(), and
paint().

12
 When an applet is loaded, java automatically calls a
series of applet class methods for starting, running
and stopping the applet code.
 The applet class therefore maintains the lifecycle of
an applet.
 The paint() method of the applet class, when it is
called, actually display the result of applet code on
the screen.
 The output may be text, graphics or sound

 The paint() method, which requires a Graphics object


as an argument, is defined as follows:
public void paint(Graphics g)
13
 This requires that the applet code imports the
java.awt package that contains the Graphics class.
 All output operations of an applet are performed
using the methods defined in the Graphics class.
APPLET TAG
 The Applet tag is used to start an applet from both
HTML document and from applet viewer.
 An applet viewer will execute each Applet tag that it
finds in a separate window, while web browsers like
Netscape Navigator, Internet Explorer and HotJava
will allow many applets in a single page.

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

16
A program that accepts two numbers as inputs ,
calculate sum and display it

import java.awt.*;
import java.applet.*;
public class userin extends Applet
{
TextField t1,t2;
public void init()
{
t1=new TextField(8);
t2=new TextField(8);
add(t1);
add(t2);
17
t1.setText("0");
t2.setText("0");
}
public void paint(Graphics g)
{
int x=0,y=0,z=0;
String s1,s2,s;
g.drawString("Input a number in each box" ,10,50);
try{
s1=t1.getText();
x=Integer.parseInt(s1);
s2=t2.getText();
y=Integer.parseInt(s2); 18

}catch(Exception e){}
z=x+y;
s=String.valueOf(z);
g.drawString("The sum is:",10,75);
g.drawString(s,100,75);
}
public boolean action(Event e,Object o)
{
repaint();
return true;
}
}

19
 The paint() methods where all the action take
places.
 getText() method- retrieves values in the form of
string from text boxes
 parseInt() method- converts sting form data into
numeric .
 valueOf() method –converts numerical values into
string before display using String class.

20

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy