• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

Java Applet Tutorial for Beginners

April 17, 2012 by ProjectsGeek Leave a Comment

Java Applet Tutorial for Beginners



This tutorial is basically for Begineers who have no knowkedge of Applets before reading this tutorial . So , I am expecting that you know some basics of Java programming . If you are also new to Java programming I suggest you to go through java tutorial also here .


So , what is Applet in Short ?


Applet is small java program that can be placed on the HTML pages . Applet are featured with graphics and you can easily animate graphics by the help of Applet .


Example of Applet can be considered as the google ads you see on the Web pages that includes rich graphics .


Now starting with the other topics that need to be covered in the turorial .


Classification of Java programs


Java programs are classified into three groups:

  • Java Applications
  • Java Applets
  • Java Servlets.

Java Applications


Java Applications are executed on the command prompt on the desktop system like other windows application .


Java Applets


Applets are executed on the web browsers on client side .


So what is Applet Finally ?


An applet is a dynamic and interactive program that can run inside a web page displayed by a browser.


It is possible to download the applets and play applets on our system.


Applets can be used as rich graphics java programs on the Web .


Limitations of Applets

  1. Applets cannot read or write to the file system.
  2. Applets cannot communicate with any other server than the one ion which they were stored originally.
  3. Applets cannot run any programs on the system.

So, after reading limitations of Applets we are sure that applets can’t harm our computer with any other code . This feature of the applet makes it more popular on the Web .


Now , We should take a simple exmaple of applet in java . To create a applet on eclipse just follow these instructions :

  1. Create a Java Project .
  2. Add package and then class to the project .
  3. Now name the class as ” myApplet ” and put the given below in the class .
  4. Now run Project as Applet…Enjoy .

 import java.awt.*;  
import java.applet.*;
public class myApplet extends Applet
{
public void paint (Graphics g)
{
g.drawString (“Hello”, 70, 30);
}
}
A simple Applet with Parameters
public class myApplet extends Applet
{
String name;
public void init() { name=getParameter(“t1”); }
public void paint (Graphics g)
{
g.drawString (name, 70, 30);
}
}

If you want to embeeded the applet into Webpages just put the below code in the Webpage . Your applet.class file should be present in the same folder of your webpage .


HTML file to Run an Applet

Inside HTML file include tag.  

   




You can tune size of applet by changing WIDTH and HEIGHT values as required .


Now , it’s time to learn meathods that are present in applet class as derived from extended applet class .


Here are the Applet Methods


init() method :


This method gets called as soon as applet is started. Initialization of all variables, creation of objects, setting of parameters etc can be done in this method. 


start() method :


This method is executed after the init() method. When a browser is used to run an applet, any time it is reloaded, the execution begins from the start() method. 


stop() method :


This method is used to halt the running of an applet. When a browser is used  to run an applet, as soon as web browser is closed, this method is called. 


destroy() method :


This method is used to free the memory occupied by the variables and objects initialized in the applet. Any clean up activity that need to be performed can be done in this method. 


paint() method :


This method helps in drawing and creating a colored background or an image onto the applet. It always takes a Graphics Class object as an parameter. 


repaint() method :


This method calls the update() method, to clear the screen. The update() method in turn calls the paint() method.


Methods in a Graphics Class


These methods are inbuild methods which you can use to draw graphics on applet . You just have to pass coordinates for drawing figures and graphics .


drawBytes( byte[], int start, int end, int x, int y);
drawChars(char[], int start, int end, int x, int y);
drawString (String str, int x, int y);
drawLine(int x1, int y1, int x2, int y2);
drawRect(int x1, int y1, int w, int h);
fillRect(int x1, int y1, int w, int h);
drawRoundRect (int x1, int y1, int w, int h, int w1, int h1);
fillRoundRect (int x1, int y1, int w, int h, int w1, int h1);
draw3DRect(int x1, int y1, int w, int h, true/false);
drawPolygon (int xs, int ys, pts);
fillPolygon (int xs, int ys, pts);


Other  methods in a Graphics Class


drawOval(int x1, int y1, int w, int h);
fillOval(int x1, int y1, int w, int h);
drawArc(int x1, int y1, int w, int h, angle1, angle2);
fillArc(int x1, int y1, int w, int h, angle1, angle2);


Now taking about Fonts and Colors available in applet :


setFont (Font f);
setColor (Color c);
drawImage (Image img, int x, int y, this);


At last Questions for Revision of Applet ?

  1. Define Applet?
  2. Explain the significance of methods present in Applet Class.
  3. Draw an applet of Displaying The hello message on the html page Showing smiling face onto the html page.



tag.



=top>

You can tune size of applet by changing WIDTH and HEIGHT values as required .

Now , it’s time to learn meathods that are present in applet class as derived from extended applet class .

Here are the Applet Methods

init() method :

This method gets called as soon as applet is started. Initialization of all variables, creation of objects, setting of parameters etc can be done in this method.

start() method :

This method is executed after the init() method. When a browser is used to run an applet, any time it is reloaded, the execution begins from the start() method.

stop() method :

This method is used to halt the running of an applet. When a browser is used to run an applet, as soon as web browser is closed, this method is called.

destroy() method :

This method is used to free the memory occupied by the variables and objects initialized in the applet. Any clean up activity that need to be performed can be done in this method.

paint() method :

This method helps in drawing and creating a colored background or an image onto the applet. It always takes a Graphics Class object as an parameter.

repaint() method :

This method calls the update() method, to clear the screen. The update() method in turn calls the paint() method.

Methods in a Graphics Class

These methods are inbuild methods which you can use to draw graphics on applet . You just have to pass coordinates for drawing figures and graphics .

drawBytes( byte[], int start, int end, int x, int y);
drawChars(char[], int start, int end, int x, int y);
drawString (String str, int x, int y);
drawLine(int x1, int y1, int x2, int y2);
drawRect(int x1, int y1, int w, int h);
fillRect(int x1, int y1, int w, int h);
drawRoundRect (int x1, int y1, int w, int h, int w1, int h1);
fillRoundRect (int x1, int y1, int w, int h, int w1, int h1);
draw3DRect(int x1, int y1, int w, int h, true/false);
drawPolygon (int xs, int ys, pts);
fillPolygon (int xs, int ys, pts);

Other methods in a Graphics Class

drawOval(int x1, int y1, int w, int h);
fillOval(int x1, int y1, int w, int h);
drawArc(int x1, int y1, int w, int h, angle1, angle2);
fillArc(int x1, int y1, int w, int h, angle1, angle2);

Now taking about Fonts and Colors available in applet :

setFont (Font f);
setColor (Color c);
drawImage (Image img, int x, int y, this);

At last Questions for Revison of Applet ?

Define Applet? Explain the significance of methods present in Applet Class.

Draw an applet of Displaying The hello message on the html page
Showing smiling face onto the html page.

Other Projects to Try:

  1. Design an applet that displays the string “Hello College Name” moving from left to right. When it reaches to right, it scrolls back to left.
  2. Java Programs
  3. Threads in Java Tutorial for Beginners
  4. Java Tutorial for beginners – Introduction to Java
  5. Communication between applet and servlet Java Code

Filed Under: Java Tutorials

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy