我制作的类:
package samples.javabean;
import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import org.jgraph.JGraph;
import org.jgraph.graph.DefaultCellViewFactory;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
public class Graphic {
String webroot;
String filename;
String random;
public Graphic()
{
webroot="e:\\workspace\\DBtest\\res";
filename="number.png";
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
public String getRandom() {
return random;
}
public void setRandom(String random) {
this.random = random;
}
public void paint()
{
GraphModel model=new DefaultGraphModel();
GraphLayoutCache view=new GraphLayoutCache(model,new DefaultCellViewFactory());
JGraph graph=new JGraph(model,view);
DefaultGraphCell cell=new DefaultGraphCell(random);
GraphConstants.setBounds(cell.getAttributes(),new Rectangle2D.Double(0,0,100,40));
GraphConstants.setGradientColor(cell.getAttributes(),Color.orange);
GraphConstants.setOpaque(cell.getAttributes(),true);
DefaultPort port=new DefaultPort();
cell.add(port);
graph.getGraphLayoutCache().insert(cell);
JFrame frame=new JFrame();
frame.getContentPane().add(new JScrollPane(graph));
frame.pack();
frame.setVisible(false);
try{
File f=new File(webroot,filename);
ImageOutputStream ios=ImageIO.createImageOutputStream(f);
BufferedImage img=graph.getImage(graph.getBackground(),1);
ImageIO.write(img,"png",ios);
ios.flush();
ios.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
调用的此类的jsp文件:
<%@ page language="java" contentType="text/html; charset=GB2312"
pageEncoding="GB2312"%>
<%@ page import="samples.javabean.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">