Applet
======
All applets are subclasses of
Applet. Thus, all applets must import
java.applet.
Applets must also
import java.awt.
It is a window to hold the
awt components.
When an applet begins, the
AWT calls the following method:
init()
start()
paint
When an applet is terminated,
the following sequence of method calls takes place:
stop()
destroy()
init()
-----
The init() method is the
first method to be called.
This method is called only
once during the run time of our applet.
start()
-------
The start() method is called
after init.It is also called to restart an applet after it has been stopped.
paint()
--------
The paint() method is called
each time our applet's output must be redrawn.
stop()
------
The stop() method to suspend
threads that don't need to run when the applet is not visible. we can restart them when start() is called if
the user returns to the page.
SYNTAX:
<APPLET
[CODEBASE=URL]
CODE=filename
[ALT=alternateText]
[NAME=instancename]
WIDTH=pixels
HEIGHT=pixels
[ALIGN=alignment]
[VSPACE=pixels]
[HSPACE=pixels]
>
[<PARAM NAME=name value=value>]
.
.
[<PARAM NAME=name value=value>]
<APPLET>
CODEBASE:
The URL that specifies the directory in which the applet code
is to be found.
CODE:
The name of the applet file,including the extension .class
ALT:
The text to be displayed if a browser supprts applets but
cannot run this one for some reason.
NAME:
The name of the applet in the Web browser. Applet must be given names if we want other
applets to be able to find and interact with them.
WIDTH:
The width of the space reserved for the applet.
HEIGHT:
The height of the space reserved for the applet.
ALIGN:
Specifies the alignment of the applet:
LEFT,RIGHT,TOP,BOTTOM,MIDDLE,BASELINE,TEXTTOP,ABSMIDDLE,
or ABSBOTTOM.
VSPACE:
The space allocated above and below the applet.
HSPCE:
The space allocated to the right and left around the applet.
PARAM NAME:
The name of a parameter to pass to the applet.
PARAM VALUE:
The value of a parameter.
syntax:
<applet code="program
name" height=200 width=200></applet>
To run
--------
appletviewer filename.java
Simple Applet Display Methods
------------------------------------
syntax:
void drawString(String message,int x,int y);
w.html
<html>
<head>
<title>
Welcome to India
</title>
</head>
<body bgcolor=pink>
<center>
Hello Friends
<hr color=red>
</center>
</body>
</html>
ex:
applet.html
<HTML>
<HEAD>
<TITLE>APPLET</TITLE>
</HEAD>
<BODY>
<HR>
<CENTER>
<APPLET
CODE=applet.class
WIDTH=200
HEIGHT=200
HSPACE=400
>
</APPLET>
</CENTER>
<HR>
</BODY>
</HTML>
applet.java
import java.applet.*;
import java.awt.*;
public class applet extends
Applet
{
public void paint(Graphics g)
{
g.drawString("HELLO FRIENDS",75,100);
}
}
import java.applet.*;
import java.awt.*;
//<applet
code="SimpleApplet" width=300 height=75>
public class SimpleApplet extends Applet
{
public void paint(Graphics g)
{
g.drawString("A simple
Applet",100,60);
}
}
Applets do not need a main()
method.
Applets must be run under an
applet viewer or a Java-compatible browser.
Example program for Applet
life cycle
import java.awt.*;
import java.applet.*;
//<applet
code="Appmethods" height=300 width=300></applet>
public class Appmethods
extends Applet
{
String s="";
public void init()
{
s+="Initialisation";
}
public void start()
{
s+="Start";
}
public void stop()
{
s+="Stop";
}
public void destroy()
{
System.out.println("Destroy");
}
public void paint(Graphics g)
{
g.drawString(s,100,100);
}
}
--------------
To set the background color
of an applet's window, use setBackground().
To set foreground color use
setForeground().
Color defines
==============
Color.black
Color.gray
Color.lightgray
Color.magenta
Color.cyan
.
.
.
ex:
setBackground(Color.green);
setForeground(Color.red);
import java.awt.*;
import java.applet.*;
/*
<applet code=Sample
width=300 height=200></applet>
*/
public class Sample extends
Applet
{
String msg;
public void init()
{
setBackground(Color.cyan);
setForeground(Color.red);
msg="Inside init()--";
}
public void start()
{
msg+="Inside start()--";
}
public void paint(Graphics g)
{
msg+="Inside paint().";
g.drawString(msg,10,30);
}
}
Using the Status Window
import java.awt.*;
import java.applet.*;
/*
<applet code=StatusWindow"
width=300 height=50>
</applet>
*/
public class StatusWindow
extends Applet
{
public void init()
{
setBackground(Color.pink);
}
public void paint(Graphics g)
{
g.drawString("This is the applet
wiondow.",10,20);
showStatus("This is show in the status
window.");
}
}
Reading Para meters
in Applets
------------------------------------
Get the value of a parameter,
we use the Applet class’s
getParameter method, passing
it the name of the parameter as specified in the <PARA M>
tag.
Ex:
import java.applet.*;
import java.awt.*;
/*
<applet
code=para.class
width=200
height=200>
<PARAM NAME=str VALUE="Hello Friends">
</applet>
*/
public class para extends
Applet
{
public void paint(Graphics g)
{
g.drawString(getParameter("str"),50,100);
}
}
import java.applet.*;
import java.awt.*;
/*
<applet
code=HelloPara.class
width=200
height=200>
<PARAM NAME=wish VALUE="Surya">
</applet>
*/
public class HelloPara
extends Applet{
Font f=new Font("Times New Roman",Font.ITALIC,30);
String Name;
public void init(){
Name=getParameter("wish");
if(Name==null)
Name="Joe";
Name="Hello "+Name;
}
public void paint(Graphics g)
{
g.setFont(f);
g.setColor(Color.red);
g.drawString(Name,50,100);
}
}
CODEBASE : contains an
alternate pathname where the classes can be located.
Ex:
Html/myapplets/DifferPara
import java.applet.*;
import java.awt.*;
public class DifferPara
extends Applet{
Font f;
String Name;
int value;
public void init(){
Name=getParameter("font");
if(Name==null)
Name="CourierNew";
String s=getParameter("size");
if(s==null)
value=15;
else
value=Integer.parseInt(s);
f=new Font(Name,Font.ITALIC,value);
}
public void paint(Graphics g)
{
g.setFont(f);
g.setColor(Color.red);
g.drawString("Hello Friends",50,100);
}
}
--
html/DifferPara.html
<html>
<body>
<applet
code=DifferPara.class
codebase=myapplet
width=300
height=200>
<PARAM NAME=font VALUE="Arial">
<PARAM NAME=size VALUE=40>
</applet>
Working with Graphics
------------------------
Drawing Lines
void drawLine(int startX,int
startY,int endX,int endY)
//Draw Lines
import java.awt.*;
import java.applet.*;
/*
<applet
code="Lines" width=300 height=200>
</applet>
*/
public class Lines extends
Applet
{
public void init()
{
setBackground(Color.green);
setForeground(Color.black);
}
public void paint(Graphics g)
{
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);
}
}
Drawing Rectangles
==================
void drawRect(int left,int top,int
width,int height)
void fillRect(int left,int top,int
width,int height)
void drawRoundRect(int left,int
top,int width,int height,int xDiam,int yDiam)
void fillroundRect(int left,
int top,int width,int height, int xDiam,int YDiam)
//Draw Rectangles
import java.awt.*;
import java.applet.*;
/*
<applet code="Rectangle"
width=300 height=200>
</applet>
*/
public class Rectangle
extends Applet
{
public void init()
{
setBackground(Color.green);
setForeground(Color.red);
}
public void paint(Graphics g)
{
g.drawRect(10,10,60,50);
g.fillRect(100,10,60,50);
g.drawRoundRect(190,10,60,50,15,15);
g.fillRoundRect(70,90,140,100,30,40);
}
}
Drawing Ellipses and Circles
------------------------------
void drawOval(int left,int top,int
width,int height)
void fillOval(int left,int top,int
width,int height)
//Draw Ellipses
import java.awt.*;
import java.applet.*;
/*
<applet
code="Ellipses" width=300 height=200>
</applet>
*/
public class Ellipses extends
Applet
{
public void init()
{
setBackground(Color.green);
setForeground(Color.red);
}
public void paint(Graphics g)
{
g.drawOval(10,10,50,50);
g.fillOval(100,10,75,50);
g.drawOval(190,10,90,30);
g.fillOval(70,90,140,100);
}
}
Drawing Arcs
--------------
void drawArc(int left,int top,int
width,int height,int startAngle,int endAngle)
void fillArc(int left,int top,int
width,int height,int startAngle,int endAngle)
//Draw Arcs
import java.awt.*;
import java.applet.*;
/*
<applet
code="Arcs" width=300 height=200>
</applet>
*/
public class Arcs extends
Applet
{
public void init()
{
setBackground(Color.green);
setForeground(Color.black);
}
public void paint(Graphics g)
{
g.drawArc(10,40,70,70,0,75);
g.fillArc(100,40,70,70,0,75);
g.drawArc(10,100,70,80,0,175);
g.fillArc(100,100,70,90,0,270);
g.drawArc(200,80,80,80,0,180);
}
}
Drawing Polygons
================
void drawPolygon(int x[],int
y[],int numPoints)
void fillPolygon(int x[],int
y[],int numPoints)
//Draw Polygon
import java.awt.*;
import java.applet.*;
/*
<applet
code="Polygon" width=300 height=200>
</applet>
*/
public class Polygon extends
Applet
{
public void paint(Graphics g)
{
int xpoints[]={30,200,30,200,30};
int ypoints[]={30,30,200,200,30};
int num=5;
g.drawPolygon(xpoints,ypoints,num);
}
}
---------------------
Working with Color
-----------------------
We can also create our own
colors using one of the color construcotrs.
Color(int red,int green,int
blue);
ex:
new Color(255,100,100);//light red.
Color(int rgbValue);
Takes single integer that
contains the mix of red,green,and blue packed into an integer.
red =16 - 23
green =8 to 15
blue = 0 -7
Color(float red,float
green,float blue)
Three float values(between
0.0 - 1.0) that specify the relative mix of red,green, and blue.
//Color
import java.awt.*;
import java.applet.*;
/*
<applet
code="ColorDemo" width=300 height=200>
</applet>
*/
public class ColorDemo
extends Applet
{
public void paint(Graphics g)
{
Color c1=new Color(255,100,100);
Color c2=new Color(100,255,100);
Color c3=new Color(100,100,255);
g.setColor(c1);
g.drawLine(0,0,100,100);
g.setColor(c2);
g.drawLine(40,25,250,180);
g.setColor(c3);
g.drawLine(20,150,400,400);
g.setColor(Color.red);
g.drawOval(10,10,50,50);
g.fillOval(70,90,140,100);
g.setColor(Color.blue);
g.drawOval(190,10,90,30);
g.setColor(Color.cyan);
g.fillRect(100,10,60,50);
}
}
Creating a Font:
================
Font(String fontName,int
fontStyle,int pointSize)
fontName:
-------------
Specifies the name of the
desired font.
ex: Sans Serif,
Serif,Symbol,....
fontStyle:
-------------
Three constants:
Font.PLAIN,Font.BOLD,Font.ITALIC
Font.BOLD|Font.ITALIC
- specifies a bold,italics style.
pointSize
---------
Specified by fontSize.
import java.awt.*;
import java.applet.*;
/*
<applet code=SampleFont
width=300 height=200></applet>
*/
public class SampleFont
extends Applet
{
public void init()
{
Font f=new Font("Arial",Font.BOLD,55);
setFont(f);
setBackground(Color.orange);
setForeground(Color.red);
}
public void paint(Graphics g)
{
g.drawString("Hai Friends",30,80);
}
}
Font
Information Methods
--------------------------------
Method
Name Object Return
Value
picture 1
Method Name
|
Object
|
Return Value
|
getFont()
|
Graphics
|
Current font object
previously set by setFont
|
getName()
|
Font
|
Current font name, a String
|
getSize()
|
Font
|
Current font size, an int
|
getStyle()
|
Font
|
Current font style, an int
constant( 0 is plain ; 1 is bold ; 2 is italic)
|
isBold()
|
Font
|
true, if font style is
bold; else false
|
isItalic()
|
Font
|
true, if font style is
italic, else false
|
isPlain()
|
Font
|
true, if font style is
plain, else false
|
Ex:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
//<applet
code=Fontdiff.class width=400 height=400></applet>
public class Fontdiff extends
Applet
{
public void paint(Graphics g)
{
String fontName;
Font f=new Font("Arial",Font.BOLD,20);
Font
f1=new Font("TimesNewRoman ",Font.ITALIC,20);
Font f2=new
Font("ComicSansMS",Font.PLAIN,20);
fontName=f.getName();
g.setColor(Color.blue);
g.setFont(f);
g.drawString(fontName,10,25);
g.setFont(f1);
g.setColor(Color.yellow);
int fontValue=f1.getSize();
String s="Font size is"+fontValue;
g.drawString(s,10,50);
g.setFont(f2);
g.setColor(Color.green);
g.drawString("good night",10,75);
}
}
----------------------------------
Image
---------
Image getImage(URL ul,STring
imagename);
Displaying an Image
=======================
boolean drawImage(Image imgObj,int left,int
top,width,height,ImageObserver imgob);
import java.awt.*;
import java.applet.*;
//<applet code=Picture width=250
height=250></applet>
public class Picture extends Applet
{
Image i;
public void init(){
i=getImage(getDocumentBase(),"132024.jpg");
}
public void paint(Graphics g)
{
g.drawImage(i,0,0,100,200,this);
}
}
import java.awt.*;
import java.applet.*;
/*
<applet
code="Face" width=300 height=50>
</applet>
*/
public class Face extends
Applet
{
public void init()
{
setBackground(Color.pink);
}
public void paint(Graphics g)
{
g.drawOval(40,40,120,150);//head
g.drawOval(57,75,30,20);//Left eye
g.drawOval(110,75,30,20);//Right eye
g.fillOval(68,81,10,10);//pupil left
g.fillOval(121,81,10,10);//pupil right
g.drawOval(85,100,30,30);//Nose
g.fillArc(60,125,80,40,180,180);//Mouth
g.drawOval(25,92,15,30);//Left ear
g.drawOval(160,92,15,30);//Right ear
}
}
==========
import java.applet.*;
import java.awt.*;
/*<applet
code="smile" width=300 height=300></applet>*/
public class smile extends
Applet
{
public void init()
{
setBackground(Color.cyan);
setSize(1000,500);
}
public void paint(Graphics g)
{
Color c1=new
Color(200,220,100);
Color c2=new
Color(100,100,100);
Color c3=new
Color(255,255,255);
g.drawOval(560,105,20,30);
g.drawOval(220,105,20,30);
g.drawOval(205,85,40,80);
g.drawOval(555,85,40,80);
g.setColor(c1);
g.fillOval(200,70,400,400);
g.setColor(c2);
g.fillOval(310,170,20,30);
g.fillOval(460,170,20,30);
g.drawArc(280,140,70,70,25,140);
g.drawArc(440,140,70,70,25,140);
g.setColor(c3);
g.drawOval(450,155,50,50);
g.drawOval(445,150,60,60);
g.drawOval(290,155,50,50);
g.drawOval(285,150,60,60);
g.drawLine(340,180,450,180);
g.drawLine(240,145,289,175);
g.drawLine(550,145,500,175);
g.drawLine(395,155,345,255);
g.drawLine(395,155,395,255);
g.drawLine(395,155,445,255);
g.fillArc(290,300,200,50,100,1000);
g.drawArc(280,280,220,70,0,180);
g.drawArc(279,278,222,70,0,180);
g.drawArc(345,245,50,20,180,180);
g.drawArc(395,245,50,20,180,180);
}
}
No comments:
Post a Comment