001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.image;
019:
020: import java.awt.Graphics;
021: import java.awt.Graphics2D;
022: import java.awt.Image;
023: import java.awt.MediaTracker;
024: import java.awt.RenderingHints;
025: import java.awt.color.ColorSpace;
026: import java.awt.geom.AffineTransform;
027: import java.awt.image.AffineTransformOp;
028: import java.awt.image.BufferedImage;
029: import java.awt.image.ColorConvertOp;
030: import java.awt.image.ConvolveOp;
031: import java.awt.image.Kernel;
032:
033: import javax.swing.JComponent;
034:
035: public class ScrollableImage extends JComponent {
036: private BufferedImage originalImage;
037:
038: private int width, height;
039:
040: private boolean transform = false;
041:
042: private static final float[] BLUR3x3 = { 1 / 10f, 1 / 10f, 1 / 10f,
043: 1 / 10f, 2 / 10f, 1 / 10f, 1 / 10f, 1 / 10f, 1 / 10f };
044:
045: public void setImage(Image image) {
046: try {
047: MediaTracker tracker = new MediaTracker(this );
048: tracker.addImage(image, 0);
049: tracker.waitForID(0);
050: } catch (Exception e) {
051: }
052: width = image.getWidth(this );
053: height = image.getHeight(this );
054: originalImage = new BufferedImage(width, height,
055: BufferedImage.TYPE_INT_RGB);
056: Graphics2D g2d = (Graphics2D) originalImage.getGraphics();
057: g2d.drawImage(image, 0, 0, this );
058: }
059:
060: public void setTransform(boolean transform) {
061: this .transform = transform;
062: }
063:
064: public void paint(Graphics g) {
065: BufferedImage bufferedImage = null;
066: if (originalImage != null && getWidth() > 0 && getHeight() > 0) {
067: int width = originalImage.getWidth();
068: int height = originalImage.getHeight();
069: int targetWidth = getWidth();
070: int targetHeight = getHeight();
071: double fac = 1;
072: if (((double) width > targetWidth)
073: || ((double) height > targetHeight)) {
074: double xfac = targetWidth / (double) width;
075: double yfac = targetHeight / (double) height;
076: fac = xfac < yfac ? xfac : yfac;
077: targetWidth = (int) Math.floor((double) width * fac);
078: targetHeight = (int) Math.floor((double) height * fac);
079: } else {
080: if (width < targetWidth)
081: targetWidth = width;
082: if (height < targetHeight)
083: targetHeight = height;
084: }
085: BufferedImage scaledImage = new BufferedImage(targetWidth,
086: targetHeight, BufferedImage.TYPE_INT_RGB);
087: Graphics2D g2d = (Graphics2D) scaledImage.getGraphics();
088: AffineTransform scaleTransform = new AffineTransform();
089:
090: if (((double) width > targetWidth)
091: || ((double) height > targetHeight)) {
092: scaleTransform.scale(fac, fac);
093: }
094:
095: RenderingHints hints = new RenderingHints(
096: RenderingHints.KEY_ANTIALIASING,
097: RenderingHints.VALUE_ANTIALIAS_ON);
098: g2d.drawImage(originalImage, new AffineTransformOp(
099: scaleTransform, hints), 0, 0);
100: if (transform) {
101: bufferedImage = new BufferedImage(targetWidth,
102: targetHeight, BufferedImage.TYPE_INT_RGB);
103: Kernel kernel = new Kernel(3, 3, BLUR3x3);
104: ConvolveOp cop = new ConvolveOp(kernel,
105: ConvolveOp.EDGE_NO_OP, null);
106: cop.filter(scaledImage, bufferedImage);
107: ColorConvertOp ccp = new ColorConvertOp(ColorSpace
108: .getInstance(ColorSpace.CS_GRAY), null);
109: ccp.filter(bufferedImage, bufferedImage);
110: } else {
111: bufferedImage = scaledImage;
112: }
113: }
114: if (bufferedImage != null) {
115: Graphics2D g2d = (Graphics2D) g;
116: g2d.drawImage(bufferedImage, 0, 0, this );
117: }
118: if (getBorder() != null) {
119: getBorder().paintBorder(this , g, 0, 0, getWidth(),
120: getHeight());
121: }
122: }
123: }
|