DeployJava working with LWJGL applet loader? (Java<>JavaScript communication)

Started by Mac70, January 05, 2013, 22:38:24

Previous topic - Next topic

Mac70

Like in topic. Is there any possibility to make DeployJava have access to applet init class, not only LWJGL applet loader? Maybe is there another solution to make using Java code from JavaScript inside HTML page possible?

Here is my current JavaScript code inside HTML page:

<script type="text/javascript" src="http://www.java.com/js/deployJava.js"></script>
<script type="text/javascript" src="http://www.kongregate.com/javascripts/kongregate_api.js"></script>

<script type="text/javascript">
	var attributes = {id:"game", code:"org.lwjgl.util.applet.AppletLoader",
		archive:"lwjgl_util_applet.jar",
		width:800, height:650} ;
	var parameters = {"al_title": "Star_Mayor",
		"al_main": "Core.Core",
		"al_jars": "Star_Mayor.jar, lwjgl.jar, jinput.jar, lwjgl_util.jar, PNGDecoder.jar, slick.jar",
		"al_windows": "Lib_Windows.jar",
		"al_linux": "Lib_Linux.jar",
		"al_mac": "Lib_Mac.jar"} ;
	var version = '1.7' ;

	deployJava.runApplet(attributes, parameters, version);

	var kongregate = parent.kongregate;
	function checkRequest(){return game.Core.api.checkRequest();}
	function passRequestValue(){return game.Core.api.passRequestValue();}
	function passRequestName(){return game.Core.api.passRequestName();}
	function passRequestType(){return game.Core.api.passRequestType();}
	function setPlayerName(name){game.Core.api.setPlayerName(name);}
	
	function timer() {
		setInterval(APICheck, 1000/60);
	}

	function APICheck() {
		if (checkRequest()==true) {
			var requestVal = passRequestValue();
			var requestName = passRequestName();
			var requestType = passRequestType();
			game.Core.KongAPI.cleanRequest();
			APIExecution(requestVal, requestType);
		}
	}

	function APIExecution(Val, Type) {
		// This function will transfer data to other functions
	}
	
	if(kongregate.services.isGuest()){
		setPlayerName("guest");
	}
	else {
		setPlayerName(kongregate.services.getUsername());
	}
	
	timer();
	
</script>


Class API (initialized in Core as "api":

package Lib.KongAPI;

public class API {
    
    public String playerName = "";
    private boolean isRequested=false;
    private int requestVal=0;
    private String requestName="";
    private String requestType="";
    
    public boolean checkRequest() {
        return isRequested;
    }
    
    public void cleanRequest() {
        isRequested=false;
    }
    
    public int passRequestValue() {
        return requestVal;
    }
    
    public String passRequestName() {
        return requestName;
    }
    
    public String passRequestType() {
        return requestType;
    }
    
    public void setRequest(String type, String name, int val) {
        requestType = type;
        requestName = name;
        requestVal= val;
        isRequested = true;
    }
    
    public void setPlayerName(String name) {
        playerName = name;
    }
    
}

kappa

Yes, use the AppletLoader.getApplet() method (from javascript) to get the loaded applet.

Mac70

New HTML/JS code:

This code opens Kongregate API:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Kongregate Game Shell</title>
    
    <script type="text/javascript" src="http://www.kongregate.com/javascripts/kongregate_api.js"></script>
    
    <style type="text/css">
      html{border: none; overflow: hidden; background-color: #333;height: 100%;}
      body{border: none; background-color: #333;margin:0; padding:0;}
    </style>
  </head>

  <body>
    <script type="text/javascript">
      function onLoadCompleted(){
        kongregate = kongregateAPI.getAPI();
        
        kongregateAPI.embedFrame("frame.html");
      }
      
      kongregateAPI.loadAPI(onLoadCompleted);
    </script>
    
    <div id="contentdiv" style="top:0px; left:0px; width:820px; height:670px; borders:none;">
    </div>
  </body>
</html>


This code initializes game:

<script type="text/javascript" src="http://www.java.com/js/deployJava.js"></script>
<script type="text/javascript" src="http://www.kongregate.com/javascripts/kongregate_api.js"></script>

<script type="text/javascript">
	var attributes = {id:"game", code:"org.lwjgl.util.applet.AppletLoader",
		archive:"lwjgl_util_applet.jar",
		width:800, height:650} ;
	var parameters = {"al_title": "Star_Mayor",
		"al_main": "Core.Core",
		"al_jars": "Star_Mayor.jar, lwjgl.jar, jinput.jar, lwjgl_util.jar, PNGDecoder.jar, slick.jar",
		"al_windows": "Lib_Windows.jar",
		"al_linux": "Lib_Linux.jar",
		"al_mac": "Lib_Mac.jar"} ;
	var version = '1.7' ;

	deployJava.runApplet(attributes, parameters, version);

	var kongregate = parent.kongregate;
	var appletloader = document.getElementById('game');
	var applet = appletloader.getApplet();
	function checkRequest(){return applet.api.checkRequest();}
	function passRequestValue(){return applet.api.passRequestValue();}
	function passRequestName(){return applet.api.passRequestName();}
	function passRequestType(){return applet.api.passRequestType();}
	function setPlayerName(name){applet.set();}
	
	function timer() {
		setInterval(function(){APICheck()}, 100);
	}

	function APICheck() {
		if(kongregate.services.isGuest()){
			setPlayerName("guest");
		}
		else {
			setPlayerName(kongregate.services.getUsername());
		}
		if (checkRequest()==true) {
			var requestVal = passRequestValue();
			var requestName = passRequestName();
			var requestType = passRequestType();
			game.Core.KongAPI.cleanRequest();
			APIExecution(requestVal, requestType);
		}
	}

	function APIExecution(Val, Type) {
		// This function will transfer data to other functions
	}
	
	timer();
	
</script>


For the test, I made set() method inside my applet init class which just changes one variable. I am trying to call it, but it seems that when setPlayerName(kongregate.services.getUsername()); is called nothing happens and everything after this call (still inside else) is ignored. Everything before call works well, alert(kongregate.services.getUsername()) returns correct (string) var.

alert(applet) returns null.

EDIT: I found a solution. I had to add timer that checks if game is fully loaded.