trouble talking to my applet

I’ve got an applet ( see below) embedded in in my html page using the gearsapplet as a template. I have an ID field in the applet tag ID=“Anatomy”.

The applet runs fine. So far so good… Now I’m trying to get communication between the applet and the browser host going and have run into a brick wall.

I’m trying to call setHide() from javascript like this document.Anatomy.setHide(parseInt(display));

But I get a javascript error ‘Object doesn’t support this property or method’

However I can call the stop() method quite happily.

Could someone point out my fault.

Thanks. Chris

import java.applet.;
import java.awt.
;
import java.io.*;

import javax.media.opengl.;
import com.sun.opengl.util.
;
import realTime.anatomy.anatomy;

/** Shows how to deploy an applet using JOGL. This demo must be
referenced from a web page via an <applet> tag. */

public class AnatomyApplet extends Applet {
private Animator animator;
anatomy flesh = new anatomy();
public void init() {
setLayout(new BorderLayout());
GLCanvas canvas = new GLCanvas();

canvas.addGLEventListener(flesh);
canvas.setSize(getSize());
add(canvas, BorderLayout.CENTER);
animator = new FPSAnimator(canvas, 60);

}

public void start() {
animator.start();
}

public void stop() {
System.err.println(“stop called”);
// FIXME: do I need to do anything else here?
animator.stop();
}

public void setHide(int n){
System.err.println(“hide called”);
flesh.hide( n );
}
public void setShow(int n ){
System.err.println(“show called”);
flesh.show( n );
}

}

strange, does your error dont come from “parseInt(display)” ?

try

alert(document.Anatomy.setHide))

maybe also try

document.applets[0].setHide(0);

Tried it every way I could think of. All these calls give the same result.

	document.Anatomy.silly(parseInt(display));
	//document.Anatomy.setHide(parseInt(display));
	//document.applets[0].setHide(0);
	//document.Anatomy.stop();
	//document.Anatomy.setHide(0);
	//document.Anatomy.setHide();
	//alert(document.Anatomy.setHide(0));

It appears that the method isn’t there? but
document.Anatomy.stop();

produces the expected message. I am stumped.

Chris

Still struggling with this.
My attemp to call my anatomy class through the JNLPAppletLauncher clearly not theright approach.
Further digging has turned up the getSubApplet() helper function but I am still stuck.

from javascript:

	var applet = document.Anatomy.getSubApplet();
	alert("through " + applet);

the alert givesthe string: anatomy.applet.AnatomyApplet[panel0,0,0,817x787,layout=java.awt.BorderLayout]
applet.hide(0);
gives a java.lanException hide{0} no such method exists

I’ve tried all the variations on calling the hide() method I can think of , no joy.
I’ve had a look through the JNLPAppletLauncher code but it’s way over my head. I don’t understand the returned string. Is that an object?

I found one example of getSubApplet use here http://eventhorizongames.com/wiki/doku.php?id=articles:processing:scripting:start

but it calls a PApplet method and whatever it’s doing doesn’t work with Applet.

Could really use some help here.

Thanks Chris

arf… it would have be easier if you told that you were using JNLPAppletLauncher … I was beleaving that was a standard Applet, so your problem is a lot more understandable now…

as getSuApplet return an Applet and not an “Anatomy” object it dont know your method that is “Anatomy specific”

EDIT:
maybe try that
((yourpackage.Anatomy)document.Anatomy.getSubApplet()).hide(0);

Hi DzzD… I didn’t understand your suggestion. I tried it ,but it wouldn’t parse. Could you explain the logic please.

I have installed the next generation plugin and enabled it but apart from a longer error message (which says the same thing) I am no further forward.

My application depends entirely on being able to call in and out from java to javascript. Certainly the documentation made it look easy.

Am I right in thinking that the correct way to create an applet that uses jogl is the method shown in the gears example. And that that must be loaded with the JNLPAppletLauncher?

The getSubApplet method (if I understand it) returns the AnatomyApplet class and that prevents me getting at anything else.

How then can I access the methods of my class? Trawling the web has turned up very little. Can someone direct me to the documentation or an example.

Beginning to get desperate here…

Couple of suggestions;

  1. Make sure that the applet tag has the ‘mayscript’ parameter.

  2. Have 2 applets on the page, your JNLP one and a ‘normal’ 1x1 pixel communicator applet (no launcher!). Javascript can certainly talk to normal applets and applets can talk to eachother so this might work?

[quote]Hi DzzD… I didn’t understand your suggestion. I tried it ,but it wouldn’t parse. Could you explain the logic please.
[/quote]
I suppose that getSubApplet() is defined as :
public Applet getSubApplet();

so when you call it, it return an Applet object, and an Applet object doesn’t have the hide(int n) method, this is why your code dont works. I was thinking that maybe it was possible to cast it.

EDIT:
if javascript cannot get acces to your applet , maybe your applet can acces javascript you can try to add the following in your applet,
from your applet use JSObject to call a javascript function that will give you your applet:

import netscape.javascript.JSObject;
...
...

public void init()
{
try
{
 JSObject htmlWin=JSObject.getWindow(this);
 Object p[]=new Object[1];
 p[0]=this;		
 htmlWin.call("init",p);
}
catch(Exception e)
{
 e.printStackTrace(System.out);
}
}

than in your html add a js function:


var applet;
function init(appRef)
{
 applet=appRef;
}

then later you can use

applet.hide(0);

It took some doing but I got it to work eventually!

My second idea was the right one. I modified the GearsApplet to include a ‘reverse’ function and then built a second applet with this code;

import java.applet.*;
import java.awt.*;
import java.util.*;
import org.jdesktop.applet.util.*;


public class GearsApplet1 extends Applet
{
    public void init()
    {
    }
    public void start()
    {
		JNLPAppletLauncher launcher;
		for (    Enumeration e = getAppletContext().getApplets();
		         e.hasMoreElements();
		         ) {
		    try {
		        Applet t = (Applet) e.nextElement();
		        if ("Gears".equals(t.getParameter("name"))) {
		            launcher = (JNLPAppletLauncher)t;
		            GearsApplet applet=(GearsApplet)launcher.getSubApplet();
		            applet.reverse();
		            break;
		        }
		    }
		    catch (ClassCastException ex) {
		    }
}

    }

    public void stop()
    {
    }
}

You can get the Javascript from the source of the demo page.

Guys, thank you so much for your help. It is very generous of you and I really appreciate it.

In the interim I realised that my difficulties could be solved if the anatomy code was part of the subApplet. After a bit of fiddling I have re-arranged it so that the anatomy class is an inner class of the AnatomyApplet class (if I understand what I’m doing :-\ ). Anyway…now I can call in via the outer class i.e. flesh.hide( int ); (hooray)

Hopefully calling out will not be such a problem.

I guess my problems are down to being very inexperienced with java!

DzzD, I tried your idea. But it came to the same dead end (for me) but it sure got me thinking!
SimonH, that is very cool, and is clearly a bail out if all else fails.

Good to know you’re both out there!

Thanks Chris