菜鸟高分求助,如何把Panel上的可视控件保存成图片 需求是这样的:在一个画布上(目前用Panel控件),画流程图。流程图包括节点和带箭号的连接线,现在的难题是如何画好的流程图保存成图片。请各位高手指教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.awt.image.*; /**JBuilder下编译测试通过 * 将某个组件上的图形或组件另存为文件 * @param comp * @param filename * @return */ public static boolean saveToJPEG(Component comp) { boolean result = true; try { int w = comp.getWidth(); int h = comp.getHeight(); BufferedImage image = (BufferedImage) comp.createImage(w, h); Graphics2D g2 = image.createGraphics(); comp.print(g2); String filename = LocalAccess.getSaveFileName(comp,"JavaComponent.jpg"); if(filename==null)return false; BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filename)); com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(bos); com.sun.image.codec.jpeg.JPEGEncodeParam jep = encoder.getDefaultJPEGEncodeParam(image); jep.setQuality(1.0f, false); encoder.setJPEGEncodeParam(jep); encoder.encode(image); bos.close(); } catch (Exception e) { result = false; e.printStackTrace(); } return result; } 在线坐等、、关于文件读写、、 java Canvas里图片不能完全显示 java中equals()方法和==比较 请教各位高手大大,如何能展现远程打开文件的进度条? 简单的字符串剪切 跪求!java解析 微软 projiect 文件的方法! JSP页面刷新闪烁的问题 真的有内存泄漏吗?高手进来! JLabel 请问高手: 关于文件读写的问题. sql server问题 applet与webservice加密通讯的问题??????????
* 将某个组件上的图形或组件另存为文件
* @param comp
* @param filename
* @return
*/
public static boolean saveToJPEG(Component comp) {
boolean result = true;
try {
int w = comp.getWidth();
int h = comp.getHeight();
BufferedImage
image = (BufferedImage) comp.createImage(w, h);
Graphics2D g2 = image.createGraphics();
comp.print(g2); String filename = LocalAccess.getSaveFileName(comp,"JavaComponent.jpg");
if(filename==null)return false;
BufferedOutputStream bos =
new BufferedOutputStream(new FileOutputStream(filename));
com.sun.image.codec.jpeg.JPEGImageEncoder encoder = com.sun.image.codec.jpeg.JPEGCodec.createJPEGEncoder(bos);
com.sun.image.codec.jpeg.JPEGEncodeParam jep = encoder.getDefaultJPEGEncodeParam(image);
jep.setQuality(1.0f, false);
encoder.setJPEGEncodeParam(jep);
encoder.encode(image);
bos.close();
}
catch (Exception e) {
result = false;
e.printStackTrace();
}
return result;
}