How to implement AppletContext interface of Applet?

Below java code show how to implement AppletContext interface –

[java]
/****************************************************************************************
* Created on 08-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
****************************************************************************************/
package com.kodehelp.java.applet;

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.MalformedURLException;
import java.net.URL;

/**
* Created by https://kodehelp.com
* Date: 8/26/11
*/
public class AppletMainClass extends Applet implements ActionListener{
static AppletMainClass myApplet;
static MyAppletStub myStub;
Image image;

public void init() {
System.out.println("Code base = " + getCodeBase());
System.out.println("Document base = " + getDocumentBase());

System.out.println("\ninit () called");
System.out.println("isActive () returns " + isActive());

Button b = new Button("Visit kodehelp.co.in");
b.addActionListener(this);
add(b);

b = new Button("Audio");
b.addActionListener(this);
add(b);

String imageName = getParameter("image");

if (imageName != null)
image = getImage(getCodeBase(), imageName);
}

public void start() {
System.out.println("start () called");
System.out.println("isActive () returns " + isActive());
}

public void paint(Graphics g) {
if (image != null)
g.drawImage(image, 0, 0, this);
}
/**
* Invoked when an action occurs.
*/
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Audio")) {
String soundName = getParameter("audio");

if (soundName != null) {
AudioClip ac = getAudioClip(getDocumentBase(), soundName);

ac.play();
}

return;
}

try {
URL u = new URL("https://kodehelp.com");
getAppletContext().showDocument(u);
} catch (MalformedURLException exc) {
System.out.println(e);
}
}

public void stop() {
System.out.println("stop () called");
System.out.println("isActive () returns " + isActive());
}

public void destroy() {
System.out.println("destroy () called");
System.out.println("isActive () returns " + isActive());
}

public static void main(String[] args) {
Frame frame = new Frame("AppletAndApp as an Application");
myApplet = new AppletMainClass();
frame.add(new Panel().add(myApplet));
frame.addNotify();
myApplet.setStub(myStub = new MyAppletStub(args));
myApplet.init();
frame.setSize(300, 200);
frame.setVisible(true);
myStub.setActive(true);
myApplet.start();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent w) {
myStub.setActive(false);
myApplet.stop();
myApplet.destroy();
System.exit(0);
}
});
}
}

/****************************************************************************************
* Created on 08-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
****************************************************************************************/
package com.kodehelp.java.applet;

import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AudioClip;
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;

/**
* Created by https://kodehelp.com
* Date: 8/26/11
*/
public class MyAppletContext implements AppletContext{
/**
* Creates an audio clip.
*
* @param url an absolute URL giving the location of the audio clip.
* @return the audio clip at the specified URL.
*/
public AudioClip getAudioClip(URL url) {
return Applet.newAudioClip(url);
}

/**
* Returns an <code>Image</code> object that can then be painted on
* the screen. The <code>url</code> argument<code> </code>that is
* passed as an argument must specify an absolute URL.
* <p/>
* This method always returns immediately, whether or not the image
* exists. When the applet attempts to draw the image on the screen,
* the data will be loaded. The graphics primitives that draw the
* image will incrementally paint on the screen.
*
* @param url an absolute URL giving the location of the image.
* @return the image at the specified URL.
* @see java.awt.Image
*/
public Image getImage(URL url) {
Toolkit tk = Toolkit.getDefaultToolkit();
return tk.getImage(url);
}

/**
* Finds and returns the applet in the document represented by this
* applet context with the given name. The name can be set in the
* HTML tag by setting the <code>name</code> attribute.
*
* @param name an applet name.
* @return the applet with the given name, or <code>null</code> if
* not found.
*/
public Applet getApplet(String name) {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

/**
* Finds all the applets in the document represented by this applet
* context.
*
* @return an enumeration of all applets in the document represented by
* this applet context.
*/
public Enumeration<Applet> getApplets() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}

/**
* Requests that the browser or applet viewer show the Web page
* indicated by the <code>url</code> argument. The browser or
* applet viewer determines which window or frame to display the
* Web page. This method may be ignored by applet contexts that
* are not browsers.
*
* @param url an absolute URL giving the location of the document.
*/
public void showDocument(URL url) {
System.out.println("Showing document " + url);
}

/**
* Requests that the browser or applet viewer show the Web page
* indicated by the <code>url</code> argument. The
* <code>target</code> argument indicates in which HTML frame the
* document is to be displayed.
* The target argument is interpreted as follows:
* <p/>
* <center><table border="3" summary="Target arguments and their descriptions">
* <tr><th>Target Argument</th><th>Description</th></tr>
* <tr><td><code>"_self"</code> <td>Show in the window and frame that
* contain the applet.</tr>
* <tr><td><code>"_parent"</code><td>Show in the applet’s parent frame. If
* the applet’s frame has no parent frame,
* acts the same as "_self".</tr>
* <tr><td><code>"_top"</code> <td>Show in the top-level frame of the applet’s
* window. If the applet’s frame is the
* top-level frame, acts the same as "_self".</tr>
* <tr><td><code>"_blank"</code> <td>Show in a new, unnamed
* top-level window.</tr>
* <tr><td><i>name</i><td>Show in the frame or window named <i>name</i>. If
* a target named <i>name</i> does not already exist, a
* new top-level window with the specified name is created,
* and the document is shown there.</tr>
* </table> </center>
* <p/>
* An applet viewer or browser is free to ignore <code>showDocument</code>.
*
* @param url an absolute URL giving the location of the document.
* @param target a <code>String</code> indicating where to display
* the page.
*/
public void showDocument(URL url, String target) {
try {
showDocument(new URL(url.toString() + target));
} catch (MalformedURLException e) {
}
}

/**
* Requests that the argument string be displayed in the
* "status window". Many browsers and applet viewers
* provide such a window, where the application can inform users of
* its current state.
*
* @param status a string to display in the status window.
*/
public void showStatus(String status) {
System.out.println(status);
}

/**
* Associates the specified stream with the specified key in this
* applet context. If the applet context previously contained a mapping
* for this key, the old value is replaced.
* <p/>
* For security reasons, mapping of streams and keys exists for each
* codebase. In other words, applet from one codebase cannot access
* the streams created by an applet from a different codebase
* <p/>
*
* @param key key with which the specified value is to be associated.
* @param stream stream to be associated with the specified key. If this
* parameter is <code>null</code>, the specified key is removed
* in this applet context.
* @throws <code>IOException</code> if the stream size exceeds a certain
* size limit. Size limit is decided by the implementor of this
* interface.
* @since 1.4
*/
public void setStream(String key, InputStream stream) throws IOException {

}

/**
* Returns the stream to which specified key is associated within this
* applet context. Returns <tt>null</tt> if the applet context contains
* no stream for this key.
* <p/>
* For security reasons, mapping of streams and keys exists for each
* codebase. In other words, applet from one codebase cannot access
* the streams created by an applet from a different codebase
* <p/>
*
* @param key key whose associated stream is to be returned.
* @return the stream to which this applet context maps the key
* @since 1.4
*/
public InputStream getStream(String key) {
return null;
}

/**
* Finds all the keys of the streams in this applet context.
* <p/>
* For security reasons, mapping of streams and keys exists for each
* codebase. In other words, applet from one codebase cannot access
* the streams created by an applet from a different codebase
* <p/>
*
* @return an Iterator of all the names of the streams in this applet
* context.
* @since 1.4
*/
public Iterator<String> getStreamKeys() {
return null;
}
}

