/** * Part of Linking Sketches Demo - Right hand sketch. #2 * Requires LinkSketches.pde in the canvas statement. * * James Brink * Revised: May 20, 2014 */ /** * Specify all the methods that can be called from Javascript */ interface JavaScript { void showXYCoordinatesJS(int x, int y); } // interface JavaScript /** * This method is called from Javascript and gives * the links to the left and middle sketches and to the Javascript. */ void setLinkSketch(Object[] pjs, JavaScript js) { pjsLeft = pjs[0]; pjsMiddle = pjs[1]; javascript = js; } // setLinkSketch Object pjsLeft, pjsMiddle; int xLine, yLine, xSquare, ySquare, xCircle, yCircle; JavaScript javascript; /** * Initialize the sketch */ void setup() { size(250,300); background(0); smooth(); noLoop(); xLine = 500; // Initially draw items off screen yLine = 500; // to hide them. xSquare = 500; ySquare = 500; xCircle = 500; yCircle = 500; } // setup /** * Draw the sketch */ void draw() { fill(0,0,0,20); rect(-1,-1,width+2,height+2); // draw lines stroke(#FFFF00); line(xLine,-1,xLine,height+2); line(-1,yLine,width+2,yLine); // draw square fill(#00FF00); rect(xSquare-10, ySquare-10, 20, 20); // draw circle stroke(#FF00FF); fill(#FF00FF); ellipse(xCircle, yCircle, 20, 20); } // draw /** * Respond to mouse movement by updating the circle location asking other * sketches to draw the circl. Also inform Javascript. */ void mouseMoved() { xCircle = mouseX; yCircle = mouseY; if (pjsLeft != null) { pjsLeft.drawCircle(xCircle, yCircle); } if (pjsMiddle != null) { pjsMiddle.drawCircle(xCircle, yCircle); } if (javascript != null) { javascript.showXYCoordinatesJS(xCircle, yCircle); } redraw(); } // mouseMoved /** * Called by Javascript to draw lines into the left halfg * of the sketch. */ void drawLines(int xCoor, int yCoor) { xLine = xCoor; yLine = yCoor; redraw(); } // drawLines /** * Get coordinates of Square */ void drawSquare(int xCoor, int yCoor) { xSquare = xCoor; ySquare = yCoor; redraw(); } // drawSquares void drawCircle(int xCoor, int yCoor) { xCircle = xCoor; yCircle = yCoor; if (pjsLeft != null) { pjsLeft.drawCircle(xCircle, yCircle); pjsMiddle.drawCircle(xCircle, yCircle); } redraw(); } // drawCircle