要实时作图?1。动态图:最好用applet,可以和servlet通过http tunneling传serialized object,画图很方便(做过,有体会)。2。静态图:继承一个JFrame,然后取得graphics,画图。再用acme的组件编码成jpg,传回去。这个我也做过,封成了taglib,做任意曲线。不过有限制。在linux/unix系统下,要配置DISPLAY参数。据说jdk1.4会有改变,不过我试过beta版,问题依旧。
调试欢乐多
我要做的是实时图。
恕我粗浅,http tunneling传serialized object是什么?能不能给个例子?
那位仁兄用过?过来帮帮我!
response.setContentType("application/x-java-serialized-object");输出用:
ObjectOutputStream out = new ObjectOutputStream(response.getOutputStream());
out.writeObject(new Double("123.4"));
out.flush();就把对象写出去了。在applet,用: URL url = new URL(protocol,host,port,this.sufix);
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
ObjectInputStream in = new ObjectInputStream(conn.getInputStream()); Object obj = in.readObject();
简单吧,呵呵。
servlet只处理读取和传输数据,然后Applet接受数据,并进行绘图,是这样吗?这样速度快不快?
能不能给出个具体的例子?
能不能给个画图的例子,没办法了,兄弟就在帮我一把,到时分全给你了!
import java.awt.event.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import com.borland.jbcl.layout.*;/**
* <p>Title: Allowance Trading System</p>
* <p>Description: The Allowance Trading System.</p>
* <p>Copyright: Copyright (c) 2001</p>
* <p>Company: Yup Studio</p>
* @author Miles.Z
* @version 1.07
*/public class RateEstimateMonitor extends Applet implements Runnable{
boolean isStandalone = false;
XYLayout xYLayout1 = new XYLayout();
Panel panel1 = new Panel();
Panel panel2 = new Panel();
Label label1 = new Label(); /**server param*/
String protocol;
String host;
int port; String sufix;
String destination;
boolean looping = true; //Get a parameter value
public String getParameter(String key, String def) {
return isStandalone ? System.getProperty(key, def) :
(getParameter(key) != null ? getParameter(key) : def);
} //Construct the applet
public RateEstimateMonitor() {} //Initialize the applet
public void init() {
sufix = this.getParameter("sufix", "");
destination = this.getParameter("destination","");
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
} URL CurPage = getCodeBase();
protocol = CurPage.getProtocol();
host = CurPage.getHost();
port = CurPage.getPort();
// protocol = "http";
// host = "localhost";
// port = 8080; // start up.
Thread t = new Thread(this);
t.start(); }
//Component initialization
private void jbInit() throws Exception {
this.setLayout(xYLayout1);
this.setBackground(new Color(255, 255, 190));
this.setFont(new java.awt.Font("SansSerif", 0, 12));
panel1.setBackground(Color.white);
panel2.setBackground(Color.blue);
xYLayout1.setWidth(237);
xYLayout1.setHeight(41);
label1.setAlignment(1);
this.add(panel1, new XYConstraints(2, 2, 231, 17));
this.add(panel2, new XYConstraints(1, 1, 233, 19));
this.add(label1, new XYConstraints(71, 21, 87, 15));
}
//Get Applet information
public String getAppletInfo() {
return "Applet Information";
}
//Get parameter info
public String[][] getParameterInfo() {
return null;
} public void checkRate() {
try{
URL url = new URL(protocol,host,port,this.sufix);
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
ObjectInputStream in = new ObjectInputStream(conn.getInputStream()); Object obj = in.readObject();
if(obj!=null) {
Double Rate = (Double)obj;
if(Rate.doubleValue()==1.0) looping = false;
repaintLabel(Rate);
repaintPanel(Rate);
System.err.println("===");
System.err.println(Rate.doubleValue());
} in.close(); } catch(Exception ex) {
looping = false; // stop.
System.err.println(ex.toString());
}
} public void repaintLabel(Double Rate) {
int rate = (int)(Rate.doubleValue()*100);
label1.setText(rate+"%");
label1.repaint();
} public void repaintPanel(Double Rate) {
double rate = Rate.doubleValue(); Graphics g = this.panel1.getGraphics();
g.clearRect(0,panel1.getSize().width,panel1.getSize().width,panel1.getSize().height);
g.setColor(new Color(144,144,255));
g.fillRect(1,1,(int)((panel1.getSize().width-2)*rate),panel1.getSize().height-2);
} public void run() {
while(looping) {
checkRate();
try{
Thread.currentThread().sleep(100);
} catch(InterruptedException ex) {}
} try {
URL url = new URL(protocol,host,port,destination);
getAppletContext().showDocument(url);
} catch(MalformedURLException ex) {
}
}
}
这个applet用来显示一个进度尺。
93-98行读了从http-tunneling传来的一个Integer对象(也就是一个整数了),紧接着就重绘了进度尺(fillRect,128)和label(setText,118)。
另外,我用的layout是borland的。你可以就用简单的,像borderLayout。至于servlet端,我给你的那几行放到doGet里就行了。gud luck!
有关这个问题,大家请到http://www.csdn.net/expert/topic/945/945722.xml?temp=.3982202
继续关照!