0% found this document useful (0 votes)
21 views21 pages

11 Applet

Uploaded by

Chetan laddha
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)
21 views21 pages

11 Applet

Uploaded by

Chetan laddha
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/ 21

Java Programming | CCIT

1
Java Programming | CCIT

Table of Contents
Applets .................................................................. 3
Applet Tag ........................................................... 3
Life cycle methods for Applet .............................. 4
Class Applet ......................................................... 5
Class Applet Hierarchy ......................................... 6
AppletViewer ....................................................... 8
Class Graphics ...................................................... 9
class Color .......................................................... 14
RGB Color Modal ............................................... 20
HSB Color Modal ............................................... 20

2
Java Programming | CCIT

Applets

 A Java applet is a specially designed Java class which can be used within web
pages to enhance them.
 A browser enabled with Java technology can download the applet from the
internet and run it.
 Applets have strict security rules that are enforced by the Web browser.

Applet Tag
 Applets can be added into web pages by using applet tag.
Example:-
<APPLET code=className width=w height=h alt="alternate Text" >
<PARAM name=pname value=val >
-----
</APPLET>
where
Code - A URL of applet class.
Height - Height to display the applet.
width - Width to display the applet.
al - Alternate text to be displayed in case browser does not support applet.
 Param tag is used to pass additional parameters to applet

3
Java Programming | CCIT

Life cycle methods for Applet


 When Browser finds Applet tag in webpage.
 It downloads the specified Applet class.
 It loads the Applet class.
 It creates Object of Applet Class.
 It calls its different methods on different events

 There are 5 main events in life of Applet


1] init 2]start 3] paint 4] stop 5] destroy
According to these event applet container call its 5 different methods.
 public void init()
o is used to initialized the Applet.
o It is invoked only once.
 public void start()
o is invoked when Applet scrolls into View or Restored.
o It is used to start the Applet.
 public void stop()
o is used to stop the Applet.
o It is invoked when Applet scrolls out of view or browser is minimized.
 public void destroy()
o is used to destroy the Applet.
o It is invoked only once.
 public void paint(Graphics g)
o is used to paint the Applet. It provides Graphics class object that can be used
for drawing oval, rectangle, arc etc.
4
Java Programming | CCIT

Class Applet [ java.applet ]


 This is the base class from which all applets are derived.
Methods:-
 public void init()
o is used to initialized the Applet.
o It is invoked only once.
 public void start()
o is invoked when Applet scrolls into View or Restored.
o It is used to start the Applet.

 public void stop()


o is used to stop the Applet.
o It is invoked when Applet scrolls out of view or browser is minimized.
 public void destroy()
o is used to destroy the Applet.
o It is invoked only once.
 public void paint(Graphics g)
o is used to paint the Applet. It provides Graphics class object that can be used
for drawing oval, rectangle, arc etc.
NOTE: - we can override these above methods to perform our task on these events.
 public void showStatus( String msg)
o Will display msg into applet containers status bar
 public URL getCodeBase()
o Gets the base URL.
o This is the URL of the directory which contains this applet.
 public URL getDocumentBase()
o Gets the URL of the document in which this applet is embedded.
 public String getParameter(String name)
o Returns the value of the named parameter.
 public Image getImage(URL url)

5
Java Programming | CCIT

o Returns an Image object that can then be painted on the screen.


 public void play(URL url)
o Plays the audio clip at the specified URL.

Class Applet Hierarchy

6
Java Programming | CCIT

Example:-
import java.applet.*;
import java.awt.*;
public class MyApplet extends Applet
{
String s="";
public void destroy()
{
s=s+"destroy ";
showStatus(s);
}
public void paint(Graphics g)
{
s=s+"paint ";
showStatus(s);
}
public void stop()
{
s=s+"stop ";
showStatus(s);
}
public void start()
{
s=s+"start ";
showStatus(s);
}
public void init()
{
s=s+"init ";
showStatus(s);
}
}
//<applet code=MyApplet width=300 height=300 ></applet> 7
Java Programming | CCIT

init start paint

NOTE: -
 Applet is an old technology.
 In old days they were supported by browsers.
 Now browsers have stopped supporting Applets.
 But still we can test our Applet by using a tool AppletViewer.
 This tool was available till jdk1.8.

AppletViewer
 It is a tool generally used by developers for testing their applets before deploying
them to a website.
Syntax :
appletviewer filename

 It loads the applet class specified in applet tag of the file and calls applet different
life cycle methods of applet on different events.

8
Java Programming | CCIT

Class Graphics [ java.awt ]


 This class provides us different methods to perform drawing and painting
operations.
Methods:-
 void drawRect(int x,int y,int w,int h)
 void fillRect(int x,int y,int w,int h)
 void drawOval(int x,int y,int w,int h)
 void fillOval(int x,int y,int w,int h)
 void drawRoundRect(int x,int y,int w,int h,int rx,int ry)
 void fillRoundRect(int x,int y,int w,int h,int rx,int ry)
 void drawArc(int x,int y,int w,int h,int startAngle,int arcAngle)
 void fillArc(int x,int y,int w,int h,int startAngle,int arcAngle)
 void drawLine(int x1 , int y1 , int x2 , int y2)
 void drawPolygon(int x[ ] , int y[ ] , int n)
 void fillPolygon(int x[ ] , int y[ ] , int n )
 void drawString(String text , int x , int y)
 void setColor(Color c)
 void setFont(Font f)
 drawImage(Image img, int x, int y, ImageObserver obj)
 drawImage(Image img, int x, int y, int w, int h,
ImageObserver obj)

9
Java Programming | CCIT

