Mac70
|
 |
«
Posted
2012-12-30 13:28:31 » |
|
Like in topic - is it possible to connect Java with JavaScript in HTML? I know that connecting with JS in .js files is possible and I did it, but I need to connect with JS in HTML. I need this because I am trying to write Kongregate API for Java. 
|
|
|
|
deepthought
|
 |
«
Reply #1 - Posted
2012-12-30 15:02:42 » |
|
will kongregate accept java applets?
|
Mad Scientist
|
|
|
Mac70
|
 |
«
Reply #2 - Posted
2012-12-30 15:08:34 » |
|
Yes, Kongregate offers full support for them. I already know one Java developer on Kongregate, and I want to expand this community. I made a research on the applets on Kongregate - everything is really easy to attach and works well. 
|
|
|
|
Games published by our own members! Check 'em out!
|
|
Mac70
|
 |
«
Reply #3 - Posted
2012-12-31 17:27:25 » |
|
Bump. Does anyone have idea how to do this connection?
|
|
|
|
deepthought
|
 |
«
Reply #4 - Posted
2013-01-01 06:22:05 » |
|
|
Mad Scientist
|
|
|
Mac70
|
 |
«
Reply #5 - Posted
2013-01-03 16:58:41 » |
|
Sadly this does not work anymore. Any other ideas?
|
|
|
|
philfrei
|
 |
«
Reply #6 - Posted
2013-01-03 19:28:33 » |
|
You can look at the code-behind-forms of any of my applets. For example: http://www.adonax.com/Jean/BotBlocker/There are various controls that work with three different applets, and they all appear to still be functional. The key routine is as follows: 1 2 3 4 5 6 7 8 9 10 11 12 13
| <script src="http://www.java.com/js/deployJava.js"></script> <script> var attributes = {id:'nobot', code:'com.adonax.BotBlocker.class', archive:'challenge.jar', width:220, height:220} ; var parameters = {fontSize:10, boxbgcolor: 'cyan', boxborder: 'true'} ; var version = '1.6' ; deployJava.runApplet(attributes, parameters, version); </script> |
By giving the app an "id", you can invoke methods in it, as in the following: 1 2 3 4
| <script language="javascript"> function startChallenge(){nobot.startNobot();} function stopChallenge(){nobot.stop();} </script> |
I'm checking a more recent applet, and it is pretty similar: 1 2 3 4 5 6 7 8 9 10
| <script src="http://www.java.com/js/deployJava.js"></script> <script> var attributes = {code:'com.adonax.texturebuilder.SimplexTextureApplet.class', archive:'SimplexBuilder121207.jar', width:1108, height:854} ; var parameters = {fontSize:10} ; var version = '1.6' ; deployJava.runApplet(attributes, parameters, version); </script> |
The second example is the applet for Sivi at the following URL (and it will "break" as soon as I update the jar). http://hexara.com/SimplexBuilder.htmlThe tricky thing, I think, is specifying the location of the class with the "init" method correctly. You have to spell out the packages. And of course the jar file has to be spelled correctly as well. This template is very fussy about syntax. The nice thing about the "bot blocker" example is that it shows a bit of interaction with the start and stop buttons. By giving the app and "id" you can reference methods in it. It was also cracked by Notch and hacked by DzzD in short order.  Hope this helps!
|
|
|
|
deepthought
|
 |
«
Reply #7 - Posted
2013-01-03 23:20:04 » |
|
Do you need to be able to do it in HTML? I was u der the impression the kongregate API for JavaScript is distributed as a .is file.
|
Mad Scientist
|
|
|
Mac70
|
 |
«
Reply #8 - Posted
2013-01-03 23:32:01 » |
|
Kongregate forces you to use HTML iframe. I have managed to write basic LWJGL working applet, it seems that from this moment everything will be much easier.
I will try to write and publish Kongregate API for Java soon, I consider that my problem is solved.
|
|
|
|
Mac70
|
 |
«
Reply #9 - Posted
2013-01-04 12:36:41 » |
|
Now I have another question - I can activate Java methods using JavaScript, but how to activate JavaScript functions using Java code? Naturally we are still talking about JavaScript inside HTML page.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
philfrei
|
 |
«
Reply #10 - Posted
2013-01-04 16:17:09 » |
|
I only know about communicating between jars, which is possible, or making JavaScript functions that look inside jars. Maybe someone else will have an answer but I don't have enough experience here. The only way I know is via Servlets & JSP which is a whole different technology, and one I am still learning about.
|
|
|
|
deepthought
|
 |
«
Reply #11 - Posted
2013-01-04 17:42:27 » |
|
Netscape.JavaScript.jsobject doesn't work?
|
Mad Scientist
|
|
|
Mac70
|
 |
«
Reply #12 - Posted
2013-01-04 19:05:50 » |
|
Netscape.JavaScript.jsobject doesn't work?
Yes, it no longer works. There are lots of JSObject tutorials, but all of them are useless, so I am looking for another solution. Edit: I found something like DOMService, but there are no tutorials or even understandable description. May this class be useful? If yes - how can I use it?
|
|
|
|
deepthought
|
 |
