Sunday, April 1, 2012

Applet


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 Parameters 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 <PARAM> 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

Slider

Image Slider By engineerportal.blogspot.in The slide is a linking image  Welcome to Engineer Portal... #htmlcaption

Tamil Short Film Laptaap

Tamil Short Film Laptaap
Laptapp

Labels

About Blogging (1) Advance Data Structure (2) ADVANCED COMPUTER ARCHITECTURE (4) Advanced Database (4) ADVANCED DATABASE TECHNOLOGY (4) ADVANCED JAVA PROGRAMMING (1) ADVANCED OPERATING SYSTEMS (3) ADVANCED OPERATING SYSTEMS LAB (2) Agriculture and Technology (1) Analag and Digital Communication (1) Android (1) Applet (1) ARTIFICIAL INTELLIGENCE (3) aspiration 2020 (3) assignment cse (12) AT (1) AT - key (1) Attacker World (6) Basic Electrical Engineering (1) C (1) C Aptitude (20) C Program (87) C# AND .NET FRAMEWORK (11) C++ (1) Calculator (1) Chemistry (1) Cloud Computing Lab (1) Compiler Design (8) Computer Graphics Lab (31) COMPUTER GRAPHICS LABORATORY (1) COMPUTER GRAPHICS Theory (1) COMPUTER NETWORKS (3) computer organisation and architecture (1) Course Plan (2) Cricket (1) cryptography and network security (3) CS 810 (2) cse syllabus (29) Cyberoam (1) Data Mining Techniques (5) Data structures (3) DATA WAREHOUSING AND DATA MINING (4) DATABASE MANAGEMENT SYSTEMS (8) DBMS Lab (11) Design and Analysis Algorithm CS 41 (1) Design and Management of Computer Networks (2) Development in Transportation (1) Digital Principles and System Design (1) Digital Signal Processing (15) DISCRETE MATHEMATICS (1) dos box (1) Download (1) ebooks (11) electronic circuits and electron devices (1) Embedded Software Development (4) Embedded systems lab (4) Embedded systems theory (1) Engineer Portal (1) ENGINEERING ECONOMICS AND FINANCIAL ACCOUNTING (5) ENGINEERING PHYSICS (1) english lab (7) Entertainment (1) Facebook (2) fact (31) FUNDAMENTALS OF COMPUTING AND PROGRAMMING (3) Gate (3) General (3) gitlab (1) Global warming (1) GRAPH THEORY (1) Grid Computing (11) hacking (4) HIGH SPEED NETWORKS (1) Horizon (1) III year (1) INFORMATION SECURITY (1) Installation (1) INTELLECTUAL PROPERTY RIGHTS (IPR) (1) Internal Test (13) internet programming lab (20) IPL (1) Java (38) java lab (1) Java Programs (28) jdbc (1) jsp (1) KNOWLEDGE MANAGEMENT (1) lab syllabus (4) MATHEMATICS (3) Mechanical Engineering (1) Microprocessor and Microcontroller (1) Microprocessor and Microcontroller lab (11) migration (1) Mini Projects (1) MOBILE AND PERVASIVE COMPUTING (15) MOBILE COMPUTING (1) Multicore Architecute (1) MULTICORE PROGRAMMING (2) Multiprocessor Programming (2) NANOTECHNOLOGY (1) NATURAL LANGUAGE PROCESSING (1) NETWORK PROGRAMMING AND MANAGEMENT (1) NETWORKPROGNMGMNT (1) networks lab (16) News (14) Nova (1) NUMERICAL METHODS (2) Object Oriented Programming (1) ooad lab (6) ooad theory (9) OPEN SOURCE LAB (22) openGL (10) Openstack (1) Operating System CS45 (2) operating systems lab (20) other (4) parallel computing (1) parallel processing (1) PARALLEL PROGRAMMING (1) Parallel Programming Paradigms (4) Perl (1) Placement (3) Placement - Interview Questions (64) PRINCIPLES OF COMMUNICATION (1) PROBABILITY AND QUEUING THEORY (3) PROGRAMMING PARADIGMS (1) Python (3) Question Bank (1) question of the day (8) Question Paper (13) Question Paper and Answer Key (3) Railway Airport and Harbor (1) REAL TIME SYSTEMS (1) RESOURCE MANAGEMENT TECHNIQUES (1) results (3) semester 4 (5) semester 5 (1) Semester 6 (5) SERVICE ORIENTED ARCHITECTURE (1) Skill Test (1) software (1) Software Engineering (4) SOFTWARE TESTING (1) Structural Analysis (1) syllabus (34) SYSTEM SOFTWARE (1) system software lab (2) SYSTEMS MODELING AND SIMULATION (1) Tansat (2) Tansat 2011 (1) Tansat 2013 (1) TCP/IP DESIGN AND IMPLEMENTATION (1) TECHNICAL ENGLISH (7) Technology and National Security (1) Theory of Computation (3) Thought for the Day (1) Timetable (4) tips (4) Topic Notes (7) tot (1) TOTAL QUALITY MANAGEMENT (4) tutorial (8) Ubuntu LTS 12.04 (1) Unit Wise Notes (1) University Question Paper (1) UNIX INTERNALS (1) UNIX Lab (21) USER INTERFACE DESIGN (3) VIDEO TUTORIALS (1) Virtual Instrumentation Lab (1) Visual Programming (2) Web Technology (11) WIRELESS NETWORKS (1)

LinkWithin