Sunday, December 22, 2013

Applet application

Besides, desktop applications that run on operating systems, Java allows you to create applications that can be run on web browsers that enable Java or applet viewers. These types of applications are called applet applications. The java.applet package has Applet class that provides necessary support for applet applications.

Unlike desktop or stand-alone application, applet does not require the main method. The execution of the applet does not begin at the main method. Rather, Applet class provides different methods serving as the basic mechanism of executing an applet application on a web browser or applet viewer. These methods are init(), start(), paint(), stop(), and destroy(). When an applet begins, the init() methods is called. You can use this method to initialize variables. The init() method is called once. The start() method is called after the init() method. This method is called every time the applet's HTML page displays on the web browser. When the user leaves the HTML page that contains applet, the applet stops by calling the stop() method. When he/she comes back to that page, the start() method will be called again to resume the applet. The destroy() method will be called after the stop method to free up resources used by applet from memory. The paint method is called each time the applet needs to redraw its output. For example, the applet draw its output when it starts and it is minimized and restored.

Create an applet

To create an applet application, you will extend the Applet class in java.applet package. The following example code is a simple applet application to display an image and a button that can be clicked to change the background color of the applet's window. You need an image file called gmaptr.png.

import java.applet.Applet;
import java.awt.Button;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
<applet code="AppletApp" width=600 height=300> 

