How to write an applet Assignment Help

Assignment Help: >> Applets >> How to write an applet

How to write an applet:

In order to write an applet, you must first define a class that extends the Applet class from the java.applet package or its subclass JApplet from the javax.swing package.

You will be pleased to know that much of what we have covered already about writing applications also applies to writing applets. Applets can use almost all the Java language features and most of the API components we have previously seen. There are a few key differences, however, between applications and applets because applets must run in conjunction with a web page. These are as follows.

  • An applet does not have a main method. It has an init method, which is invoked by the browser when the web page is loaded.
  • Applets have a graphical user interface only, and use normal Swing or AWT features for this. When running in a browser, they cannot read or write using the standard streams such as System.out .
  • For the applet GUI, you do not need to construct a Frame or JFrame object, as the browser window is used instead.
  • An application with a GUI may use the setSize, setTitle or setVisible methods of the Frame class. For applets, sizing is done in the HTML file; they cannot have title bars and they are made visible automatically.

This means that it is quite straightforward to convert a simple graphical application into an applet. Here is a simple applet that displays a label containing a familiar message:

import java.awt. *;
import javax.swing. *;

public class HelloWalrusApplet extends JApplet
{
public void init( )
{
Container pane = getContentPane( );
JLabel label = new JLabel("Hello Walrus");
pane.add(label);
}
}

The applet is a subclass of JApplet, which is a standard class from the Swing library. It defines one method, init, which is automatically run when the applet is first loaded, such as when you first view the associated web page.

Before we can run this simple applet, we need an HTML page. This can contain any normal HTML tags and other content, but must also contain a special tag that links the web page to our applet. Traditionally this is the role of the <APPLET> tag, logically enough, you might think. Here is some suitable HTML:

Note that except for the <HTML>
<HR> tag, each HTML tag <HEAD>
has its corresponding
closing tag starting with a <TITLE>Testing the Applet HelloWalrusApplet</TITLE>
slash '/'. </HEAD>
<BODY>
<H1>A message for walruses everywhere</H1>
<HR>
<APPLET CODE="HelloWalrusApplet.class" WIDTH=400 HEIGHT=300>
</APPLET>
<HR>
</BODY>
</HTML>

This contains a few items of standard content - a title for the web page, a piece of text that is displayed in large font (because of the <H1> tag), and two <HR> tags that cause the applet to be enclosed by two horizontal lines (horizontal 'rules'). The <APPLET> tag has three parameters, specifying the location of the compiled code for the applet together with the width and height in pixels of the applet window on the screen.

Finally, to run the applet, we can use a Java-enabled browser (which we shall discuss further later) or perhaps, at this stage, the appletviewer program. We supply the name of the HTML file (not the applet file) to the appletviewer program and it displays just the applet window, ignoring the rest of the HTML. The result should be similar to that in Figure.

1734_applet1.png

Figure: Running a simple applet using the appletviewer program

Using a browser to view the applet gives quite a different result, as shown in Figure.

1688_applet2.png

Figure: Viewing the web page and the simple applet using a browser

Now we see the effect of the HTML tags and other contents of the web page. The applet is shown in a window of size 400 by 300 pixels, as specified in the <APPLET> tag.

This reinforces the point made earlier - the appletviewer is very useful for checking the applet by itself during development. For final testing of how the applet will look when its web page is displayed, you must use a browser. For an applet to be made widely available, you will need to test it using different browsers and browser versions, as there are quite a few differences in how browsers support various features of applets. We shall discuss this in a later subsection.

HTML tag options for applets

In more recent versions of HTML (since HTML 4.0), the <APPLET> tag has been deprecated and we are expected to use the <OBJECT> tag. Unfortunately, older versions of browsers are not aware of the change, so we have to decide whether to comply with HTML 4.0 (and perhaps have to accept that our applets do not work on some browsers) or to continue to use <APPLET> tags, which are recognized on all browsers apart from very ancient ones. The choice depends on how widespread you expect the use of the applet to be. If it is only to be available within a limited environment, such as an Intranet, then you may be able to ensure that all the browsers can cope with <OBJECT> tags. In this unit, we will continue to use the <APPLET> tag.

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your How to write an applet homework and assignments? Live How to write an applet experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer How to write an applet homework help, java assignment help and How to write an applet projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

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