⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jpegimageexporter.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Created on 2005-1-20
 *
 */
package eti.bi.alphaminer.operation.result.export;


import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.swing.JComponent;
import com.prudsys.pdm.Transform.Filter.MiningFileFilter;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

import eti.bi.exception.AppException;
import eti.bi.exception.SysException;

/**
 * Export a JPanel to a JPEG file.
 * 
 * @author TWang On Jan 20, 2005.
 *
 */
public class JPEGImageExporter extends FileExporter {

	public final static MiningFileFilter FILTER;

	public final static String EXTENSION = "jpg";

	public final static String DESCRIPTION = "JPEG Images";

	public static File DEFAULT_FILE = new File("JPEGImage.jpg");

	private JComponent m_JComp = null;

	static {
		FILTER = new MiningFileFilter();
		FILTER.addExtension(EXTENSION);
		FILTER.setDescription(DESCRIPTION);
	}

	public JPEGImageExporter(JComponent a_Jcomp, File a_File,
			boolean a_EnforceFileName) {
		m_JComp = a_Jcomp;
		if (a_EnforceFileName && !FILTER.accept(a_File)) {
			String oldPath = a_File.getAbsolutePath();
			String newPath = oldPath.concat(".");
			newPath = newPath.concat(EXTENSION);
			File newFile = new File(newPath);
			m_ExportFile = newFile;
		} else {
			m_ExportFile = a_File;
		}
	}

	/* (non-Javadoc)
	 * @see eti.bi.alphaminer.common.util.FileExporter#export()
	 */
	public void export() throws SysException, AppException {
	    
	    //<<tyleung 20/4/2005  duplicate warning message
//		if (m_ExportFile != null && m_ExportFile.exists()) {
//			MessageDialog.showWarning("File '"+m_ExportFile.getAbsolutePath()+"' already exists and will be overwritten", DESCRIPTION);
//		}
	    // tyleung 20/4/2005>>
		
		// The JPanle is displayable after the paint() function finishes.
		if (! m_JComp.isDisplayable()){
			throw new SysException("The input JPanel is not displable yet!");
		}
		
		Rectangle rect = m_JComp.getBounds();
 		Image fileImage = m_JComp.createImage(rect.width, rect.height); 
 		
		Graphics g = fileImage.getGraphics();
		//write to the image
		m_JComp.paint(g);
		//dispose of the graphics content
		g.dispose();
		//Output the image
		int imgaeWidth = fileImage.getWidth(null);
		int imgaeHeight = fileImage.getHeight(null);
		BufferedImage tag = new BufferedImage(imgaeWidth, imgaeHeight,
				BufferedImage.TYPE_INT_RGB);
		tag.getGraphics().drawImage(fileImage, 0, 0, imgaeWidth, imgaeHeight,
				null);
		FileOutputStream out;
		try {
			out = new FileOutputStream(m_ExportFile);

			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

			encoder.encode(tag);
			out.close();
		} catch (ImageFormatException e1) {
			new AppException(e1.toString());
		} catch (IOException e1) {
			new AppException("Unable to access file "
					+ m_ExportFile.getAbsolutePath());
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -