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.
will kongregate accept java applets?
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.
Bump. Does anyone have idea how to do this connection?
http://docs.oracle.com/javase/1.5.0/docs/guide/plugin/developer_guide/java_js.html
I’ll do a bit of expireminting when I get to an actual computulator tomorrow.
Sadly this does not work anymore. Any other ideas?
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:
<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:
<script language="javascript">
function startChallenge(){nobot.startNobot();}
function stopChallenge(){nobot.stop();}
</script>
I’m checking a more recent applet, and it is pretty similar:
<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.html
The 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!
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.
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.
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.
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.
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?
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)
I wanted to make simple game using Kongregate API, but I changed my mind and will host my “main” game Star Mayor directly.
Some links:
Kongregate public access preview key (you need to logout from Kongregate first to be able to use it!): http://www.kongregate.com/games/Mac70/lwjgl-test_preview?guest_access_key=cc0899466b61fa407f4b33f75695099a3fd6bdc34f01855bc5aab27237d4aa2e
Compiled code (it can’t run outside Kongregate): https://dl.dropbox.com/u/67758055/LWJGL%20test/LWJGL%20test.zip
Source code: https://dl.dropbox.com/u/67758055/LWJGL%20test/src.zip
I have one idea how to make Java comunicating with JavaScript in another way. I will show this as JavaScript pseudocode:
function timer() {
while (true) {
setTimeout(APICheck, 1000000000/60);
}
}
function APICheck() {
if (game.Core.KongAPI.checkRequest==true) {
var requestVal = game.Core.KongAPI.passRequestValue();
var requestType = game.Core.KongAPI.passRequestType();
game.Core.KongAPI.cleanRequest();
APIExecution(requestVal, requestType);
}
}
function APIExecution(Val, Type) {
// This function will transfer data to other functions
}
That’s pretty much what I was saying.
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?
Try creating a function as a variable and pass it to your applet and see what kind of object the applet gets passed.
Could you please put up code for this?
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.
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.
Code that reads javaScript file:
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:
myFunction();
function myFunction()
{
toJava.println(Math.PI);
}
Everything returns 3.141592653589793 into output.
EDIT: I finally found a solution! Everything works now.