How To Create Your Own AppletViewer In Java

Introduction

In this article we discuss appletviewer in Java and how to create your own appletviewer.

AppletViewer

The Java appletviewer command allows you to run applets outside or without a web browser. It is compatible with the tool supplied by Sun Microsystems, the appletviewer. Appletviewer is generally used by developers for testing their applets before deploying them to a website.

For running the appletviewer tool, we need the use of the Native Abstract Window Toolkit (NAWT). 

In Java, it is the best option for running Java applets without the use of a web browser. Appletviewer is generally used since they fill the place of web browser, it functions very differently from a web browser. It operates on HTML documents. Each time the applet viewer encounters an applet tag in an HTML document, it launches a separate applet viewer window containing the respective applet.

fig-3.jpg

This figure illustrates the appletviewer in Java. In this figure an applet class containing the string "welcome-to-applet-in-java" will be shown by an appletviewer.

Advantages

  1. Applet-viewer is used to burn study CDs/DVDs.
  2. Full-fledged diagnostic viewer, FDA cleared and CE certified, that may be used on computers having Java installed in it, without installing the full OnePacs study download and workstation software.

How to create your own AppletViewer

As you know, the appletviewer tool creates a frame and displays the output of the applet in the frame. You can also create your frame and display the applet output.

Example

GraphicsEx.java

First we create a simple applet class, "GraphicsEx.java". This class contains the "drawString" method to display the string message "welcome to c-sharpcorner.com".

import java.awt.Graphics;

import java.applet.Applet;

public class GraphicsEx extends Applet

{

public void paint(Graphics grp)

{

grp.drawString("welcome to c-sharpcorner.com", 50, 50);

}

}

MyAppletViewer.java

Now, we write the code for the program that works like the appletviewer tool.

import java.awt.Frame;

import java.awt.Graphics;

import java.applet.Applet;

public class MyAppletViewer extends Frame

  {

    public static void main(String[] args) throws Exception

      {

        Class cls=Class.forName(args[0]);

        MyAppletViewer apltv=new MyAppletViewer();

        apltv.setSize(350,350);

        apltv.setLayout(null);

        apltv.setVisible(true);

        Applet aplt=(Applet)cls.newInstance();

        aplt.start();

        Graphics grp=apltv.getGraphics();

        aplt.paint(grp);

        aplt.stop();

      }

  }

/*

<applet code="MyAppletViewer.class" width="400" height="400">

</applet

*/

Output

First we compile our programs as in the following.

Fig-1.jpg

Now show our program that works like an appletviewer tool, in other words we have created our own appletviewer tool.

fig-2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all