Design an Applet to display 2 concentric Circles in Applet of size 200 X 200.

import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.drawOval(0,0,200,200);
g.drawOval(50,50,100,100);
}
}

//<applet code=MyApplet width=200 height=200 ></applet>

10
Java Programming | CCIT

Design an Applet of size 200 X 200 containing 10 concentric circles.

import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
for(int i=0;i<10;i++)
g.drawOval(i*10,i*10,200-i*20,200-i*20);
}
}

//<applet code=MyApplet width=200 height=200 ></applet>

11
Java Programming | CCIT

Design an Applet of size 200 X 200 containing 4 squares.

import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.fillRect(5,5,90,90);
g.fillRect(105,5,90,90);
g.fillRect(5,105,90,90);
g.fillRect(105,105,90,90);
}
}

//<applet code=MyApplet width=200 height=200 ></applet>

12
Java Programming | CCIT

Design an Applet of size 200 X 200 containing a Smile Face.

import java.applet.Applet;
import java.awt.Graphics;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.drawOval(0,0,200,200);
g.drawArc(30,30,140,140,180,180);
g.fillOval(50,50,30,30);
g.fillOval(120,50,30,30);
}
}

//<applet code=MyApplet width=200 height=200 ></applet>

13
Java Programming | CCIT

class Color [ java.awt ]


 An Object of this type represents a Color.
 This class provides us 12 ready made color objects
Data member:-
 public static final Color red;
 public static final Color blue;
 public static final Color green;
 public static final Color yellow;
 public static final Color orange;
 public static final Color pink;
 public static final Color magenta;
 public static final Color cyan;
 public static final Color white;
 public static final Color black;
 public static final Color darkGray;
 public static final Color lightGray;
For ex:-
g.setColor(Color.red);
g.drawOval(10,20,200,100);
Constructors:-
 Color(int r, int g, int b)
o Creates a color with specified RGB components
o values are in range 0 – 255
For ex: Color c=new Color(0,0,255);
g.setColor(c);

14
Java Programming | CCIT

 Color(float r, float g, float b)


o creates a color with specified RGB components
o values are in range 0.0 – 1.0
For ex: Color c=new Color( 0.0f,0.0f,1.0f);
g.setColor(c);
Methods:-
 Color darker()
o Creates a new Color that is a darker version of this Color.
For ex: Color c1=Color.red;
Color c2=c1.darker();
 Color brighter()
o Creates a new Color that is a brighter version of this Color.
Design an Applet of size 200 X 200 containing 10 concentric circles.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{ public void paint(Graphics g)
{
for(int i=0;i<10;i++)
{
if(i%2==0)
g.setColor(Color.blue);
else
g.setColor(Color.red);
g.fillOval(i*10,i*10,200-i*20,200-i*20);
}
}
}
15
//<applet code=MyApplet width=200 height=200 ></applet>
Java Programming | CCIT

Design an Applet of size 200 X 200 containing 4 squares.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillRect(5,5,90,90);
g.setColor(Color.green);
g.fillRect(105,5,90,90);
g.setColor(Color.blue);
g.fillRect(5,105,90,90);
g.setColor(Color.yellow);
g.fillRect(105,105,90,90);
}
}
//<applet code=MyApplet width=200 height=200 ></applet>

16
Java Programming | CCIT

Design an Applet of size 200 X 200 containing the following figure.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
int xp[]={100,200,0};
int yp[]={0,170,170};
g.setColor(Color.blue);
g.fillPolygon(xp,yp,3);
g.setColor(Color.orange);
g.fillOval(75,75,50,50);
g.setColor(Color.red);
g.drawString("CCIT Amravati",65,190);
}
}

//<applet code=MyApplet width=200 height=200 ></applet>

17
Java Programming | CCIT

Design an Applet of size 200 X 200 containing 12 Pie Slice.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
Color co[]={Color.red,Color.blue,Color.green,
Color.magenta,Color.cyan,Color.yellow };
for(int i=0;i<12;i++)
{
g.setColor(co[i%6]);
g.fillArc(0,0,200,200,i*30,30);
}
}
}

//<applet code=MyApplet width=200 height=200 ></applet>

18
Java Programming | CCIT

Design an Applet of size 256 X 200 containing 256 lines of different colors from
red to blue.

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;
public class MyApplet extends Applet
{
public void paint(Graphics g)
{
for(int x=0;x<256;x++)
{
Color clr=new Color(255-x,0,x);
g.setColor(clr);
g.drawLine(x,0,x,200);
}
}
}

//<applet code=MyApplet width=256 height=200 ></applet>

19
Java Programming | CCIT

RGB Color Modal


 It is used to create color object by using the RGB (Red, Green, Blue) color model.
Syntax :
Color c=new Color(red, green,blue)

o red : a value from 0 and 255 to indicate the amount of red.


o green : a value from 0 and 255 to indicate the amount of green.
o blue : a value from 0 and 255 to indicate the amount of blue.
For ex:- Color c=new Color(255, 0, 255);

HSB Color Modal


 It is used to create color object by using the HSB color model .
Syntax :
static Color getHSBColor(hue,saturation,brightness)

 Hue
o It is number between 0.0 and 1.0 which indicates the hue of the color.
 Saturation
o It is number between 0.0 and 1.0 which indicates color depth.
o 1 will make the color as deep as possible.
o 0 will take all the color out of the mixture and make it a shade of gray.
 Brightness
o Is is decimal number between 0.0 and 1.0 which indicates brightness.
o 1 will make the color as light as possible.
o 0 will make it very dark.

20
Java Programming | CCIT

21

You might also like