«
Reply #13 - Posted
2013-01-04 22:22:48 » |
|
you could use phillfrei's code to pull the applet for which function needs to be called and to deliver the result.
also you said you could connect with JS in js files. could you please put up a file with your example and sources?
strangely enough, the sun demos with jsobject worked for me ( maybe it's just opera)
|
Mad Scientist
|
|
|
|
deepthought
|
 |
«
Reply #15 - Posted
2013-01-05 14:56:51 » |
|
That's pretty much what I was saying.
|
Mad Scientist
|
|
|
Mac70
|
 |
«
Reply #16 - Posted
2013-01-05 21:55:59 » |
|
Philfrei, can you show BotBlocker code?
By the way, I am using LWJGL Applet Loader to load my LWJGL applet, so I have another problem - as "code" attribute I must use "org.lwjgl.util.applet.AppletLoader". Is it still possible to use methods from Java somehow?
|
|
|
|
deepthought
|
 |
«
Reply #17 - Posted
2013-01-06 03:43:29 » |
|
Try creating a function as a variable and pass it to your applet and see what kind of object the applet gets passed. Like in topic - is it possible to connect Java with JavaScript in HTML? I know that connecting with JS in .js files is possible and I did it, but I need to connect with JS in HTML. I need this because I am trying to write Kongregate API for Java.  Could you please put up code for this?
|
Mad Scientist
|
|
|
philfrei
|
 |
«
Reply #18 - Posted
2013-01-06 04:35:18 » |
|
Was there a specific question about BotBlocker?
The html/js function calls into BotBlocker invoke public java methods that have void returns. But there's no reason you can't call a method with a return value, as far as I know.
The button and text field in the app itself are Swing components, are part of the app, not the html or js.
|
|
|
|
Mac70
|
 |
«
Reply #19 - Posted
2013-01-06 13:28:17 » |
|
I currently have problem with LWJGL applet loader, as I can't use your code directly, philfrei. I must find a way to go through applet loader and call methods from my applet. Could you please put up code for this?
Code that reads javaScript file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| import java.io.IOException; import java.io.InputStream; import java.util.logging.Level; import java.util.logging.Logger; import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException;
public class KongregateAPI { ScriptEngineManager jsM; ScriptEngine engine; Bindings bind; int[] ints = new int[100]; public KongregateAPI() { jsM=new ScriptEngineManager(); engine = jsM.getEngineByName("JavaScript"); bind = engine.getBindings(ScriptContext.ENGINE_SCOPE); bind.put("toJava", System.err); try { engine.eval(readScript("TestFunction.js")); } catch (ScriptException ex) { Logger.getLogger(KongregateAPI.class.getName()).log(Level.SEVERE, null, ex); } } private String readScript(String source) { InputStream scriptFile = KongregateAPI.class.getResourceAsStream(source); int end = 0; try { end = scriptFile.available(); } catch (IOException ex) { Logger.getLogger(KongregateAPI.class.getName()).log(Level.SEVERE, null, ex); } int[] arr = new int[end]; String result=""; for (int i=0; i<end; i++) { try { arr[i] = scriptFile.read(); result = result+(char)arr[i]; } catch (IOException ex) { Logger.getLogger(KongregateAPI.class.getName()).log(Level.SEVERE, null, ex); } } return result; } } |
TestFunction.js: 1 2 3 4 5 6
| myFunction();
function myFunction() { toJava.println(Math.PI); } |
Everything returns 3.141592653589793 into output. EDIT: I finally found a solution! Everything works now. 
|
|
|
|
BoBear2681
|
 |
«
Reply #20 - Posted
2013-01-06 16:41:13 » |
|
Yes, it no longer works. There are lots of JSObject tutorials, but all of them are useless, so I am looking for another solution.
I know it sounds like you're going another way with this, but I don't think this is true. This example on Oracle's site works for me with a Java 7 plugin.
|
|
|
|
Mac70
|
 |
«
Reply #21 - Posted
2013-01-06 16:45:44 » |
|
I already found a solution. In few days I will create and submit Java API for Kongregate and instructions how to submit Java2d/LWJGL game to Kongregate. Maybe someone will use it to submit his own game to Kongregate. 
|
|
|
|
deepthought
|
 |
«
Reply #22 - Posted
2013-01-06 17:40:27 » |
|
I definitely plan to submit one. Also, how did you do it?
|
Mad Scientist
|
|
|
Mac70
|
 |
«
Reply #23 - Posted
2013-01-06 17:53:50 » |
|
Check "Kongregate API for Java" topic. I written instructions how to submit LWJGL game, but after little modifications you can send Java2d game, too. 
|
|
|
|
deepthought
|
 |
«
Reply #24 - Posted
2013-01-06 17:56:45 » |
|
awesomeness 
|
Mad Scientist
|
|
|
Alan_W
|
 |
«
Reply #25 - Posted
2013-01-06 18:03:57 » |
|
IIRC the Netscape JSObject is no longer supported in Firefox, but the DOM interface model doesn't work with Plugin-1, which means it doesn't work with Mac's with the Apple Java Plugin. Now that Oracle are providing Java for Macs, it might be worth looking at the DOM Interface again.
|
Time flies like a bird. Fruit flies like a banana.
|
|
|
Mac70
|
 |
«
Reply #26 - Posted
2013-01-06 19:15:50 » |
|
awesomeness  Thanks. Now we just have to wait until the first Java game will appear on Kongregate. 
|
|
|
|
BoBear2681
|
 |
«
Reply #27 - Posted
2013-01-06 21:17:43 » |
|
IIRC the Netscape JSObject is no longer supported in Firefox, but the DOM interface model doesn't work with Plugin-1, which means it doesn't work with Mac's with the Apple Java Plugin. Now that Oracle are providing Java for Macs, it might be worth looking at the DOM Interface again.
Wow, good information, thanks. Just plain HTML 5 looks better every day!
|
|
|
|
|