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

📄 imageelement.java

📁 Java写的ERP系统
💻 JAVA
字号:
/******************************************************************************
 * The contents of this file are subject to the   Compiere License  Version 1.1
 * ("License"); You may not use this file except in compliance with the License
 * You may obtain a copy of the License at http://www.compiere.org/license.html
 * Software distributed under the License is distributed on an  "AS IS"  basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * The Original Code is                  Compiere  ERP & CRM  Business Solution
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc.
 * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved.
 * Contributor(s): ______________________________________.
 *****************************************************************************/
package org.compiere.print.layout;

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.util.*;
import java.net.*;
import java.sql.*;

import org.apache.log4j.Logger;

import org.compiere.print.*;
import org.compiere.util.DB;

/**
 *	Image Element
 *
 * 	@author 	Jorg Janke
 * 	@version 	$Id: ImageElement.java,v 1.4 2003/04/24 06:15:08 jjanke Exp $
 */
public class ImageElement extends PrintElement
{
	/**
	 *	Create Image from Image
	 *  @param image image
	 */
	public ImageElement(Image image)
	{
		m_image = image;
		if (m_image != null)
			log.debug(image);
		else
			log.error("Image is NULL");
	}	//	ImageElement

	/**
	 *	Create Image from URL
	 *	@param imageURLstring image url
	 */
	public ImageElement(String imageURLstring)
	{
		URL imageURL = getURL(imageURLstring);
		if (imageURL != null)
			m_image = Toolkit.getDefaultToolkit().getImage(imageURL);
		if (m_image != null)
			log.debug("URL=" + imageURL);
		else
			log.error("ImageElement not loaded - URL=" + imageURL);
	}	//	ImageElement

	/**
	 *	Create Image from URL
	 *  @param imageURL image url
	 */
	public ImageElement(URL imageURL)
	{
		if (imageURL != null)
			m_image = Toolkit.getDefaultToolkit().getImage(imageURL);
		if (m_image != null)
			log.debug("URL=" + imageURL);
		else
			log.error("ImageElement not loaded - URL=" + imageURL);
	}	//	ImageElement

	/**
	 *	Create Image from Attachment
	 * 	@param AD_PrintFormatItem_ID record id
	 */
	public ImageElement(int AD_PrintFormatItem_ID)
	{
		loadAttachment(AD_PrintFormatItem_ID);
	}	//	ImageElement

	/**	Logger				*/
	private Logger			log = Logger.getLogger (getClass());
	/**	The Image			*/
	private Image	m_image = null;

	/*************************************************************************/

	/**
	 * 	Get URL from String
	 *  @param urlString url or resource
	 *  @return URL or null
	 */
	private URL getURL (String urlString)
	{
		URL url = null;
		//	not a URL - may be a resource
		if (urlString.indexOf("//:") == -1)
		{
			ClassLoader cl = getClass().getClassLoader();
			url = cl.getResource(urlString);
			if (url != null)
				return url;
			log.error("getURL - Not found - " + urlString);
			return null;
		}
		//	load URL
		try
		{
			url = new URL (urlString);
		}
		catch (MalformedURLException ex)
		{
			log.error("getURL", ex);
		}
		return url;
	}	//	getURL;

	/**
	 * 	Load Attachment
	 * 	@param AD_PrintFormatItem_ID record id
	 */
	private void loadAttachment(int AD_PrintFormatItem_ID)
	{
		String sql = "SELECT BinaryData FROM AD_Attachment "
			+ "WHERE AD_Table_ID=489 AND Record_ID=?";
		byte[] imageData = null;
		try
		{
			PreparedStatement pstmt = DB.prepareStatement(sql);
			pstmt.setInt(1, AD_PrintFormatItem_ID);
			ResultSet rs = pstmt.executeQuery();
			if (rs.next())
			{
				Blob blob = rs.getBlob(1);
				imageData = blob.getBytes(0, (int)blob.length());	//	limits it to 64k
			}
			rs.close();
			pstmt.close();
		}
		catch (SQLException e)
		{
			log.error("loadAttachment", e);
		}
		if (imageData != null)
			m_image = Toolkit.getDefaultToolkit().createImage(imageData);
		if (m_image != null)
			log.debug("loadAttachment - Attachment Size=" + imageData.length);
		else
			log.error("loadAttachment - ImageElement not loaded - AD_PrintFormatItem_ID=" + AD_PrintFormatItem_ID);
	}	//	loadAttachment

	/*************************************************************************/

	/**
	 * 	Calculate Image Size.
	 * 	Set p_width & p_height
	 * 	@return true if calculated
	 */
	protected boolean calculateSize()
	{
		p_width = 0;
		p_height = 0;
		if (m_image == null)
			return true;
		//	we have an image
		waitForLoad(m_image);
		p_width = m_image.getWidth(this);
		p_height = m_image.getHeight(this);
		return true;
	}	//	calculateSize

	/**
	 * 	Paint Image
	 * 	@param g2D Graphics
	 *  @param pageStart top left Location of page
	 *  @param pageNo page number for multi page support (0 = header/footer) - ignored
	 *  @param ctx print context
	 *  @param isView true if online view (IDs are links)
	 */
	public void paint(Graphics2D g2D, int pageNo, Point2D pageStart, Properties ctx, boolean isView)
	{
		if (m_image == null)
			return;

		//	Position
		Point2D.Double location = getAbsoluteLocation(pageStart);
		int x = (int)location.x;
		if (MPrintFormatItem.FIELD_ALIGN_TRAILING.equals(p_FieldAlignmentType))
			x += p_maxWidth - p_width;
		else if (MPrintFormatItem.FIELD_ALIGN_CENTER.equals(p_FieldAlignmentType))
			x += (p_maxWidth - p_width) / 2;
		int y = (int)location.y;
		//
		g2D.drawImage(m_image, x, y, this);
	}	//	paint

}	//	ImageElement

⌨️ 快捷键说明

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