public class AppletApp extends Applet implements ActionListener{

public void init(){
Button bt=new Button("Change Color");


public void paint(Graphics g){
try {
MediaTracker mt=new MediaTracker(this);
URL imgurl=new URL(getCodeBase().toString()+"gmaptr.png");
Image img=getImage(imgurl);
g.drawImage(img,10, 10,this);

} catch (Exception e) {
// TODO Auto-generated catch block
showStatus("There are one button and one image on the applet window.");

public void actionPerformed(ActionEvent e){
Color c=getBackground();


You need to create AppletApp.class file from the source code above. This can be done by compiling the source code with the javac command from command prompt.

Embed the applet in HTML file

In order to execute the applet on a web browser, you need to create an HTML file that includes the applet. You can include the applet within the HTML document by using the <applet> tag. Below is an example HTML file.

<!DOCTYPE html>
<head> <title>Applet Test</title>
<applet code="AppletApp" width=600 height=300>


After you create the HTML file that contains applet, you can execute the applet by using the appletviewer tool that comes with JDK. The command line below will execute the AppletApp application in the appletviewer from command prompt.

D:\sitedemo\AppletApp\AppletTest\bin>appletviewer AppletApp.html

run applet from command prompt

If you are Eclipse user, you can execute the applet in the appletviewer from Eclipse by clicking the Run menu item.
Besides running the applet from command prompt and Eclipse, you can run the applet in a web browser that supports Java. You need to copy the AppleAppt.class, AppletApp.html, and other related files to a directory of your web server. Then run the HTML file as you do with other HTML, php, or jsp pages. In my machine i have Chrome browser and WampServer installed.

run applet from web browser


  1. Thanks for taking the time to provide us with your valuable statistics. I attempt to offer our applicants with notable care and we take your remarks to coronary heart. As constantly, we appreciate yourself assurance and accept as true within us. custom essay helps college students to enhance traditional essay writing abilities. Other than the college syllabus, writing ability is likewise needed to broaden self-development.

  2. Thanks for taking the time to provide us with your valuable statistics

  3. Awesome interior what is an essay design that is because here is some thing good for the people who loved fashion and related trend

  4. It is nice article to improve my knowledge.thank you for sharing useful post
    web programming tutorial

  5. Hi there, I found your blog via Google while searching for such kinda informative post and your post looks very interesting for me.writers have great importance in the writing company because they are the ones who solve customer's problems with respect to their needs. write my essay

  6. I read your post. It is very informative and helpful to me. I admire the message valuable information you provided in your article. Thank you for posting, again!

  7. A lot of thanks for your whole effort on this web site. Kate really likes making time for investigation and it's really simple to grasp why. My partner and i hear all concerning the lively mode you convey efficient tricks by means of the website and as well recommend response from other individuals on that subject then our own daughter is now becoming educated a great deal. Enjoy the rest of the year. You're performing a brilliant job. digital marketing company

  8. You all need to have such classes because there is much for your to learn and then implement this learning in a better model so just visit page and make your presentations worked.

  9. I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.

  10. I want to show my appreciation to this writer for rescuing me from such a condition. Because of checking throughout the world wide web and finding ideas which are not powerful, I was thinking my life was gone. Being alive without the approaches to the issues you have sorted out through your good guide is a serious case, as well as the kind which could have badly damaged my entire career if I hadn't discovered your web page. Your good mastery and kindness in playing with all the stuff was important. I am not sure what I would have done if I hadn't discovered such a thing like this. I can also at this point look forward to my future. Thanks very much for your high quality and sensible help. I won't hesitate to endorse your web site to any individual who needs to have support on this topic. SEO Singapore

  11. Admiring the hard work you put into your blog and in depth information you present. It's nice to come across a blog every once in a while that isn't the same old rehashed material. Great read! I've bookmarked your site and I'm adding your RSS feeds to my Google account.
    scoping for the website project

  12. Wow! Thank you! I permanently needed to write on my site something like that. Can I include a fragment of your post to my site? benefits of Instagram

  13. Do you mind if I quote a couple of your posts as long as I provide credit and sources back to your blog? My blog is in the very same niche as yours and my visitors would really benefit from some of the information you provide here. Please let me know if this alright with you. Appreciate it!
    eFusion Technology Pte Ltd

  14. of course like your web-site but you need to check the spelling on quite a few of your posts. A number of them are rife with spelling issues and I find it very bothersome to tell the truth nevertheless I’ll certainly come back again.
    search engine

  15. Generally I do not read article on blogs, but I would like to say that this write-up very forced me to try and do it! Your writing style has been surprised me. Thanks, quite nice article.
    digital marketing firm

  16. Greetings! I've been reading your blog for a while now and finally got the bravery to go ahead and give you a shout out from Austin Texas! Just wanted to mention keep up the good job!
    Everybody loves content

  17. Amazing blog your blog is really good and your article has always good thank you for information.

    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร สล็อตออนไลน์ กับทางทีมงานของเราเพียงเท่านั้น
    สมัครสล็อตออนไลน์ >>> Goldenslot
    สนใจร่วมสนุกกับ คาสิโนออนไลน์ คลิ๊กได้เลย
    มีทั้งคาสิโนออนไลน์ หวยออนไลน์ ฟุตบอลออนไลน์ สล็อตออนไลน์ และอื่นๆอีกมากมาย

  18. I'm really loving the theme/design of your weblog. Do you ever run into any internet browser compatibility problems? A handful of my blog readers have complained about my site not operating correctly in Explorer but looks great in Opera. Do you have any solutions to help fix this problem?
    designing CTA buttons

  19. Hello, i read your blog occasionally and i own a similar one and i was just curious if you get a lot of spam feedback? If so how do you prevent it, any plugin or anything you can advise? I get so much lately it's driving me mad so any assistance is very much appreciated.
    costs for building a website

  20. I'm not sure why but this site is loading very slow for me. Is anyone else having this problem or is it a problem on my end? I'll check back later on and see if the problem still exists.
    Singapore hotel promotion

  21. Hey! I know this is kinda off topic however I'd figured I'd ask. Would you be interested in exchanging links or maybe guest authoring a blog post or vice-versa? My site covers a lot of the same subjects as yours and I feel we could greatly benefit from each other. If you are interested feel free to shoot me an e-mail. I look forward to hearing from you! Excellent blog by the way! army uniform

  22. Do you know how to register a company in Saudi Arabia???

  23. Hello! This is my first visit to your blog! We are a group of volunteers and starting a new project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a outstanding job! small condo interior design singapore

  24. I'm not sure if this is a formatting issue or something to do with internet browser compatibility but I thought I'd post to let you know.creative event agency Singapore

  25. I am very enjoyed because of this blog. Its an insightful topic. It help me very much to solve some problems. Its opportunity are very fantastic and working style so speedy. funeral home

  26. Very nice information thank you so much bro i like your ideas good work. putlocker

  27. Great beat ! I wish to apprentice while you amend your web site, how can i subscribe for a blog web site? The account aided me a acceptable deal. I had been a little bit acquainted of this your broadcast offered bright clear idea