|
![]() |
名片设计 CorelDRAW Illustrator AuotoCAD Painter 其他软件 Photoshop Fireworks Flash |
|
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; } 返回类别: 教程 上一教程: Reading and Writing a Properties File(From Sun) 下一教程: 如何用开源软件构建 java 编程环境 您可以阅读与"java生成JPEG 图像文件"相关的教程: · 如何将做好的JAVA程序生成可执行文件 · 好东西介绍,直接用 java 命令行动态生成jpg文件 · 用Java生成文本文件(收集) · 使用hibernate扩展工具hbm2java根据配置文件生成持久化对象类(2.1.2) · 头文件生成器-JAVAH.EXE |
![]() ![]() |
快精灵印艺坊 版权所有 |
首页![]() ![]() ![]() ![]() ![]() ![]() ![]() |