Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] Connecting Java with JavaScript in HTML? (Kongregate)  (Read 3698 times)
0 Members and 1 Guest are viewing this topic.
Offline 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. Smiley

Check out my Devblog! Smiley
Offline deepthought
« Reply #1 - Posted 2012-12-30 15:02:42 »

will kongregate accept java applets?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline 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. Wink

Check out my Devblog! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mac70
« Reply #3 - Posted 2012-12-31 17:27:25 »

Bump. Does anyone have idea how to do this connection?

Check out my Devblog! Smiley
Offline deepthought
« Reply #4 - Posted 2013-01-01 06:22:05 »

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.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Mac70
« Reply #5 - Posted 2013-01-03 16:58:41 »

Sadly this does not work anymore. Any other ideas?

Check out my Devblog! Smiley
Offline 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.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.  Roll Eyes

Hope this helps!

"It's after the end of the world! Don't you know that yet?"
Offline 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.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline 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.

Check out my Devblog! Smiley
Offline 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.

Check out my Devblog! Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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.

"It's after the end of the world! Don't you know that yet?"
Offline deepthought
« Reply #11 - Posted 2013-01-04 17:42:27 »

Netscape.JavaScript.jsobject doesn't work?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline 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?

Check out my Devblog! Smiley
Offline 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)

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Mac70
« Reply #14 - Posted 2013-01-05 12:27:16 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
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
}

Check out my Devblog! Smiley
Offline deepthought
« Reply #15 - Posted 2013-01-05 14:56:51 »

That's pretty much what I was saying.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline 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?

Check out my Devblog! Smiley
Offline 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. Smiley
Could you please put up code for this?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline 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.

"It's after the end of the world! Don't you know that yet?"
Offline 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. Smiley

Check out my Devblog! Smiley
Offline BoBear2681

JGO Coder


Medals: 19



« 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.
Offline 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. Wink

Check out my Devblog! Smiley
Offline deepthought
« Reply #22 - Posted 2013-01-06 17:40:27 »

I definitely plan to submit one. Also, how did you do it?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline 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. Wink

Check out my Devblog! Smiley
Offline deepthought
« Reply #24 - Posted 2013-01-06 17:56:45 »

awesomeness Grin

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Alan_W

JGO Knight


Medals: 8
Projects: 3


Java tames rock!


« 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.
Offline Mac70
« Reply #26 - Posted 2013-01-06 19:15:50 »

awesomeness Grin

Thanks. Now we just have to wait until the first Java game will appear on Kongregate. Smiley

Check out my Devblog! Smiley
Offline BoBear2681

JGO Coder


Medals: 19



« 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!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (30 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (113 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!