首先:AJAX从Server.jsp页面中获取数据
function GetResult()
{
var xmlHttp = false;
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(!xmlhttp&&typeof XMLHttpRequest!='undefined'){
xmlhttp=new XMLHttpRequest();
}
xmlhttp.open("POST","Server.jsp",false);
var strResult="1,1,1";
xmlhttp.send();
strResult= xmlhttp.responseText;
return strResult;
}
function MyShow()
{
timer = window.setInterval("GetResult()",1000);
}
在jsp页面中显示applet
<applet ARCHIVE="batikMerge.jar" NAME="images1" codebase="class" alt="SVG"
code="svgImage.ShowSVG.class" width="400" height="400" align="center">
通过js脚本将获取的数据传递给Applet类中的变量
document.images1.setAllParam(GetResult());
images是applet的名字,setAllParam是applet类的一个方法。这个方法设置AllParam的值。
Result传递给Applet后,applet获取参数,然后修改Applet中SVG的元素属性。
修改参数的函数
public void modify_Device_value(SVGDocument svgdoc) {
initValue();
List<String> ldevice = this.init_DeviceName();
int ilen = ldevice.size();
Element edeviceName;
for (int i = 0; i < ilen; i++) {
edeviceName = svgdoc.getElementById(ldevice.get(i).toString());
edeviceName.setTextContent(Valuelist.get(i));
}
}
为了让Applet中SVG的运行信息实时更新,Applet采用显示实现这个动画属性,这样Applet中的SVG便有自动刷新的功能。
下面是显示SVG的Applet类,用到了Thread机制来实现刷新。
package svgImage;
public class ShowSVG extends Applet implements Runnable {
private static final long serialVersionUID = 0L;
private Thread reader;
public String allParam;
private List<String> Valuelist;
private JSVGCanvas svg_image;
private SVGDocument svgdoc;
public void destroy() {
super.destroy();
}
public void paint(Graphics g) {
initValue();
modify_Device_value(svgdoc);
}
public void initValue() {
获取传递进来的值,解析之后传递给每个变量
}
public void init() {
Panel pane = new Panel();
svg_image = new JSVGCanvas();
svg_image.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svg_image.setEnableImageZoomInteractor(true);
svg_image.setEnableZoomInteractor(true);// 图片大小
svg_image.setEnablePanInteractor(true);// 图片移动
svg_image.setPreferredSize(new Dimension(400, 400));
pane.add(svg_image);
this.add(pane);
}
public void modify_Device_value(SVGDocument svgdoc) {
}
public void run() {
Thread me = Thread.currentThread();
while (reader == me) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
repaint();
}
}
public void start() {
reader = new Thread(this);
reader.start();
}
public void stop() {
reader = null;
}
public void setAllParam(String allParam) {
从Ajax传到Applet的值在这里被传递给内部变量。
this.allParam = allParam;
}
}
java.lang.IllegalStateException: RunnableQueue not started or has exited
这个错误出现在上面的Applet中,程序运行一段时间胡出现那个错误,是因为在修改的时候,不能同步修改。
程序经过优化,修改,最后的结果是:
package svgImage;
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Panel;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import org.apache.batik.bridge.UpdateManager;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGDocument;
import java.util.*;
import javax.swing.JApplet;
public class ShowSVG extends Applet implements Runnable {
/**
*
*/
private static final long serialVersionUID = 0L;
private Thread reader;
public String allParam;
private List<String> Valuelist; //通过Ajax技术从外部传递过来
private JSVGCanvas svg_image;
private SVGDocument svgdoc;
private Panel pane ;
private List<String> lDeviceName;
public void destroy() {
svg_image.dispose();
}
public void paint(Graphics g) {
System.out.println("paint");
modifyDeviceValue();
}
public void splitValue() {
Valuelist = new ArrayList<String>();
String[] s=new String[]{"1","1","1"};
if(allParam!=null){
s= allParam.split(",");
}
for (int i = 0; i < s.length; i++) {
Valuelist.add(s[i]);
}
}
public void init() {
System.out.println("init");
svg_image = new JSVGCanvas();
pane= new Panel();
svg_image.setEnableImageZoomInteractor(true);
svg_image.setEnableZoomInteractor(true);// 图片大小
svg_image.setEnablePanInteractor(true);// 图片移动
// 预设SVG的大小,不设置的话,显示的图片会很小。 2008-07-28 by fengxinhe
svg_image.setPreferredSize(new Dimension(400, 400));
init_DeviceName();
try {
URL url = new URL(this.getCodeBase(), "../images/svgshow3.svg");
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory saxf = new SAXSVGDocumentFactory(parser);
Document doc = saxf.createDocument(url.toString());
svgdoc = (SVGDocument) doc;
String requesttype = this.getParam();// 要修改的元素
Element eeye = svgdoc.getElementById(requesttype);
NodeList nl = eeye.getElementsByTagName("circle");// 压力
Node node1 = nl.item(1);
Element animateElement = addAnimateElement(svgdoc);
node1.appendChild(animateElement);// 添加动画元素
} catch (IOException e) {
e.printStackTrace();
}
}
public Element addAnimateElement(SVGDocument svgdoc) {
Element animateNode = svgdoc.createElementNS(
"http://www.w3.org/2000/svg", "animate");// must include
// namespace
animateNode.setAttribute("attributeName", "fill");
animateNode.setAttribute("attributeType", "XML");
animateNode.setAttribute("from", "white");
animateNode.setAttribute("to", "red");
animateNode.setAttribute("dur", "0.4s");
animateNode.setAttribute("fill", "freeze");
animateNode.setAttribute("repeatCount", "indefinite");
return animateNode;
}
public String getParam() {
String devicetype = this.getParameter("type");
return devicetype;
}
public void modifyDeviceValue() {
splitValue();
if(um==null){
System.out.println("UpdateManager==null");
}else{
svg_image.getUpdateManager().getUpdateRunnableQueue().invokeLater
(new Runnable() {
public void run() {
Element edeviceName;
int ilen = lDeviceName.size();
for (int i = 0; i < ilen; i++) {
edeviceName = svgdoc.getElementById(lDeviceName.get(i).toString());
edeviceName.setTextContent(Valuelist.get(i));
}
}
});
}
}
public void init_DeviceName() {
lDeviceName = new ArrayList<String>();
// tspan id
String id_device_press = "vpress";
String id_device_vtemp = "vtemp";
String id_device_vhum = "vhum";
lDeviceName.add(id_device_press);
lDeviceName.add(id_device_vtemp);
lDeviceName.add(id_device_vhum);
}
public void run() {
System.out.println("run");
Thread me = Thread.currentThread();
while (reader == me) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
repaint();
}
}
public void start() {
svg_image.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svg_image.setSVGDocument(svgdoc);
pane.add(svg_image);
this.add(pane);
reader = new Thread(this);
System.out.println("start");
reader.start();
}
public void stop() {
System.out.println("stop");
svg_image.setSVGDocument(null);
reader = null;
}
public String getAllParam() {
return allParam;
}
public void setAllParam(String allParam) {
this.allParam = allParam;
}
}
function GetResult()
{
var xmlHttp = false;
try{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}catch(e){
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if(!xmlhttp&&typeof XMLHttpRequest!='undefined'){
xmlhttp=new XMLHttpRequest();
}
xmlhttp.open("POST","Server.jsp",false);
var strResult="1,1,1";
xmlhttp.send();
strResult= xmlhttp.responseText;
return strResult;
}
function MyShow()
{
timer = window.setInterval("GetResult()",1000);
}
在jsp页面中显示applet
<applet ARCHIVE="batikMerge.jar" NAME="images1" codebase="class" alt="SVG"
code="svgImage.ShowSVG.class" width="400" height="400" align="center">
通过js脚本将获取的数据传递给Applet类中的变量
document.images1.setAllParam(GetResult());
images是applet的名字,setAllParam是applet类的一个方法。这个方法设置AllParam的值。
Result传递给Applet后,applet获取参数,然后修改Applet中SVG的元素属性。
修改参数的函数
public void modify_Device_value(SVGDocument svgdoc) {
initValue();
List<String> ldevice = this.init_DeviceName();
int ilen = ldevice.size();
Element edeviceName;
for (int i = 0; i < ilen; i++) {
edeviceName = svgdoc.getElementById(ldevice.get(i).toString());
edeviceName.setTextContent(Valuelist.get(i));
}
}
为了让Applet中SVG的运行信息实时更新,Applet采用显示实现这个动画属性,这样Applet中的SVG便有自动刷新的功能。
下面是显示SVG的Applet类,用到了Thread机制来实现刷新。
package svgImage;
public class ShowSVG extends Applet implements Runnable {
private static final long serialVersionUID = 0L;
private Thread reader;
public String allParam;
private List<String> Valuelist;
private JSVGCanvas svg_image;
private SVGDocument svgdoc;
public void destroy() {
super.destroy();
}
public void paint(Graphics g) {
initValue();
modify_Device_value(svgdoc);
}
public void initValue() {
获取传递进来的值,解析之后传递给每个变量
}
public void init() {
Panel pane = new Panel();
svg_image = new JSVGCanvas();
svg_image.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svg_image.setEnableImageZoomInteractor(true);
svg_image.setEnableZoomInteractor(true);// 图片大小
svg_image.setEnablePanInteractor(true);// 图片移动
svg_image.setPreferredSize(new Dimension(400, 400));
pane.add(svg_image);
this.add(pane);
}
public void modify_Device_value(SVGDocument svgdoc) {
}
public void run() {
Thread me = Thread.currentThread();
while (reader == me) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
repaint();
}
}
public void start() {
reader = new Thread(this);
reader.start();
}
public void stop() {
reader = null;
}
public void setAllParam(String allParam) {
从Ajax传到Applet的值在这里被传递给内部变量。
this.allParam = allParam;
}
}
java.lang.IllegalStateException: RunnableQueue not started or has exited
这个错误出现在上面的Applet中,程序运行一段时间胡出现那个错误,是因为在修改的时候,不能同步修改。
程序经过优化,修改,最后的结果是:
package svgImage;
import java.applet.Applet;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Panel;
import java.io.IOException;
import java.net.URL;
import java.util.Random;
import org.apache.batik.bridge.UpdateManager;
import org.apache.batik.dom.svg.SAXSVGDocumentFactory;
import org.apache.batik.swing.JSVGCanvas;
import org.apache.batik.util.XMLResourceDescriptor;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.svg.SVGDocument;
import java.util.*;
import javax.swing.JApplet;
public class ShowSVG extends Applet implements Runnable {
/**
*
*/
private static final long serialVersionUID = 0L;
private Thread reader;
public String allParam;
private List<String> Valuelist; //通过Ajax技术从外部传递过来
private JSVGCanvas svg_image;
private SVGDocument svgdoc;
private Panel pane ;
private List<String> lDeviceName;
public void destroy() {
svg_image.dispose();
}
public void paint(Graphics g) {
System.out.println("paint");
modifyDeviceValue();
}
public void splitValue() {
Valuelist = new ArrayList<String>();
String[] s=new String[]{"1","1","1"};
if(allParam!=null){
s= allParam.split(",");
}
for (int i = 0; i < s.length; i++) {
Valuelist.add(s[i]);
}
}
public void init() {
System.out.println("init");
svg_image = new JSVGCanvas();
pane= new Panel();
svg_image.setEnableImageZoomInteractor(true);
svg_image.setEnableZoomInteractor(true);// 图片大小
svg_image.setEnablePanInteractor(true);// 图片移动
// 预设SVG的大小,不设置的话,显示的图片会很小。 2008-07-28 by fengxinhe
svg_image.setPreferredSize(new Dimension(400, 400));
init_DeviceName();
try {
URL url = new URL(this.getCodeBase(), "../images/svgshow3.svg");
String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory saxf = new SAXSVGDocumentFactory(parser);
Document doc = saxf.createDocument(url.toString());
svgdoc = (SVGDocument) doc;
String requesttype = this.getParam();// 要修改的元素
Element eeye = svgdoc.getElementById(requesttype);
NodeList nl = eeye.getElementsByTagName("circle");// 压力
Node node1 = nl.item(1);
Element animateElement = addAnimateElement(svgdoc);
node1.appendChild(animateElement);// 添加动画元素
} catch (IOException e) {
e.printStackTrace();
}
}
public Element addAnimateElement(SVGDocument svgdoc) {
Element animateNode = svgdoc.createElementNS(
"http://www.w3.org/2000/svg", "animate");// must include
// namespace
animateNode.setAttribute("attributeName", "fill");
animateNode.setAttribute("attributeType", "XML");
animateNode.setAttribute("from", "white");
animateNode.setAttribute("to", "red");
animateNode.setAttribute("dur", "0.4s");
animateNode.setAttribute("fill", "freeze");
animateNode.setAttribute("repeatCount", "indefinite");
return animateNode;
}
public String getParam() {
String devicetype = this.getParameter("type");
return devicetype;
}
public void modifyDeviceValue() {
splitValue();
if(um==null){
System.out.println("UpdateManager==null");
}else{
svg_image.getUpdateManager().getUpdateRunnableQueue().invokeLater
(new Runnable() {
public void run() {
Element edeviceName;
int ilen = lDeviceName.size();
for (int i = 0; i < ilen; i++) {
edeviceName = svgdoc.getElementById(lDeviceName.get(i).toString());
edeviceName.setTextContent(Valuelist.get(i));
}
}
});
}
}
public void init_DeviceName() {
lDeviceName = new ArrayList<String>();
// tspan id
String id_device_press = "vpress";
String id_device_vtemp = "vtemp";
String id_device_vhum = "vhum";
lDeviceName.add(id_device_press);
lDeviceName.add(id_device_vtemp);
lDeviceName.add(id_device_vhum);
}
public void run() {
System.out.println("run");
Thread me = Thread.currentThread();
while (reader == me) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
}
repaint();
}
}
public void start() {
svg_image.setDocumentState(JSVGCanvas.ALWAYS_DYNAMIC);
svg_image.setSVGDocument(svgdoc);
pane.add(svg_image);
this.add(pane);
reader = new Thread(this);
System.out.println("start");
reader.start();
}
public void stop() {
System.out.println("stop");
svg_image.setSVGDocument(null);
reader = null;
}
public String getAllParam() {
return allParam;
}
public void setAllParam(String allParam) {
this.allParam = allParam;
}
}