Implementing Applet Assignment Help

Assignment Help: >> Applet Life Cycle - Implementing Applet

Implementing Applet:

Java applet source code is written in the similar way as java application source code along with a text editor. A difference is in which java applets do not have a main method. Alternatively, they have various other methods which are called through the VM when requested through the browser. Here is the source code for the simple FilledBox applet:

import java.awt.*;

import java.applet.Applet;

/* filled box displays a filled, colored box in the browser window */

public class FilledBox extends Applet {

color b;

public void init()

{

String s;

s= getParameter("color");

b=Color.gray;

if (s != null)

{

if(s.equals("red")) b=Color.red;

if(s.equals("white")) b=Color.white;

if(s.equals("blue"))b=Color.blue;

}

}

public void paint(Graphics g) {

g.setColor(b);

g.fillRect(0,0,size().width, size().height);

}

}

It's a little more complicated than the java application example, but that is because it does more. You will recall that a main method is required by all java applications; it is conspicuously absent in this applet. In fact, java applets do not have any required methods at all.  However,  there  are  five  methods  that  the  VM  may  call  when requested by the web browser

public void init() initializes the applet. Called only once.

public void start() Called when the browser is ready to start executing the initialized applet. Can be called multiple times if user keeps leaving and returning to the web page. also called when browser deiconified.

public void stop() Called when the browser wishes to stop executing the applet. Called whenever the user leaves the web page. also called when browser iconified.

public void destroy() Called when the browser clears the applet out of memory.

public void paint(Graphics g) Called whenever the browser needs to redraw the applet

The applet will have no functionality for the specific method not implemented if the applet does not implement any of these methods. In the instance, init and paint are implemented. The init function obtains the desired box color from a parameter in the host document. A paint method draws the filled box in the browser window.

Save this java applet source as FilledBox. Java

using Javac

The java compiler works the same on applets as it does on java applications

javac FilledBox.java

Here are a few tips that may help you get start.  First, applet classes must always be declared public or they will not get compiled. Also, note that java is case sensitive FilledBox java is not the similar as filledBox.Java and will not be compiled.

If the java code is acceptable to the compiler, they only message you will see is about a deprecated API:

note: FilledBox. java uses a deprecated API. recompile with

"-deprecation" for details.

1 warning.

for now, ignore the warning. As long as there were no error messages in the file FilledBox. class will be created. If there were error messages, you need to go back and fix your code. There are several different kinds of error messages which the compiler might produce when given a source file. The simplest to fix are syntax errors, like as a missing semicolon or closing brace. Other messages will highlight incorrect use of variable types, invalid expressions, or violation access restrictions. Getting your source code to compile is only the first part of the debugging process; error free compilation does not guarantee that your program will do what you need. But doesn't worry about debugging just yet this instance is easy sufficient that is should run without any problems.

Before you can run your applet, your must create an HTML document to host it.

Creating and HTML File Using appletviewer
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd