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