我有Java Swing应用程序。当用户点击MenuItem时,我想在HTML页面上显示图形,使用JavaScript。 Java代码:
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代码:
<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>
如何使用参数 double [] myPoints 调用函数 draw (在 body onload 上),从Java程序动态更改HTML?< / p>
感谢。