UNIT-5 JAVA APPLETS & GRAPHICS PROGRAMMING
1. APPLET
An applet is a special kind of Java program that runs in a Java enabled browser. This is the first Java
program that can run over the network using the browser. Applet is typically embedded inside a web
page and runs in the browser.
Applets are small Java applications that can be accessed on an Internet server, transported over
Internet, and can be automatically installed and run as a part of a web document.
After a user receives an applet, the applet can produce a graphical user interface.
To create an applet, a class must class [Link] class.
An Applet class does not have any main () method. It is viewed using JVM. The JVM can use
either a plug-in of the Web browser or a separate runtime environment to run an applet
application.
The appletviewer runs an applet in the window. It is usually the fastest and easiest way to test an
applet. Create an applet containing the <applet> tag in the comment, and compile it. It is for testing
purposes only.
To run an applet using the appletviewer tool, write in the command prompt:
c:\>javac [Link] c:\>appletviewer [Link]
Advantages of applets
● It works on the client-side, so the response time is shorter.
● It is secured. Applets have strict security rules that apply to web browsers.
● It runs on browsers on multiple platforms, including Linux, Windows, and Mac Os.
2. APPLET LIFE CYCLE
Every Java applet inherits a set of default behaviors from the Applet class. As a result, when an applet is
loaded, it undergoes a series of changes in its state.
The applet state includes:
● Born on initialization state
● Running state
● Idle state
● Dead or destroyed state
1
1. Initialization State:
Applet enters the initialization state when it is first loaded. This is achieved by calling the
init() method of the Applet class. The initialization occurs only once in the applet's life cycle.
public void init(){
.........
. (action)
}
2. Running State:
The start() method contains the actual code of the applet that should run.
The start() method executes immediately after the init() method. It also executes whenever the applet
is restored, maximized or moving from one tab to another tab in the browser.
public void start(){
.........
. (action)
}
3. Idle or Stopped State:
The stop() method stops the execution of the applet. The stop() method executes when the
applet is minimized or when moving from one tab to another in the browser.
public void stop(){
.........
. (action)
}
2
4. Dead State:
The destroy() method executes when the applet window is closed or when the tab containing
the web page is closed. The stop() method executes just before when destroy() method is invoked. The
destroy() method removes the applet object from memory.
public void destroy(){
.........
. (action)
}
5. Display State:
The paint() method is used to redraw the output on the applet display area. The paint()
method executes after the execution of start() method and whenever the applet or browser is
resized.
public void paint(Graphics g){
.........
. (Display statements)
}
3
3. APPLET TAG
4. ADDING APPLET TO HTML FILE
5. EMBEDDING <APPLET> TAG JAVA CODE
Applets are embedded in HTML documents with the <APPLET> tag.
It contains attributes that identify the applet to be displayed and, optionally, give the web browser
hints about how it should be displayed.
<PARAM> tags that contain data to be passed to the applet.
Attributes are name/value pairs that are interpreted by a web browser or appletviewer.
4
Attributes of the <APPLET> tag specify general features that apply to all applets, such as size and
alignment.
Three attributes are required in the <APPLET> tag. Two of these
attributes, width and height, specify the space the applet occupies on the screen. The third required attribute
code, you must supply this attribute. The code attribute specifies the class file from which the applet is
loaded.
6. PASSING PARAMETER TO APPLET
We can get any information from the HTML file as a parameter.
or this purpose, Applet class provides a method named getParameter().
Syntax: public String getParameter(String parameterName)
5
Passing parameters to an applet.
import [Link].*; import
[Link].*;
/* <applet code="Applet8" width="400" height="200">
<param name="Name" value="Roger">
<param name="Age" value="26">
<param name="Sport" value="Tennis">
<param name="Food" value="Pasta">
<param name="Fruit" value="Apple">
<param name="Destination" value="California">
</applet>
*/
public class Applet8 extends Applet { String name;
String age;
String sport;
String food;
String fruit;
String destination; public void
init(){
name = getParameter("Name"); age =
getParameter("Age"); food =
getParameter("Food"); fruit =
getParameter("Fruit");
destination = getParameter("Destination"); sport =
getParameter("Sport");
}
public void paint(Graphics g){
[Link]("Reading parameters passed to this applet -", 20, 20);
[Link]("Name -" + name, 20, 40);
[Link]("Age -" + age, 20, 60); [Link]("Favorite fruit -" +
fruit, 20, 80); [Link]("Favorite food -" + food, 20, 100);
[Link]("Favorite destination -" + name, 20, 120);
[Link]("Favorite sport -" + sport, 20, 140);
}
}
6
7. ADDING CONTROLS TO APPLET
❖ A Button is a GUI control that when pressed (via the mouse) performs some action or
calculation.
❖ A TextField is a control that allows one to type in text or numbers. It can be used as a source
of input or output (mainly input). The 'contents' of a TextField are always Strings.
❖ A Label is a control that is used for output only. It contains only text and merely writes to
the Applet surface.
8. GRAPHICS CLASS
To perform graphics like shapes, lines, textbox, label, checkbox, frames, and menu bars in java, the
Graphics class is used.
The Graphics class defines a number of drawing functions, Each shape can be drawn edge-only or
filled. To draw shapes on the screen, we may call one of the methods available in the Graphics class.
void drawLine(int startX, int startY, int endX, int endY);
7
void drawRect(int top, int left, int width, int height); void fillRect(int top,
int left, int width, int height);
void drawRoundRect(int top,int left,int width, int height int Xdiameter, int YDiameter);
void fillRoundRect(int top,int left,int width, int height int Xdiameter, int YDiameter);
void drawOval(int top, int left, int width, int height); void fillOval(int top,
int left, int width, int height);
8
void drawArc(int top, int left, int width, int height, int startAngle, int sweetAngle);
void fillArc(int top, int left, int width, int height, int startAngle, int sweetAngle);
void drawPolygon(int ,[] , int y[], int numPointer); void
fillPolygon(int ,[] , int y[], int numPointer);
9
9. COLOR CLASS & METHODS
The Color class is a part of Java Abstract Window Toolkit (AWT) package.
The Color class creates color by using the given RGBA values where RGBA
stands for RED, GREEN, BLUE, and ALPHA.
The value for individual components RGBA ranges from 0 to 255 or 0.0 to 0.1. The value of alpha
determines the opacity of the color, where 0 or 0.0 stands fully transparent and 255 or 1.0 stands
opaque.
method explanation
Determines whether another Color object is equal to this Color.
equals(Object obj)
getColor(String nm) Finds a color in the system properties.
getColor(String nm, Color v) Finds a color in the system properties.
getColor(String nm, int v) Finds a color in the system properties.
hashCode() computes the hash code for this Color.
10
10. FONT CLASS & METHODS
The Font class provides a method of specifying and using fonts.
The Font class constructor constructs font objects using the font’s name, style (PLAIN, BOLD,
ITALIC, or BOLD + ITALIC), and point size.
The getName() method returns the logical Java font name of a particular font. The
getFamily() method returns the operating system-specific name of the font. The standard
Java font names are Courier, Helvetica, TimesRoman etc.
Font getFont() It is a method of Graphics class used to get the font property. setFont(Font f) is
used to set a font in the graphics context.
Font style is specified using constants from the Font class:
[Link]
[Link]
11
12
String getFamily()
1
Returns the family name of this Font.
static Font getFont(String nm)
2
Returns a Font object fom the system properties list.
static Font getFont(String nm, Font font)
3
Gets the specified Font from the system properties list.
String getFontName()
4
Returns the font face name of this Font.
String getName()
5
Returns the logical name of this Font.
int getSize()
6
Returns the point size of this Font, rounded to an integer.
int getStyle()
7
Returns the style of this Font.
boolean isBold()
8
Indicates whether or not this Font object's style is BOLD.
boolean isItalic()
9
Indicates whether or not this Font object's style is ITALIC.
boolean isPlain()
10
Indicates whether or not this Font object's style is PLAIN.
13
import [Link].*; import
[Link].*; import
[Link].*;
public class AWTGraphicsDemo extends Frame {
public AWTGraphicsDemo(){
super("Java AWT Examples");
prepareGUI();
}
public static void main(String[] args){
AWTGraphicsDemo awtGraphicsDemo = new AWTGraphicsDemo(); [Link](true);
}
private void prepareGUI(){ setSize(400,400); }
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D)g;
Font plainFont = new Font("Serif", [Link], 24);
[Link](plainFont);
[Link]("Welcome to TutorialsPoint", 50, 70); Font
italicFont = new Font("Serif", [Link], 24);
[Link](italicFont);
[Link]("Welcome to TutorialsPoint", 50, 120); Font boldFont
= new Font("Serif", [Link], 24); [Link](boldFont);
[Link]("Welcome to TutorialsPoint", 50, 170);
Font boldItalicFont = new Font("Serif", [Link]+[Link], 24); [Link](boldItalicFont);
[Link]("Welcome to TutorialsPoint", 50, 220);
}
}
14