/*
* JImageView.java
*
* Created on 30. June 2009, 13:27
*
* Copyright (C) 30. June 2009 <Reiner>
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package shared.image;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import javax.swing.JComponent;
import javax.swing.RepaintManager;
/**
*
* @author reiner
*/
public class JImageView extends JComponent implements Printable
{
private Object m_interpolation = RenderingHints.VALUE_INTERPOLATION_BICUBIC;
private Image m_img;
public boolean hasImage()
{
return (m_img != null);
}
public void setImage(Image img)
{
m_img = img;
}
public void setImage(String imgPath)
{
m_img = Toolkit.getDefaultToolkit().createImage(imgPath);
}
public void paintComponent(Graphics g)
{
Graphics gd = g.create();
doPaint(gd, getSize(), false);
}
public boolean doPaint(Graphics g, Dimension dim, boolean bPrint)
{
if (m_img != null)
{
double dx=0, dy=0, d = 0;
Graphics2D g2d = (Graphics2D)g;
int w = m_img.getWidth(this);
int h = m_img.getHeight(this);
if (dim == null) dim = new Dimension(w, h);
dx = (double)dim.width / (double)w;
dy = (double)dim.height / (double)h;
d = Math.min(dx, dy);
g2d.scale(d, d);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, m_interpolation);
Rectangle rect = g2d.getClipBounds();
if (rect != null)
g2d.drawImage(m_img, (int)rect.getX(), (int)rect.getY(), (int)(rect.getX() + rect.getWidth()), (int)(rect.getY() + rect.getHeight()), (int)rect.getX(), (int)rect.getY(), (int)(rect.getX() + rect.getWidth()), (int)(rect.getY() + rect.getHeight()), this);
}
return false;
}
public int print(Graphics pg, PageFormat pageFormat, int pageIndex)
{
if (m_img != null && pageIndex == 0)
{
pg.translate((int)pageFormat.getImageableX(), (int)pageFormat.getImageableY());
RepaintManager currentManager = RepaintManager.currentManager(this);
currentManager.setDoubleBufferingEnabled(false);
Graphics2D g2d = (Graphics2D)pg;
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);
doPaint(pg, new Dimension((int)pageFormat.getImageableWidth(), (int)pageFormat.getImageableHeight()), true);
currentManager.setDoubleBufferingEnabled(true);
System.gc();
return PAGE_EXISTS;
}
return NO_SUCH_PAGE;
}
}