JavaScript to Java calls
in
Programming Questions
•
3 years ago
So, this issue has been discussed a bit here
http://processing.org/discourse/yabb2/YaBB.pl?num=1190848859 but I was unable to solve me problems. Essentially, my issue is the same: I have created an applet (in Eclipse), deployed a jar which I packaged with the core library from Processing and, though I can make calls from the Applet to the javascript functions in the page, I can not go in the other direction.
Since I am already a bit familiar with JQuery and Prototype, both of which I am making use of in page for various reasons, I tried using selectors to get at the object but to no effect. Initially, I had been using the html as Processing had rendered it -- using <object> tags -- but have since tried <applet> tags. While both rendered, neither worked.
The function that I am trying to access is within the main class -- called squaresApplet -- which extends PApplet.
What on earth can I do to solve this? I realize that I could simply construct a button in processing .. but that's not nearly as elegant.
For reference, the java, html and javascript code. The function I want to call is called 'reset()'.
Since I am already a bit familiar with JQuery and Prototype, both of which I am making use of in page for various reasons, I tried using selectors to get at the object but to no effect. Initially, I had been using the html as Processing had rendered it -- using <object> tags -- but have since tried <applet> tags. While both rendered, neither worked.
The function that I am trying to access is within the main class -- called squaresApplet -- which extends PApplet.
What on earth can I do to solve this? I realize that I could simply construct a button in processing .. but that's not nearly as elegant.
For reference, the java, html and javascript code. The function I want to call is called 'reset()'.
- import processing.core.*;
- import netscape.javascript.JSObject;
- public class squaresApplet extends PApplet {
- int canvasWidth = 700;
- int canvasHeight = 300;
- float[][] squares;
- int oldSquareClicked = -1;
- int newSquareClicked = -1;
- int squareHover = -1;
- boolean[] linesVisible = new boolean[12];
- public void setup() {
- size(this.canvasWidth, this.canvasHeight);
- background(color(255, 255, 255));
- strokeWeight(1.0F);
- frameRate(15.0F);
- smooth();
- this.squares = new float[4][3];
- this.squares[0][0] = (this.canvasWidth / 3);
- this.squares[0][1] = (this.canvasHeight / 6);
- this.squares[0][2] = 30.0F;
- this.squares[1][0] = (this.canvasWidth * 2 / 3);
- this.squares[1][1] = (this.canvasHeight / 6);
- this.squares[1][2] = 30.0F;
- this.squares[2][0] = (this.canvasWidth / 3);
- this.squares[2][1] = (this.canvasHeight * 5 / 6);
- this.squares[2][2] = 30.0F;
- this.squares[3][0] = (this.canvasWidth * 2 / 3);
- this.squares[3][1] = (this.canvasHeight * 5 / 6);
- this.squares[3][2] = 30.0F;
- this.linesVisible[0] = false;
- this.linesVisible[1] = false;
- this.linesVisible[2] = false;
- this.linesVisible[3] = false;
- this.linesVisible[4] = false;
- this.linesVisible[5] = false;
- this.linesVisible[6] = false;
- this.linesVisible[7] = false;
- this.linesVisible[8] = false;
- this.linesVisible[9] = false;
- this.linesVisible[10] = false;
- this.linesVisible[11] = false;
- }
- public void draw() {
- update(this.mouseX, this.mouseY);
- /*if (this.hoverNext)
- fill(color(255, 255, 255));
- else {
- fill(color(211, 211, 211));
- }
- rect(530.0F, 365.0F, 165.0F, 30.0F);
- if (this.hoverReset)
- fill(color(255, 255, 255));
- else {
- fill(color(211, 211, 211));
- }
- rect(530.0F, 333.0F, 165.0F, 30.0F);
- PFont localPFont = loadFont("Ziggurat-HTF-Black-32.vlw");
- textFont(localPFont, 50.0F);
- fill(0);
- text("Reset", 590.0F, 355.0F, 30.0F, 20.0F);
- text("Next", 595.0F, 385.0F, 30.0F, 20.0F);*/
- stroke(100);
- int i = 0;
- int j = 1;
- int k = 2;
- for (int m = 0; m <= 3; m++) {
- if ((this.squareHover == m) || (this.newSquareClicked == m)) {
- fill(255.0F, 215.0F, 0.0F);
- } else {
- fill(0.0F, 121.0F, 184.0F);
- }
- rect(this.squares[m][i], this.squares[m][j], this.squares[m][k],
- this.squares[m][k]);
- }
- for (int m = 0; m <= 11; m++) {
- if (this.linesVisible[m] == false) {
- eraseLine(m);
- }
- }
- for (int m = 0; m <= 11; m++)
- if (this.linesVisible[m] != false)
- drawLine(m);
- }
- public void eraseLine(int paramInt) {
- strokeWeight(7.0F);
- fill(color(255, 255, 255));
- stroke(color(255, 255, 255));
- int[][] arrayOfInt = getPointsArray();
- int i = 0;
- int j = 1;
- if (paramInt == 0) {
- line(arrayOfInt[0][i], arrayOfInt[0][j], arrayOfInt[1][i],
- arrayOfInt[1][j]);
- triangle(arrayOfInt[1][i], arrayOfInt[1][j], arrayOfInt[1][i] - 10,
- arrayOfInt[1][j] + 10, arrayOfInt[1][i] - 10,
- arrayOfInt[1][j] - 10);
- } else if (paramInt == 1) {
- line(arrayOfInt[0][i], arrayOfInt[0][j], arrayOfInt[1][i],
- arrayOfInt[1][j]);
- triangle(arrayOfInt[0][i], arrayOfInt[0][j], arrayOfInt[0][i] + 10,
- arrayOfInt[0][j] + 10, arrayOfInt[0][i] + 10,
- arrayOfInt[0][j] - 10);
- } else if (paramInt == 2) {
- line(arrayOfInt[2][i], arrayOfInt[2][j], arrayOfInt[3][i],
- arrayOfInt[3][j]);
- triangle(arrayOfInt[3][i], arrayOfInt[3][j], arrayOfInt[3][i] - 10,
- arrayOfInt[3][j] - 10, arrayOfInt[3][i] + 10,
- arrayOfInt[3][j] - 10);
- } else if (paramInt == 3) {
- line(arrayOfInt[2][i], arrayOfInt[2][j], arrayOfInt[3][i],
- arrayOfInt[3][j]);
- triangle(arrayOfInt[2][i], arrayOfInt[2][j], arrayOfInt[2][i] - 10,
- arrayOfInt[2][j] + 10, arrayOfInt[2][i] + 10,
- arrayOfInt[2][j] + 10);
- } else if (paramInt == 4) {
- line(arrayOfInt[4][i], arrayOfInt[4][j], arrayOfInt[5][i],
- arrayOfInt[5][j]);
- triangle(arrayOfInt[5][i], arrayOfInt[5][j], arrayOfInt[5][i] + 10,
- arrayOfInt[5][j] + 10, arrayOfInt[5][i] + 10,
- arrayOfInt[5][j] - 10);
- } else if (paramInt == 5) {
- line(arrayOfInt[4][i], arrayOfInt[4][j], arrayOfInt[5][i],
- arrayOfInt[5][j]);
- triangle(arrayOfInt[4][i], arrayOfInt[4][j], arrayOfInt[4][i] - 10,
- arrayOfInt[4][j] + 10, arrayOfInt[4][i] - 10,
- arrayOfInt[4][j] - 10);
- } else if (paramInt == 6) {
- line(arrayOfInt[6][i], arrayOfInt[6][j], arrayOfInt[7][i],
- arrayOfInt[7][j]);
- triangle(arrayOfInt[7][i], arrayOfInt[7][j], arrayOfInt[7][i] - 10,
- arrayOfInt[7][j] + 10, arrayOfInt[7][i] + 10,
- arrayOfInt[7][j] + 10);
- } else if (paramInt == 7) {
- line(arrayOfInt[6][i], arrayOfInt[6][j], arrayOfInt[7][i],
- arrayOfInt[7][j]);
- triangle(arrayOfInt[6][i], arrayOfInt[6][j], arrayOfInt[6][i] - 10,
- arrayOfInt[6][j] - 10, arrayOfInt[6][i] + 10,
- arrayOfInt[6][j] - 10);
- } else if (paramInt == 8) {
- line(arrayOfInt[8][i], arrayOfInt[8][j], arrayOfInt[10][i],
- arrayOfInt[10][j]);
- triangle(arrayOfInt[10][i], arrayOfInt[10][j], arrayOfInt[10][i],
- arrayOfInt[10][j] - 10, arrayOfInt[10][i] - 10,
- arrayOfInt[10][j]);
- } else if (paramInt == 9) {
- line(arrayOfInt[9][i], arrayOfInt[9][j], arrayOfInt[11][i],
- arrayOfInt[11][j]);
- triangle(arrayOfInt[11][i], arrayOfInt[11][j], arrayOfInt[11][i],
- arrayOfInt[11][j] - 10, arrayOfInt[11][i] + 10,
- arrayOfInt[11][j]);
- } else if (paramInt == 10) {
- line(arrayOfInt[8][i], arrayOfInt[8][j], arrayOfInt[10][i],
- arrayOfInt[10][j]);
- triangle(arrayOfInt[8][i], arrayOfInt[8][j], arrayOfInt[8][i],
- arrayOfInt[8][j] + 10, arrayOfInt[8][i] + 10,
- arrayOfInt[8][j]);
- } else if (paramInt == 11) {
- line(arrayOfInt[9][i], arrayOfInt[9][j], arrayOfInt[11][i],
- arrayOfInt[11][j]);
- triangle(arrayOfInt[9][i], arrayOfInt[9][j], arrayOfInt[9][i],
- arrayOfInt[9][j] + 10, arrayOfInt[9][i] - 10,
- arrayOfInt[9][j]);
- }
- }
- public void drawLine(int paramInt) {
- strokeWeight(5.0F);
- fill(color(100));
- stroke(color(100));
- int[][] arrayOfInt = getPointsArray();
- int i = 0;
- int j = 1;
- if (paramInt == 0) {
- line(arrayOfInt[0][i], arrayOfInt[0][j], arrayOfInt[1][i],
- arrayOfInt[1][j]);
- triangle(arrayOfInt[1][i], arrayOfInt[1][j], arrayOfInt[1][i] - 10,
- arrayOfInt[1][j] + 10, arrayOfInt[1][i] - 10,
- arrayOfInt[1][j] - 10);
- } else if (paramInt == 1) {
- line(arrayOfInt[0][i], arrayOfInt[0][j], arrayOfInt[1][i],
- arrayOfInt[1][j]);
- triangle(arrayOfInt[0][i], arrayOfInt[0][j], arrayOfInt[0][i] + 10,
- arrayOfInt[0][j] + 10, arrayOfInt[0][i] + 10,
- arrayOfInt[0][j] - 10);
- } else if (paramInt == 2) {
- line(arrayOfInt[2][i], arrayOfInt[2][j], arrayOfInt[3][i],
- arrayOfInt[3][j]);
- triangle(arrayOfInt[3][i], arrayOfInt[3][j], arrayOfInt[3][i] - 10,
- arrayOfInt[3][j] - 10, arrayOfInt[3][i] + 10,
- arrayOfInt[3][j] - 10);
- } else if (paramInt == 3) {
- line(arrayOfInt[2][i], arrayOfInt[2][j], arrayOfInt[3][i],
- arrayOfInt[3][j]);
- triangle(arrayOfInt[2][i], arrayOfInt[2][j], arrayOfInt[2][i] - 10,
- arrayOfInt[2][j] + 10, arrayOfInt[2][i] + 10,
- arrayOfInt[2][j] + 10);
- } else if (paramInt == 4) {
- line(arrayOfInt[4][i], arrayOfInt[4][j], arrayOfInt[5][i],
- arrayOfInt[5][j]);
- triangle(arrayOfInt[5][i], arrayOfInt[5][j], arrayOfInt[5][i] + 10,
- arrayOfInt[5][j] + 10, arrayOfInt[5][i] + 10,
- arrayOfInt[5][j] - 10);
- } else if (paramInt == 5) {
- line(arrayOfInt[4][i], arrayOfInt[4][j], arrayOfInt[5][i],
- arrayOfInt[5][j]);
- triangle(arrayOfInt[4][i], arrayOfInt[4][j], arrayOfInt[4][i] - 10,
- arrayOfInt[4][j] + 10, arrayOfInt[4][i] - 10,
- arrayOfInt[4][j] - 10);
- } else if (paramInt == 6) {
- line(arrayOfInt[6][i], arrayOfInt[6][j], arrayOfInt[7][i],
- arrayOfInt[7][j]);
- triangle(arrayOfInt[7][i], arrayOfInt[7][j], arrayOfInt[7][i] - 10,
- arrayOfInt[7][j] + 10, arrayOfInt[7][i] + 10,
- arrayOfInt[7][j] + 10);
- } else if (paramInt == 7) {
- line(arrayOfInt[6][i], arrayOfInt[6][j], arrayOfInt[7][i],
- arrayOfInt[7][j]);
- triangle(arrayOfInt[6][i], arrayOfInt[6][j], arrayOfInt[6][i] - 10,
- arrayOfInt[6][j] - 10, arrayOfInt[6][i] + 10,
- arrayOfInt[6][j] - 10);
- } else if (paramInt == 8) {
- line(arrayOfInt[8][i], arrayOfInt[8][j], arrayOfInt[10][i],
- arrayOfInt[10][j]);
- triangle(arrayOfInt[10][i], arrayOfInt[10][j], arrayOfInt[10][i],
- arrayOfInt[10][j] - 10, arrayOfInt[10][i] - 10,
- arrayOfInt[10][j]);
- } else if (paramInt == 9) {
- line(arrayOfInt[9][i], arrayOfInt[9][j], arrayOfInt[11][i],
- arrayOfInt[11][j]);
- triangle(arrayOfInt[11][i], arrayOfInt[11][j], arrayOfInt[11][i],
- arrayOfInt[11][j] - 10, arrayOfInt[11][i] + 10,
- arrayOfInt[11][j]);
- } else if (paramInt == 10) {
- line(arrayOfInt[8][i], arrayOfInt[8][j], arrayOfInt[10][i],
- arrayOfInt[10][j]);
- triangle(arrayOfInt[8][i], arrayOfInt[8][j], arrayOfInt[8][i],
- arrayOfInt[8][j] + 10, arrayOfInt[8][i] + 10,
- arrayOfInt[8][j]);
- } else if (paramInt == 11) {
- line(arrayOfInt[9][i], arrayOfInt[9][j], arrayOfInt[11][i],
- arrayOfInt[11][j]);
- triangle(arrayOfInt[9][i], arrayOfInt[9][j], arrayOfInt[9][i],
- arrayOfInt[9][j] + 10, arrayOfInt[9][i] - 10,
- arrayOfInt[9][j]);
- }
- }
- public int[][] getPointsArray() {
- int[][] arrayOfInt = new int[12][2];
- int i = 30;
- arrayOfInt[0][0] = (this.canvasWidth / 3 + i + 10);
- arrayOfInt[0][1] = (this.canvasHeight / 6 + i / 2);
- arrayOfInt[1][0] = (this.canvasWidth * 2 / 3 - 10);
- arrayOfInt[1][1] = (this.canvasHeight / 6 + i / 2);
- arrayOfInt[2][0] = (this.canvasWidth * 2 / 3 + i / 2);
- arrayOfInt[2][1] = (this.canvasHeight / 6 + i + 10);
- arrayOfInt[3][0] = (this.canvasWidth * 2 / 3 + i / 2);
- arrayOfInt[3][1] = (this.canvasHeight * 5 / 6 - 10);
- arrayOfInt[4][0] = (this.canvasWidth * 2 / 3 - 10);
- arrayOfInt[4][1] = (this.canvasHeight * 5 / 6 + i / 2);
- arrayOfInt[5][0] = (this.canvasWidth / 3 + i + 10);
- arrayOfInt[5][1] = (this.canvasHeight * 5 / 6 + i / 2);
- arrayOfInt[6][0] = (this.canvasWidth / 3 + i / 2);
- arrayOfInt[6][1] = (this.canvasHeight * 5 / 6 - 10);
- arrayOfInt[7][0] = (this.canvasWidth / 3 + i / 2);
- arrayOfInt[7][1] = (this.canvasHeight / 6 + i + 10);
- arrayOfInt[8][0] = (this.canvasWidth / 3 + i + 7);
- arrayOfInt[8][1] = (this.canvasHeight / 6 + i + 7);
- arrayOfInt[9][0] = (this.canvasWidth * 2 / 3 - 7);
- arrayOfInt[9][1] = (this.canvasHeight / 6 + i + 7);
- arrayOfInt[10][0] = (this.canvasWidth * 2 / 3 - 7);
- arrayOfInt[10][1] = (this.canvasHeight * 5 / 6 - 7);
- arrayOfInt[11][0] = (this.canvasWidth / 3 + i + 7);
- arrayOfInt[11][1] = (this.canvasHeight * 5 / 6 - 7);
- return arrayOfInt;
- }
- public void update(int paramInt1, int paramInt2) {
- int i = 0;
- int j = 1;
- int k = 2;
- if ((paramInt1 < this.canvasWidth * 2 / 3 + 30 + 5)
- && (paramInt1 > this.canvasWidth / 3 - 5)
- && (paramInt2 > this.canvasHeight / 6)
- && (paramInt2 < this.canvasHeight * 5 / 6 + 30 + 5)) {
- if (isInSquare(paramInt1, paramInt2, this.squares[0][i],
- this.squares[0][j], this.squares[0][k])) {
- this.squareHover = 0;
- } else if (isInSquare(paramInt1, paramInt2, this.squares[1][i],
- this.squares[1][j], this.squares[1][k])) {
- this.squareHover = 1;
- } else if (isInSquare(paramInt1, paramInt2, this.squares[2][i],
- this.squares[2][j], this.squares[2][k])) {
- this.squareHover = 2;
- } else if (isInSquare(paramInt1, paramInt2, this.squares[3][i],
- this.squares[3][j], this.squares[3][k]))
- this.squareHover = 3;
- else
- this.squareHover = -1;
- } else {
- this.squareHover = -1;
- }
- }
- public boolean isInSquare(int paramInt1, int paramInt2, float paramFloat1,
- float paramFloat2, float paramFloat3) {
- return (paramInt1 >= paramFloat1)
- && (paramInt1 <= paramFloat1 + paramFloat3)
- && (paramInt2 >= paramFloat2)
- && (paramInt2 <= paramFloat2 + paramFloat3);
- }
- public void mousePressed() {
- int i = 0;
- int j = 1;
- int k = 2;
- //JSObject.getWindow(this).eval( "alert("Processing says hello.")" );
- JSObject jso = JSObject.getWindow(this);
- if ((this.mouseX < this.canvasWidth * 2 / 3 + 30 + 5)
- && (this.mouseX > this.canvasWidth / 3 - 5)
- && (this.mouseY > this.canvasHeight / 6)
- && (this.mouseY < this.canvasHeight * 5 / 6 + 30 + 5)) {
- if (isInSquare(this.mouseX, this.mouseY, this.squares[0][i],
- this.squares[0][j], this.squares[0][k])) {
- setNewSquareClicked(0);
- } else if (isInSquare(this.mouseX, this.mouseY, this.squares[1][i],
- this.squares[1][j], this.squares[1][k])) {
- setNewSquareClicked(1);
- } else if (isInSquare(this.mouseX, this.mouseY, this.squares[2][i],
- this.squares[2][j], this.squares[2][k])) {
- setNewSquareClicked(2);
- } else if (isInSquare(this.mouseX, this.mouseY, this.squares[3][i],
- this.squares[3][j], this.squares[3][k])) {
- setNewSquareClicked(3);
- }
- setLinesVisible();
- if ((this.oldSquareClicked != -1) && (this.newSquareClicked != -1)) {
- // newAction('AddRelation')
- try {
- jso.eval("newAction('AddRelation')");
- //jso.eval("alert('Just added a new relation?')");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- // ("newAction()", "AddRelation");
- this.oldSquareClicked = -1;
- this.newSquareClicked = -1;
- }
- } /*else if ((this.mouseX >= 530) && (this.mouseX <= 530 + 165)
- && (this.mouseY >= 365) && (this.mouseY <= 365 + 30)) {
- // newAction('EndTrial');
- if (jso != null)
- try {
- jso.eval("newAction('EndTrial')");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- } else if ((this.mouseX >= 530) && (this.mouseX <= 530 + 165)
- && (this.mouseY >= 333) && (this.mouseY <= 333 + 30)) {
- this.reset();
- // newAction('Reset');
- if (jso != null)
- try {
- jso.eval("newAction('Reset')");
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }*/
- }
- public void setNewSquareClicked(int paramInt) {
- this.oldSquareClicked = this.newSquareClicked;
- this.newSquareClicked = paramInt;
- }
- public void setLinesVisible() {
- JSObject jso = JSObject.getWindow(this);
- if (this.oldSquareClicked == 0) {
- if (this.newSquareClicked == 1)
- this.linesVisible[0] = true;
- else if (this.newSquareClicked == 2)
- this.linesVisible[7] = true;
- else if (this.newSquareClicked == 3)
- this.linesVisible[8] = true;
- } else if (this.oldSquareClicked == 1) {
- if (this.newSquareClicked == 0)
- this.linesVisible[1] = true;
- else if (this.newSquareClicked == 2)
- this.linesVisible[9] = true;
- else if (this.newSquareClicked == 3)
- this.linesVisible[2] = true;
- } else if (this.oldSquareClicked == 2) {
- if (this.newSquareClicked == 0)
- this.linesVisible[6] = true;
- else if (this.newSquareClicked == 1)
- this.linesVisible[11] = true;
- else if (this.newSquareClicked == 3)
- this.linesVisible[5] = true;
- } else if (this.oldSquareClicked == 3) {
- if (this.newSquareClicked == 0)
- this.linesVisible[10] = true;
- else if (this.newSquareClicked == 1)
- this.linesVisible[3] = true;
- else if (this.newSquareClicked == 2) {
- this.linesVisible[4] = true;
- }
- }
- if ((oldSquareClicked != -1) && (newSquareClicked != -1)) {
- // updateSquaresArray(oldSquareClicked, newSquareClicked);
- if(jso != null )
- try {
- //jso.eval("alert('"+this.oldSquareClicked+ "','" + this.newSquareClicked+"')");
- jso.eval("updateSquaresArray('"+this.oldSquareClicked+ "','" + this.newSquareClicked+"')");
- } catch (Exception ex) {
- ex.printStackTrace(); }
- }
- }
- public void resetLinesVisible() {
- for (int i = 0; i <= 11; i++)
- this.linesVisible[i] = false;
- }
- public void resetSquareClicked() {
- this.oldSquareClicked = -1;
- this.newSquareClicked = -1;
- }
- public static void main(String[] paramArrayOfString) {
- PApplet.main(new String[] { "--bgcolor=#D4D0C8", "squaresApplet" });
- }
- <body>
- <div id="container" style=" margin-left: auto ; margin-right: auto ;">
- <div id="assertionSpace" style="font-family: Lucida Grande,Verdana,Arial,sans-serif; font-size: .8em; height:40px">
- <center><h1><label id="assertion">Quantified Assertion.</label></h1></center>
- </div>
- <applet id="squaresApplet-one"
- name="squaresApplet-one"
- archive="squaresApplet.jar"
- code="squaresApplet"
- width="700" MAYSCRIPT
- height="300">
- </applet>
- <div style="width:74%; float:left;"><h1></h1></div><button id="Next" class="button" style="width:24%;padding-right:1%;">Next</button>
- <div style="width:74%; float:left;"><h1></h1></div><button id="Reset" class="button" style="width:24%;padding-right:1%;">Reset</button>
- </div>
- </body>
- function newAction(actionType){
- if(actionType=="StartTrial" || "AddRelation"){
- callLogAction(actionType);
- } else if(actionType == "Reset" || actionType == "EndTrial"){
- //document.squaresApplet.squaresApplet.reset();
- //document.applets[0].reset();
- $j("#squaresApplet-one")[0].reset();
- resetSquaresArray();
- moveNumber = 0;
- // reset world
- // then call logAction
- }
- }
1
