Java Applet Basics
Java applets,however,run from inside a
world wide web browser. a reference to an applet is embedded in a webpage using
a special HTML tag.
Because java applets run inside a java
browser,they have the advantage of the structure the browser provides: an existing window,an event-handling and graphics
context,and the surrounding user interface.
Java applets can be downloaded from
anywhere and run on a client's system ,restrictions are necessary to prevent an
applet from causing system damage or security breaches.
some of the restrictions on
applet can be as following
· applets can't read or write to the reader's file system,except in
specific directories.some browsers may not even allow an applet to read or
write to the file system at all
· applets can't communicate with a server other than the one that had
originally stored the applet.
· applets can't run any program on the reader's system.for Unix
system.
· applets can't load program native to the local platform,including
shared libraries such as DLLs
EXAMPLE :1
open a new file in he
editor and type the following code.
File name: MyFirstApplet.java
import java.awt.*;
public class MyFirstApplet extends java.applet.Applet{
public void paint(Graphics g){
g.drawString("My first java applet",70,30);
}
}
/*
<applet CODE="MyFirstApplet.class" WIDTH=400
HEIGHT=400></applet>
*/
·
save this file,as My
FirstApplet.java and compile it using javac.this will result in the creation of
a class file for the same
·
now create on HTML file
and within its body,type
Output
Passing Parameter to Applet
File name: BarChart.java
import
java.awt.*;
import
java.applet.*;
public class
BarChart extends Applet
{
int n=0;
String
label[];
int value[];
public void
init()
{
try
{
n=Integer.parseInt(getParameter("coloumn"));
label=new
String[n];
value=new
int[n];
label[0]=getParameter("label1");
label[1]=getParameter("label2");
label[2]=getParameter("label3");
label[3]=getParameter("label4");
value[0]=Integer.parseInt(getParameter("c1"));
value[1]=Integer.parseInt(getParameter("c2"));
value[2]=Integer.parseInt(getParameter("c3"));
value[3]=Integer.parseInt(getParameter("c4"));
}
catch(NumberFormatException
e){}
}
public void
paint(Graphics g)
{
for(int
i=0;i<n;i++)
{
g.setColor(Color.red);
g.drawString(label[i],20,i*50+30);
g.fillRect(50,i*50+10,value[i],40);
}
}
}
/*<applet
code="BarChart.class" width=300 Height=250>
<param
name="coloumns" value="4">
<param
name="c1" value="110">
<param
name="c2" value="150">
<param
name="c3" value="100">
<param
name="c4" value="170">
<param
name="label1" value="91">
<param
name="label2" value="92">
<param
name="label3" value="93">
<param
name="label4" value="94">
</applet>
*/
Output
PASSING
PARAMETERS TO APPLET
Testing the Result
An Applet Architecture
init()
The init() method is the first method to be
called. This is where you should initialize variables. This method is called
only once during the run time of your applet.
start()
The start() method is called after init(). It
is also called to restart an applet after it has been stopped. Whereas init()
is called once the first time an applet is loaded start() is called
Paint()
The
paint() method is called each time your applet's output must be redrawn.
Stop()
The
stop() method is called when a web browser leaves the HTML document containing
the applet
The repaint() Method
Repaint
() can be done mentioning time in miliseconds to be performed in future.
The Graphics
Class
import java. awt. Graphics;
public class MyClass extends java.applet.Applet{
...
}
Lines
File name: Lines.java
import java.awt.*;
import java.applet.*;
/*
<applet code="Lines" width=300 height=200>
</applet>
*/
public class Lines extends Applet{
public void paint(Graphics g){
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);
g.drawLine(40,25,250,180);
g.drawLine(75,90,400,400);
g.drawLine(20,150,400,40);
g.drawLine(5,290,80,19);
}
}
Output
Rectangles
The java
graphics primitives provide not just one, but three kinds of rectangles:
v Plain rectangles
v Rounded rectangles, which are
rectangles with rounded corners
v Three-dimensional rectangles, which
are drawn with a shaded border
Rounded
Rectangles
Rectangles
File name: Rectangles.java
import java.awt.*;
import java.applet.*;
/*
<applet code="Rectangles" width=300 height=200>
</applet>
*/
public class Rectangles extends Applet{
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);
}
}
Output
Rounded Rectangles
void drawRoundRect(int top, int left,
int width, int height, int)
xdiam, int y diam
void fillRoundRect (int top,int left,
int width, int height, int
xdiam, int y diam
public void paint(Graphics g){
g. draw Rect(20,20,60,60);
g.fil Rect(120,20,60,60);
}
The following applet draws several
rectangles:
public void paint(Graphics g){
g. draw Roun Rect
(20,20,60,6010,10);
g.fillRound Rect(120,20,60,60,20,20);
}
Three Dimensional Rectangles
draw 3DRect(int x1, int y1, int width,int height, true/false).
rectangles.public void paint(Gra[hics g){
g.draw
3DReact(20,20,60,60,true);
g.draw
3DReact(120,20,60,60,false);
}
Polygons
draw polygon (int x[], int y[],int jun points)
fil Polygon (int x[], int y[],int jun points)
Polygon
File name: HourGlass.java
import java.awt.*;
import java.applet.*;
/*
<applet code="HourGlass" width=230 height=210>
</applet>
*/
public class HourGlass extends Applet{
public void paint(Graphics g){
int xpoints[]={30,200,30,200,30};
int ypoints[]={30,30,200,200,30};
g.drawPolygon(xpoints,ypoints,5);
}
}
Output
Ovals
File name: Ellipses.java
import java.awt.*;
import java.applet.*;
/*
<applet code="Ellipses" width=300 height=200>
</applet>
*/
public class Ellipses extends Applet{
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);
}
}
Output
Arc
draw Are (int top, int left, int width, int height,int start
angle, int sweep
angle)
fillAre(int top, int left, int width, int height,int start
angle, int sweep
angle)
File name: Arcs.java
import java.awt.*;
import java.applet.*;
/*
<applet code="Arcs" width=300 height=200>
</applet>
*/
public class Arcs extends Applet{
public void paint(Graphics g){
g.drawArc(10,40,70,70,0,75);
g.drawArc(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);
g.fillArc(70,40,50,80,0,90);
}
}
Output
File name: Lamp.java
import java.awt.*;
/*
<applet code="Lamp" width=300 height=200>
</applet>
*/
public class Lamp extends java.applet.Applet{
public void paint(Graphics g){
g.fillRect(0,250,290,290);
g.drawLine(125,250,125,160);
g.drawLine(175,250,175,160);
g.drawArc(85,157,130,50,-65,312);
g.drawArc(85,87,130,50,62,58);
g.drawLine(85,177,119,89);
g.drawLine(215,177,181,89);
g.fillArc(78,120,40,40,63,-174);
g.fillOval(120,96,40,40);
g.fillArc(173,100,40,40,110,180);
}
}
Output
Creating Font Objects
Creating Font Objects
Font f = new Font("Time Roman",Font,BOLD,24);
Font methods
Name |
In Object |
Action |
GetFont() |
Graphics |
Returns the current font object as
previously set by |
GetName() |
Font |
Returns the name of the font as a
string |
getSize() |
Font |
returns the current font size (an integer)
|
GetStyle() |
Font |
Returns the current style of the
font (stryle are integer constants: o is plain, 1 is bold,2 is italic,3 is
bold italic) |
isPlain() |
Font |
Return true or false if the font's
styleis plain |
is Bold() |
Font |
Returns true or false if the font's
style is bold |
isItalic() |
Font |
Returns true or false if the font's
style is italic |
Fontf=new
Font("TimesRoman",Font.BOLD,36);
Font Metrics fmetrics =
get FontMetrics(f);
g.setfont(f);
File name: ManyFonts.java
/*<applet code="ManyFonts" width=300 height=300>
</applet>
*/
import java.awt.*;
import java.awt.Font;
import java.awt.Graphics;
public class ManyFonts extends java.applet.Applet{
public void paint(Graphics g){
Font f=new Font("TimesRoman",Font.PLAIN,18);
Font fb=new Font("TimesRoman",Font.BOLD,18);
Font fi=new Font("TimesRoman",Font.ITALIC,18);
Font fbi=new Font("TimesRoman",Font.BOLD+Font.ITALIC,18);
g.setFont(f);
g.drawString("This is a plain font",10,25);
g.setFont(fb);
g.drawString("This is a bold font",10,50);
g.setFont(fi);
g.drawString("This is an italic font",10,75);
g.setFont(fbi);
g.drawString("This is a bold italic font",10,100);
}
}
Output
Font Metrics Methods
File name: FontMetricsDemo.java
import java.applet.*;
import java.awt.*;
import java.awt.Font;
import java.awt.FontMetrics;
/*
<applet code=FontMetricsDemo width=300 height=300>
</applet>
*/
public class FontMetricsDemo extends Applet{
Font f1,f2;
int ascent,descent,height,leading;
String one,two,three,four;
public void init(){
f1=new Font("Helvetica",Font.BOLD,14);
f2=new Font("TimesRoman",Font.BOLD+Font.ITALIC,12);
}
public void paint(Graphics g){
g.setFont(f1);
ascent=g.getFontMetrics().getAscent();
descent=g.getFontMetrics().getDescent();
height=g.getFontMetrics().getHeight();
leading=g.getFontMetrics().getLeading();
one="Ascent of font f1 is: "+ascent;
two="Descent of font f1 is: "+descent;
three="Height of font f1 is: "+height;
four="Leading of font f1 is: "+leading;
g.drawString(one,20,20);
g.drawString(two,20,50);
g.drawString(three,20,80);
g.drawString(four,20,110);
}
}
Output
Font Metrics
File name: Centered.java
import java.awt.*;
import java.applet.*;
import java.awt.Graphics;
import java.awt.Font;
import java.awt.FontMetrics;
/*
<applet code=Centered width=300 height=300>
</applet>
*/
public class Centered extends java.applet.Applet{
public void paint(Graphics g){
Font f=new Font("TimesRoman",Font.PLAIN,36);
FontMetrics fm=getFontMetrics(f);
g.setFont(f);
String s="This is how the world ends.";
int xstart=(size().width-fm.stringWidth(s))/2;
int ystart=(size().height+fm.getHeight())/2;
g.drawString(s,xstart,ystart);
}
}
Output
Color
Color
Using Color Objects
Standard colors
Color Name |
RGB
Value |
Color.White |
255,255,255 |
Color.black |
0,0,0 |
Color.lightGray |
192,192,192 |
Color.gray |
128,128,128 |
Color.darkGray |
64,64,64 |
Color.red |
255,0,0 |
Color.green |
0,255,0 |
Color.blue |
0,0,255 |
Color.yellow |
255,255,0 |
Color.magenta |
255,0,255 |
Color.cyan |
0,255,255 |
Color.pink |
255,175,175 |
Color.orange |
255,200,0 |
Testing and Setting the
Current Colors
File name: ColorAppletDemo.java
import java.awt.*;
import java.awt.Font;
import java.applet.*;
/*
<applet code=ColorAppletDemo width=300 height=300>
</applet>
*/
public class ColorAppletDemo extends Applet{
public void paint(Graphics g){
Font f=new Font("TimesRoman",Font.BOLD,20);
Font f1=new Font("Courier",Font.ITALIC,20);
Font f2=new Font("Helevitica",Font.PLAIN,20);
g.setColor(Color.red);
g.setFont(f);
g.drawString("Be Happy.Be hopeful",30,30);
g.setColor(Color.blue);
g.setFont(f1);
g.drawString("Be happy.Be hopeful",30,70);
g.setColor(Color.pink);
g.setFont(f2);
g.drawString("Be happy.Be hopeful",30,110);
}
}
Output
0 comments:
Post a Comment