/****************************************************************************************
* Created on 08-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
****************************************************************************************/
package com.kodehelp.java.applet;

import java.applet.AppletContext;
import java.applet.AppletStub;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;

/**
* Created by https://kodehelp.com
* Date: 8/26/11
*/
public class MyAppletStub implements AppletStub{
private boolean active = false;
private Hashtable ht = new Hashtable();
private MyAppletContext context;

public void setActive(boolean active) {
this.active = active;
}

MyAppletStub(String[] args) {
context = new MyAppletContext();
if ((args.length & 1) != 0)
return;

for (int i = 0; i < args.length; i += 2)
ht.put(args[i], args[i + 1]);
}
/**
* Determines if the applet is active. An applet is active just
* before its <code>start</code> method is called. It becomes
* inactive just before its <code>stop</code> method is called.
*
* @return <code>true</code> if the applet is active;
* <code>false</code> otherwise.
*/
public boolean isActive() {
return false; //To change body of implemented methods use File | Settings | File Templates.
}

/**
* Gets the URL of the document in which the applet is embedded.
* For example, suppose an applet is contained
* within the document:
* <blockquote><pre>
* http://java.sun.com/products/jdk/1.2/index.html
* </pre></blockquote>
* The document base is:
* <blockquote><pre>
* http://java.sun.com/products/jdk/1.2/index.html
* </pre></blockquote>
*
* @return the {@link java.net.URL} of the document that contains the
* applet.
* @see java.applet.AppletStub#getCodeBase()
*/
public URL getDocumentBase() {
URL u = null;
try {
u = new URL("file:/C:./sample.html");
} catch (MalformedURLException e) {
}
return u;
}

/**
* Gets the base URL. This is the URL of the directory which contains the applet.
*
* @return the base {@link java.net.URL} of
* the directory which contains the applet.
* @see java.applet.AppletStub#getDocumentBase()
*/
public URL getCodeBase() {
URL u = null;
try {
u = new URL("file:/C:./");
} catch (MalformedURLException e) {
}

return u;
}

/**
* Returns the value of the named parameter in the HTML tag. For
* example, if an applet is specified as
* <blockquote><pre>
* &lt;applet code="Clock" width=50 height=50&gt;
* &lt;param name=Color value="blue"&gt;
* &lt;/applet&gt;
* </pre></blockquote>
* <p/>
* then a call to <code>getParameter("Color")</code> returns the
* value <code>"blue"</code>.
*
* @param name a parameter name.
* @return the value of the named parameter,
* or <tt>null</tt> if not set.
*/
public String getParameter(String name) {
return (String) ht.get(name);
}

/**
* Returns the applet’s context.
*
* @return the applet’s context.
*/
public AppletContext getAppletContext() {
return context;
}

/**
* Called when the applet wants to be resized.
*
* @param width the new requested width for the applet.
* @param height the new requested height for the applet.
*/
public void appletResize(int width, int height) {
this.active = active;
}
}

[/java]

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *