We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Communication between JS/Flash and Processing
Page Index Toggle Pages: 1
Communication between JS/Flash and Processing (Read 6157 times)
Communication between JS/Flash and Processing
Aug 12th, 2005, 7:27pm
 
I dunno how to use the javascript library after reading the doc. Can someone pls post an example here, showing the communication between processing and javascript?

Basically I wanna know how to access JS var/function/etc from Processing, and vice versa.....

Besides, I want to do a Flash and Processing communication. I can think of using Javasciprt. Is there other ways to do it?
Re: Communication between JS/Flash and Processing
Reply #1 - Aug 13th, 2005, 12:12pm
 
You could use PHP to write param (http://processing.org/reference/param_.html) which processing then can read. And to communicate with the flashfile, i think you can use php to pass variables to a flash-file..
hope this gets you somewhere!

-seltar
Re: Communication between JS/Flash and Processing
Reply #2 - Aug 14th, 2005, 11:09am
 
You can access function in an Applet like this.

Appletcode:
Code:

import java.awt.Graphics;
import java.applet.Applet;

public class HelloWorld extends Applet
{
String myString ="Das ist mein Hallo-Text";

public void paint(Graphics g)
{
g.drawString(myString, 25, 20);
}
public void setString(String aString)
{
myString = aString;
repaint();
}
}

HTMLCode:
Code:

<html><head><title>Test</title>
</head><body>
<applet code="HelloWorld" name="Hallo" width=150 height=25>
</applet><br>
<form name="Eingabe" action="">
<input type="text" name="Text">
<input type="button" value="Test"
onclick="document.Hallo.setString(document.Eingabe.Text.value)">
</form>
</body></html>

From Flash you can directly call javascript functions with the getUrl function:

getUrl("javascript:yourfunction()");

I think it should simular work from the applet, so maybe the processing link function works:

link("javascript:yourfunction()");

Unfortunatly you can not call function in flash with javascript you can only change values.

Take a look here:

http://www.macromedia.com/support/flash/publishexport/scriptingwithflash/
Re: Communication between JS/Flash and Processing
Reply #3 - Aug 14th, 2005, 12:11pm
 
the new version of Flash allows direct calling of javascirpt functions and variables so i think this is very convenient....


thanks for ur applet example....but if i want to do so in Processing (since i'm not typing java codes but Processing now) what is the code? i.e. the code in Processing to pass values and call functions of Javascirpt?
Re: Communication between JS/Flash and Processing
Reply #4 - Aug 15th, 2005, 6:23pm
 
can anyone paste the code of Processing that can call and run javascript fcts and var?
Re: Communication between JS/Flash and Processing
Reply #5 - Apr 28th, 2006, 3:59pm
 
just to make this complete ( should go into processinghacks.com ? )

applet - to - javascript

see:
http://www.rgagnon.com/javadetails/java-0172.html

Code:

... see cellos and my posts further down


javascript  - to - applet

see:
http://bezier.de/jsloading/

Code:

<script type="text/javascript" language="javascript">
<!--
var checkApplet = function () {
   if ( document.applets && document.applets.length > 0 )
   {
       var papplet = document.applets[0];  // processing applet

       if ( papplet.frameCount > 0 )  // it's running
       {
           alert( papplet.frameCount );  // do smtg usefull instead ...
       }
       else
       {
           setTimeout( 'checkApplet()', 1000 );  // try again in 1 sec.
       }
   }
}

window.onload = function () {
   setTimeout( 'checkApplet()', 1000 );
}
//-->
</script>


F
Re: Communication between JS/Flash and Processing
Reply #6 - Apr 28th, 2006, 5:57pm
 
That reflection stuff is pretty nasty.  I would suggest just assuming that the user has netscape.javascript.JSObject, and using the class directly.

However, if you absolutely want to use refleciton, then you should really use Classs.getMethod instead of getMethods.  That whole compareTo()==0 is also really gross (use .equals()!).

So something like:
Code:

try {
Class c = Class.forName("netscape.javascript.JSObject");
Method getWindow = c.getMethod("getWindow",new Class[]{Applet.class}); // on java 1.5 you can do c.getMethod("getWindow",Applet.class);
Method eval = c.getMethod("eval",new Class[]{String.class});


Object jswin = getw.invoke(c, new Object[]{this});

jsresult = eval.invoke(jswin, new Object[]{ jscmd }).toString();


Lot shorter, huh? Wink
Re: Communication between JS/Flash and Processing
Reply #7 - Apr 28th, 2006, 6:17pm
 
hi cello.
super. thanks for looking into it ...
"my" code was just a copy paste from the mentioned url. it seemed to work ok. i guess it's a little dusty.

F
Re: Communication between JS/Flash and Processing
Reply #8 - Apr 28th, 2006, 6:22pm
 
cello wrote on Apr 28th, 2006, 5:57pm:
... I would suggest just assuming that the user has netscape.javascript.JSObject, and using the class directly.  ...


true. it's part of the processing-dist by default. totally forgot that.

Code:

import netscape.javascript.*;

void setup () {
   size( 200, 200 );
}

void draw () {
   //
   // from 30 down to one line ... that's processing.
   //
   JSObject.getWindow(this).eval( "alert(\"Processing says hello.\")" );
   noLoop();
}


F
Re: Communication between JS/Flash and Processing
Reply #9 - Apr 29th, 2006, 6:48pm
 
I do believe getWindow returns a JSObject, so you do not need to cast it.

Marcello
Re: Communication between JS/Flash and Processing
Reply #10 - Apr 30th, 2006, 11:19am
 
arrgg ... sorry. you're right.

changed code above.

best,
F
Re: Communication between JS/Flash and Processing
Reply #11 - Apr 13th, 2008, 11:58pm
 
Just a quick note on using call() vs eval() for JSObject:

I was using JSObject.getWindow(this).call("JSfunction()") and filling in parameters for each call.... I found out this didn't execute my Javascript correctly.

However, when using JSObject.getWindow(this).eval("JSfunction()") it worked like a charm.  Just make sure to set the quotes correctly if you are passing in Java variables into your Javascript parameters, e.g.:


eval("javaScriptFunction('"+javaparameters+"');");

See: http://www.rgagnon.com/javadetails/java-0177.html
Re: Communication between JS/Flash and Processing
Reply #12 - Feb 12th, 2009, 11:55pm
 
I get a repeatable crash of Firefox when running the last example. (MaxOSX, Feb 3/09 release);
Re: Communication between JS/Flash and Processing
Reply #13 - Mar 3rd, 2009, 4:42pm
 
To be specific: Firefox freezes up completely when the alert diaolg presents itself!
Re: Communication between JS/Flash and Processing
Reply #14 - Sep 9th, 2009, 4:40am
 
"document.applets" doesn't work in Firefox, so you might be better of using "getElementById" instead. Just add a id to your object.
Page Index Toggle Pages: 1