Create 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".
  1. import java.awt.Graphics;  
  2. import java.applet.Applet;  
  3. public class GraphicsEx extends Applet  
  4. {  
  5.     public void paint(Graphics grp)  
  6.     {  
  7.         grp.drawString("welcome to c-sharpcorner.com"5050);  
  8.     }  
  9. }  
MyAppletViewer.java
 
Now, we write the code for the program that works like the appletviewer tool.
  1. import java.awt.Frame;  
  2. import java.awt.Graphics;  
  3. import java.applet.Applet;  
  4. public class MyAppletViewer extends Frame {  
  5.     public static void main(String[] args) throws Exception  
  6.     {  
  7.         Class cls = Class.forName(args[0]);  
  8.         MyAppletViewer apltv = new MyAppletViewer();  
  9.         apltv.setSize(350350);  
  10.         apltv.setLayout(null);  
  11.         apltv.setVisible(true);  
  12.         Applet aplt = (Applet) cls.newInstance();  
  13.         aplt.start();  
  14.         Graphics grp = apltv.getGraphics();  
  15.         aplt.paint(grp);  
  16.         aplt.stop();  
  17.     }  
  18. }  
  19. /* 
  20. <applet code="MyAppletViewer.class" width="400" height="400"> 
  21. </applet> 
  22. */  
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