java生成JPEG 图像文件. 代码如下: 
 我的这段代码主要提供给我自己编写的工作流设计器对象使用(EWorkFlowControl ewf), EWorkFlowControl 继承了JComponent重载了绘制的方法,如果有兄弟需要用到这个功能的,只需要将EWorkFlowControl 替换成自己的JComponent对象即可。 
 /** 使当前输入的工作流对象上的图形生成JPEG 图像文件.  
* 每一个节点对应一个图片.  
* 建立的文件存放在jar 文件文件夹下,并创建一个子文件夹  
* 以节点的名称为文件夹的名称.  
* @param ewf 需要生成jpeg图片文件的工作流对象.  
* @return boolean: 返回一个布尔值,表示是否保存成功. 
 */  
public static boolean buildThePictrue(EWorkFlowControl ewf)  
{  
//modify by mw  
//列出流程中所有的node节点.  
//分别设置这些节点为选中状态,并存成图片(名称为节点的流水号)  
INode rootNode = ewf.getNote(); Rectangle bounds = ewf.getBounds();  
INode[] children = rootNode.getNodeChildren(); 
 int childrenLen = rootNode.getNodeChildCount();  
String nodeNumber = null;  
boolean flag = true;  
//消除所有节点得选中状态.  
for(int j = 0; j < childrenLen; j++) {  
children[j].setSelect(false); } 
 //生成总图(我是一个节点一个图像)  
try {  
java.awt.image.BufferedImage image; image = new java.awt.image.BufferedImage(bounds.width, bounds.height, java.awt.image.BufferedImage.TYPE_INT_RGB);  
Graphics g = image.getGraphics(); g.setColor(Color.white);  
g.fillRect(0, 0, bounds.width, bounds.height); 
 ewf.paintComponent(g);  
String path = System.getProperty("user.dir") +System.getProperty("file.separator") +ewf.getWorkFlowName() +"["+ewf.getWorkFlowCode()+"]"; 
 File fd = new File(path);  
if (!fd.exists()) { 
 fd.mkdirs();  
}  
String filePath = path + System.getProperty("file.separator")+"0.jpeg";  
File imgFile = new File(filePath);  
FileOutputStream fos = new FileOutputStream(imgFile);  
BufferedOutputStream bos = new BufferedOutputStream(fos);  
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);  
encoder.encode(image); bos.close();  
}  
catch(Exception ex) {  
flag = false;  
}  
//到此第一个图像已经生成!  
//循环生成图片  
for(int i = 0; i < childrenLen; i++)  
{  
nodeNumber = children[i].getNodeNumber(); 
 if (nodeNumber == null)  
{  
flag = false; continue;  
} else { 
 children[i].setSelect(true);  
try {  
java.awt.image.BufferedImage image; image = new java.awt.image.BufferedImage(bounds.width, bounds.height, java.awt.image.BufferedImage.TYPE_INT_RGB);  
Graphics g = image.getGraphics();  
g.setColor(Color.white);  
g.fillRect(0, 0, bounds.width, bounds.height);  
ewf.paintComponent(g);  
String path = System.getProperty("user.dir") +System.getProperty("file.separator") +ewf.getWorkFlowName() +"["+ewf.getWorkFlowCode()+"]";  
File fd = new File(path);  
if (!fd.exists()) {  
fd.mkdirs();  
}  
String filePath = path + System.getProperty("file.separator")+nodeNumber+".jpeg";  
File imgFile = new File(filePath);  
FileOutputStream fos = new FileOutputStream(imgFile);  
BufferedOutputStream bos = new BufferedOutputStream(fos); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);  
encoder.encode(image); bos.close();  
children[i].setSelect(false);  
} catch(Exception ex) {  
flag = false;  
} } }  
if (flag) {  
new ExceptionDialog(PIC_SAVE_OK,PIC_SAVE_DIALOG_TITLE, DesktopFrame.getCurrentInstance(), true).setVisible(true);  
} else {  
new ExceptionDialog(PIC_SAVE_ERROR,PIC_SAVE_DIALOG_TITLE, DesktopFrame.getCurrentInstance(), true).setVisible(true);  
} 
 return flag;  
}  
 
  |