0

I have Java Swing application. When user clicks on MenuItem I want to show graph on HTML page, using JavaScript. Java code:

 if(e.getActionCommand().equals("show graph")){
        if(mainPopulation != null) {
            double[] myPoints = mainPopulation.getElements();
            Desktop d = Desktop.getDesktop();
            try {
                d.open(new File("D:/fp.html"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }
    }

JavaScript code:

<html>
 <head>
  <script type="application/x-javascript">
    function draw(params) {
      var canvas = document.getElementById("canvas");
      if (canvas.getContext) {
        var ctx = canvas.getContext("2d");
        ctx.font = '10pt Calibri';
        ctx.fillStyle = 'blue'; 
        for(i=0;i<params.length;i++){
            ctx.beginPath();
            ctx.moveTo(params[i], params[i+1]);
            ctx.lineTo(params[i + 2], params[i + 3]);
            ctx.stroke();           
            ctx.fillText('(' + params[i].toString() + ';' + params[i +1].toString() + ')', params[i], params[i+1]);
            i += 1;
        }
      }
    }
  </script>
 </head>
 <body onload="draw([10,10,20,20,30,30,40,40,40,100,50,90,70,90,60,50]);">
   <canvas id="canvas" width="1000" height="1000"></canvas>
 </body>
</html>

How can I call function draw(on body onload) with parameter double[] myPoints, dynamically change HTML from Java program?

Thanks.

7
  • Why not just draw the graph as a Swing control? Commented Mar 12, 2013 at 9:41
  • What Java program? A desktop app? An applet? Server-side code? Commented Mar 12, 2013 at 10:01
  • @user546252 : A simple desktop app. Commented Mar 12, 2013 at 10:24
  • @McDowell : I want to learn how Java interacts with Web technologies. Commented Mar 12, 2013 at 10:26
  • Problem solved! I open html-file as text file and then edit it with BufferedWriter, inserting into function call appropriate parameters. Commented Mar 12, 2013 at 11:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.