Professional Documents
Culture Documents
Presented By
Harish Panwar
(09211007)
E-4,CS-IDD
1
APPLETS AND APPLICATIONS
An applet is a Java program that runs on a web
page
Applets can be run within any modern browser
To run modern Java applets, old browsers need an
up-to-date Java plugin
appletviewer is a program that can run
An application is a Java program that runs all by
itself
2
PACKAGES AND CLASSES
Java supplies a huge library of pre-written
“code,” ready for you to use in your programs
Code is organized into classes
Classes are grouped into packages
One way to use this code is to import it
You can import a single class, or all the
classes in a package
3
THE APPLET CLASS
4
IMPORTING THE APPLET CLASS
Here is the directive that you need:
import java.applet.Applet;
import is a keyword
java.applet is the name of the package
A dot ( . ) separates the package from the class
Applet is the name of the class
There is a semicolon ( ; ) at the end
5
THE JAVA.AWT PACKAGE
“awt” stands for “Abstract Window Toolkit”
The java.awt package includes classes for:
Drawing lines and shapes
Drawing letters
Setting colors
Choosing fonts
If it’s drawn on the screen, then java.awt is
probably involved!
6
IMPORTING THE JAVA.AWT PACKAGE
Since you may want to use many classes from
the java.awt package, simply import them all:
import java.awt.*;
The asterisk, or star (*), means “all classes”
The import directives can go in any order, but
must be the first lines in your program
7
C AND C++ PROGRAMMERS ONLY
8
THE APPLET SO FAR
import java.applet.Applet;
import java.awt.*;
9
YOUR APPLET CLASS
public class Drawing extends Applet {
… }
Drawing is the name of your class
Class names should always be capitalized
extends Applet says that our Drawing is a kind of
Applet, but with added capabilities
Java’s Applet just makes an empty window
We are going to draw in that window
The only way to make an applet is to extend Applet
10
THE APPLET SO FAR
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
…we still need to put some code in here...
}
11
THE PAINT METHOD
Our applet is going to have a method to paint
some colored rectangles on the screen
This method must be named paint
paint needs to be told where on the screen it can
draw
This will be the only parameter it needs
paint doesn’t return any result
12
THE PAINT METHOD, PART 2
public void paint(Graphics g) { … }
public says that anyone can use this method
void says that it does not return a result
A Graphics (short for “Graphics context”) is an
object that holds information about a painting
It remembers what color you are using
It remembers what font you are using
You can “paint” on it (but it doesn’t remember what
you have painted)
13
THE APPLET SO FAR
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
public void paint(Graphics g) {
…we still need to put some code in here…
}
}
14
COLORS
The java.awt package defines a class named Color
There are 13 predefined colors—here are their fully-
qualified names:
Color.BLACK Color.PINK Color.GREEN
Color.DARK_GRAY Color.RED Color.CYAN
Color.GRAY Color.ORANGE Color.BLUE
Color.LIGHT_GRAY Color.YELLOW
Color.WHITE Color.MAGENTA
Setting a color
g.setColor(Color.RED);
g.setBackground(Color.Green);
g.setForeground(Color.BLUE);
15
THE PAINT METHOD SO FAR
16
JAVA’S COORDINATE SYSTEM
(0, 0) (50, 0)
(w-1, h-1)
18
THE COMPLETE APPLET
import java.applet.Applet;
import java.awt.*;
// CIT 591 example
public class Drawing extends Applet {
public void paint(Graphics g) {
g.setColor(Color.BLUE);
g.fillRect(20, 20, 50, 30);
g.setColor(Color.RED);
g.fillRect(50, 30, 50, 30);
}
}
19
SOME MORE JAVA.AWT METHODS
g.drawLine( x1 , y1 , x2 , y2 );
g.drawOval( left , top , width , height );
g.fillOval( left , top , width , height );
g.drawRoundRect( left , top , width , height );
g.fillRoundRect( left , top , width , height );
g.drawArc( left , top , width , height ,
startAngle , arcAngle );
g.drawString( string , x , y );
20
HTML
<html>
<head>
<title> Hi World Applet </title>
</head>
<body>
<applet code="HiWorld.class"
width=300 height=200>
<param name="arraysize" value="10">
</applet>
</body>
</html>
21
THE <APPLET> TAG
The <applet ...> ... </applet> tag is what tells your browser
to put an applet here
<applet> has three required attributes:
code="HiWorld.class" tells the browser where to find the code
Alternatively, if your code is in a .jar file, you can say
archive="HiWorld.jar“
width="300" tells the browser how many pixels wide to make the applet
window
A typical monitor resolution is 72 pixels/inch
height="200" tells the browser how many pixels high to make the applet
window
In a browser, you cannot resize the applet; but in appletviewer,
you can resize the applet
22
<PARAM NAME="ARRAYSIZE" VALUE="10">
Example:
String s = getParameter("arraysize");
try { size = Integer.parseInt (s) }
catch (NumberFormatException e) {…}
23
THE CLASS APPLET
java.awt.Panel
java.applet.Applet
destroy()
init()
start()
stop()
24
THE LIFE-CYCLE OF APPLET
init()
Called exactly once in an applet’s life.
Called when applet is first loaded, which is
after object creation, e.g., when the browser
visits the web page for the first time.
Used to read applet parameters, start
downloading any other images or media
files, etc.
25
APPLET LIFE-CYCLE (CONT.)
start()
Called at least once.
Called when an applet is started or
restarted, i.e., whenever the browser visits
the web page.
stop()
Called at least once.
Called when the browser leaves the web
page.
26
APPLET LIFE-CYCLE (CONT.)
destroy()
Called exactly once.
Called when the browser unloads the applet.
Used to perform any final clean-up.
init
start destroy
stop
start
27
PUBLIC VOID PAINT(GRAPHICS G)
Needed if you do any drawing or painting other
than just using standard GUI Components
Any painting you want to do should be done
here, or in a method you call from here
Painting that you do in other methods may or
may not happen
Never call paint(Graphics), call repaint( )
28
REPAINT( )
Call repaint( ) when you have changed something
and want your changes to show up on the screen
You do not need to call repaint() when something in
Java’s own components (Buttons, TextFields, etc.)
You do need to call repaint() after drawing commands
(drawRect(...), fillRect(...), drawString(...), etc.)
repaint( ) is a request--it might not happen
When you call repaint( ), Java schedules a call to
update(Graphics g)
29
Ex:- Input 3 numbers using applet and print their
sum,product,average, smallest and largest number.
package harish;
import java.awt.*;
import java.applet.*;
import java.text.*;
/*<applet code=t7_q5 width=150 height=200></applet>*/
public class input extends Applet{
TextField text1,text2,text3;
public void init(){
text1=new TextField(10);
text2=new TextField(10);
text3=new TextField(10);
add(text1);
add(text2);
add(text3);
}
public void paint(Graphics g){
float a=0,b=0,c=0,smallest,largest;
String s1,s2,s3,sum,average,product,s_small,s_large;
g.drawString("enter a number in each box ", 10, 50);
try{
s1=text1.getText();
a=Float.parseFloat(s1);
s2=text2.getText();
b=Float.parseFloat(s2);
s3=text3.getText();
c=Float.parseFloat(s3);
30
}
catch(Exception e){}
sum=String.valueOf(a+b+c);
average=String.valueOf((a+b+c)/3);
product=String.valueOf(a*b*c);
smallest=a;
if(b<smallest)smallest=b;
if(c<smallest)smallest=c;
s_small=String.valueOf(smallest);
largest=a;
if(b>largest)largest=b;
if(c>largest)largest=c;
s_large=String.valueOf(largest);
g.drawString("sum of entered no. is "+sum,10,70);
g.drawString("product of entered no. is "+product,10,90);
g.drawString("average of entered no. is "+average,10,110);
g.drawString("smallest of entered no. is "+s_small,10,130);
g.drawString("largest of entered no. is "+s_large,10,150);
}
public boolean action(Event event, Object obj){
repaint(0);
return true;
}
}
31
THE END
Thank You
32