Provides an abstract implementation of the BufferedImageOp
* interface. This class can be used to created new image filters based
* on BufferedImageOp
.
A color mixer filter can be used to mix a solid color to an image. The
* result is an image tinted by the specified color. The force of the effect
* can be controlled with the mixValue
, a number between 0.0 and
* 1.0 that can be seen as the percentage of the mix (0.0 does not affect the
* source image and 1.0 replaces all the pixels by the solid color).
The color of the pixels in the resulting image is computed as follows:
** cR = cS * (1 - mixValue) + cM * mixValue **
Definition of the parameters:
*cR
: color of the resulting pixelcS
: color of the source pixelcM
: the solid color to mix with the source imagemixValue
: strength of the mix, a value between 0.0 and 1.0Creates a new color mixer filter. The specified color will be used
* to tint the source image, with a mixing strength defined by
* mixValue
.
mixColor
is null
*/
public ColorTintFilter(Color mixColor, float mixValue) {
if (mixColor == null) {
throw new IllegalArgumentException("mixColor cannot be null");
}
this.mixColor = mixColor;
if (mixValue < 0.0f) {
mixValue = 0.0f;
} else if (mixValue > 1.0f) {
mixValue = 1.0f;
}
this.mixValue = mixValue;
}
/**
* Returns the mix value of this filter.
* * @return the mix value, between 0.0 and 1.0 */ public float getMixValue() { return mixValue; } /** *Returns the solid mix color of this filter.
* * @return the solid color used for mixing */ public Color getMixColor() { return mixColor; } /** * {@inheritDoc} */ @Override public BufferedImage filter(BufferedImage src, BufferedImage dst) { if (dst == null) { dst = createCompatibleDestImage(src, null); } int width = src.getWidth(); int height = src.getHeight(); int[] pixels = new int[width * height]; GraphicsUtilities.getPixels(src, 0, 0, width, height, pixels); mixColor(pixels); GraphicsUtilities.setPixels(dst, 0, 0, width, height, pixels); return dst; } private void mixColor(int[] pixels) { int mix_a = mixColor.getAlpha(); int mix_r = mixColor.getRed(); int mix_g = mixColor.getBlue(); int mix_b = mixColor.getGreen(); for (int i = 0; i < pixels.length; i++) { int argb = pixels[i]; int a = (argb >> 24) & 0xFF; int r = (argb >> 16) & 0xFF; int g = (argb >> 8) & 0xFF; int b = (argb ) & 0xFF; a = (int) (a * (1.0f - mixValue) + mix_a * mixValue); r = (int) (r * (1.0f - mixValue) + mix_r * mixValue); g = (int) (g * (1.0f - mixValue) + mix_g * mixValue); b = (int) (b * (1.0f - mixValue) + mix_b * mixValue); pixels[i] = a << 24 | r << 16 | g << 8 | b; } } } /* * Copyright (c) 2007, Romain Guy * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of the TimingFramework project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ class DoneStepPanel extends JPanel { public DoneStepPanel() { setLayout(new GridBagLayout()); setBackground(Color.BLACK); setOpaque(false); JLabel label = new JLabel("Done"); label.setFont(new Font("Helvetica", Font.BOLD, 64)); // NON-NLS label.setForeground(Color.WHITE); add(label, new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0)); } } /* * Copyright (c) 2007, Romain Guy * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of the TimingFramework project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ class DragAndDropStepPanel extends JComponent { private BufferedImage dropHere; public DragAndDropStepPanel() { setBackground(Color.BLACK); setOpaque(false); loadSupportImage(); } private void loadSupportImage() { try { dropHere = GraphicsUtilities.loadCompatibleImage( getClass().getResource("drop-here.png")); // NON-NLS //dropHere = Reflection.createReflection(dropHere); } catch (IOException e) { e.printStackTrace(); } } @Override protected void paintComponent(Graphics g) { //Rectangle clip = g.getClipBounds(); //g.setColor(getBackground()); //g.fillRect(clip.x, clip.y, clip.width, clip.height); int x = (getWidth() - dropHere.getWidth()) / 2; int y = (getHeight() - dropHere.getHeight()) / 2; g.drawImage(dropHere, x, y, null); } } /* * $Id: FastBlurFilter.java,v 1.1 2007/01/15 16:12:02 gfx Exp $ * * Dual-licensed under LGPL (Sun and Romain Guy) and BSD (Romain Guy). * * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * Copyright (c) 2006 Romain GuyCreates a new blur filter with a default radius of 3.
*/ public GaussianBlurFilter() { this(3); } /** *Creates a new blur filter with the specified radius. If the radius * is lower than 0, a radius of 0.1 will be used automatically.
* * @param radius the radius, in pixels, of the blur */ public GaussianBlurFilter(int radius) { if (radius < 1) { radius = 1; } this.radius = radius; } /** *Returns the radius used by this filter, in pixels.
* * @return the radius of the blur */ public int getRadius() { return radius; } /** * {@inheritDoc} */ @Override public BufferedImage filter(BufferedImage src, BufferedImage dst) { int width = src.getWidth(); int height = src.getHeight(); if (dst == null) { dst = createCompatibleDestImage(src, null); } int[] srcPixels = new int[width * height]; int[] dstPixels = new int[width * height]; float[] kernel = createGaussianKernel(radius); GraphicsUtilities.getPixels(src, 0, 0, width, height, srcPixels); // horizontal pass blur(srcPixels, dstPixels, width, height, kernel, radius); // vertical pass //noinspection SuspiciousNameCombination blur(dstPixels, srcPixels, height, width, kernel, radius); // the result is now stored in srcPixels due to the 2nd pass GraphicsUtilities.setPixels(dst, 0, 0, width, height, srcPixels); return dst; } /** *Blurs the source pixels into the destination pixels. The force of * the blur is specified by the radius which must be greater than 0.
*The source and destination pixels arrays are expected to be in the * INT_ARGB format.
*After this method is executed, dstPixels contains a transposed and * filtered copy of srcPixels.
* * @param srcPixels the source pixels * @param dstPixels the destination pixels * @param width the width of the source picture * @param height the height of the source picture * @param kernel the kernel of the blur effect * @param radius the radius of the blur effect */ static void blur(int[] srcPixels, int[] dstPixels, int width, int height, float[] kernel, int radius) { float a; float r; float g; float b; int ca; int cr; int cg; int cb; for (int y = 0; y < height; y++) { int index = y; int offset = y * width; for (int x = 0; x < width; x++) { a = r = g = b = 0.0f; for (int i = -radius; i <= radius; i++) { int subOffset = x + i; if (subOffset < 0 || subOffset >= width) { subOffset = (x + width) % width; } int pixel = srcPixels[offset + subOffset]; float blurFactor = kernel[radius + i]; a += blurFactor * ((pixel >> 24) & 0xFF); r += blurFactor * ((pixel >> 16) & 0xFF); g += blurFactor * ((pixel >> 8) & 0xFF); b += blurFactor * ((pixel ) & 0xFF); } ca = (int) (a + 0.5f); cr = (int) (r + 0.5f); cg = (int) (g + 0.5f); cb = (int) (b + 0.5f); dstPixels[index] = ((ca > 255 ? 255 : ca) << 24) | ((cr > 255 ? 255 : cr) << 16) | ((cg > 255 ? 255 : cg) << 8) | (cb > 255 ? 255 : cb); index += height; } } } static float[] createGaussianKernel(int radius) { if (radius < 1) { throw new IllegalArgumentException("Radius must be >= 1"); } float[] data = new float[radius * 2 + 1]; float sigma = radius / 3.0f; float twoSigmaSquare = 2.0f * sigma * sigma; float sigmaRoot = (float) Math.sqrt(twoSigmaSquare * Math.PI); float total = 0.0f; for (int i = -radius; i <= radius; i++) { float distance = i * i; int index = i + radius; data[index] = (float) Math.exp(-distance / twoSigmaSquare) / sigmaRoot; total += data[index]; } for (int i = 0; i < data.length; i++) { data[i] /= total; } return data; } } /* * Copyright (c) 2007, Romain Guy * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above * copyright notice, this list of conditions and the following * disclaimer in the documentation and/or other materials provided * with the distribution. * * Neither the name of the TimingFramework project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ class ImageTransferHandler extends TransferHandler { @Override public boolean canImport(TransferSupport support) { if (!support.isDrop()) { return false; } if (!support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { return false; } boolean copySupported = (COPY & support.getSourceDropActions()) == COPY; if (copySupported) { support.setDropAction(COPY); return true; } return false; } @Override public boolean importData(TransferSupport support) { if (!support.isDrop()) { return false; } if (!support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) { return false; } Transferable t = support.getTransferable(); try { Object data = t.getTransferData(DataFlavor.javaFileListFlavor); final File file = ((ListGraphicsUtilities
contains a set of tools to perform
* common graphics operations easily. These operations are divided into
* several themes, listed below.
Compatible images can, and should, be used to increase drawing * performance. This class provides a number of methods to load compatible * images directly from files or to convert existing images to compatibles * images.
*This class provides a number of methods to easily scale down images. * Some of these methods offer a trade-off between speed and result quality and * shouuld be used all the time. They also offer the advantage of producing * compatible images, thus automatically resulting into better runtime * performance.
*All these methodes are both faster than
* {@link java.awt.Image#getScaledInstance(int, int, int)} and produce
* better-looking results than the various drawImage()
methods
* in {@link java.awt.Graphics}, which can be used for image scaling.
This class provides two methods to get and set pixels in a buffered image. * These methods try to avoid unmanaging the image in order to keep good * performance.
* * @author Romain GuyReturns a new BufferedImage
using the same color model
* as the image passed as a parameter. The returned image is only compatible
* with the image passed as a parameter. This does not mean the returned
* image is compatible with the hardware.
BufferedImage
, compatible with the color model
* of image
*/
public static BufferedImage createColorModelCompatibleImage(BufferedImage image) {
ColorModel cm = image.getColorModel();
return new BufferedImage(cm,
cm.createCompatibleWritableRaster(image.getWidth(),
image.getHeight()),
cm.isAlphaPremultiplied(), null);
}
/**
* Returns a new compatible image with the same width, height and * transparency as the image specified as a parameter.
* * @see java.awt.Transparency * @see #createCompatibleImage(int, int) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createTranslucentCompatibleImage(int, int) * @see #loadCompatibleImage(java.net.URL) * @see #toCompatibleImage(java.awt.image.BufferedImage) * @param image the reference image from which the dimension and the * transparency of the new image are obtained * @return a new compatibleBufferedImage
with the same
* dimension and transparency as image
*/
public static BufferedImage createCompatibleImage(BufferedImage image) {
return createCompatibleImage(image, image.getWidth(), image.getHeight());
}
/**
* Returns a new compatible image of the specified width and height, and * the same transparency setting as the image specified as a parameter.
* * @see java.awt.Transparency * @see #createCompatibleImage(java.awt.image.BufferedImage) * @see #createCompatibleImage(int, int) * @see #createTranslucentCompatibleImage(int, int) * @see #loadCompatibleImage(java.net.URL) * @see #toCompatibleImage(java.awt.image.BufferedImage) * @param width the width of the new image * @param height the height of the new image * @param image the reference image from which the transparency of the new * image is obtained * @return a new compatibleBufferedImage
with the same
* transparency as image
and the specified dimension
*/
public static BufferedImage createCompatibleImage(BufferedImage image,
int width, int height) {
return CONFIGURATION.createCompatibleImage(width, height,
image.getTransparency());
}
/**
* Returns a new opaque compatible image of the specified width and * height.
* * @see #createCompatibleImage(java.awt.image.BufferedImage) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createTranslucentCompatibleImage(int, int) * @see #loadCompatibleImage(java.net.URL) * @see #toCompatibleImage(java.awt.image.BufferedImage) * @param width the width of the new image * @param height the height of the new image * @return a new opaque compatibleBufferedImage
of the
* specified width and height
*/
public static BufferedImage createCompatibleImage(int width, int height) {
return CONFIGURATION.createCompatibleImage(width, height);
}
/**
* Returns a new translucent compatible image of the specified width * and height.
* * @see #createCompatibleImage(java.awt.image.BufferedImage) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createCompatibleImage(int, int) * @see #loadCompatibleImage(java.net.URL) * @see #toCompatibleImage(java.awt.image.BufferedImage) * @param width the width of the new image * @param height the height of the new image * @return a new translucent compatibleBufferedImage
of the
* specified width and height
*/
public static BufferedImage createTranslucentCompatibleImage(int width,
int height) {
return CONFIGURATION.createCompatibleImage(width, height,
Transparency.TRANSLUCENT);
}
/**
* Returns a new compatible image from a URL. The image is loaded from the * specified location and then turned, if necessary into a compatible * image.
* * @see #createCompatibleImage(java.awt.image.BufferedImage) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createCompatibleImage(int, int) * @see #createTranslucentCompatibleImage(int, int) * @see #toCompatibleImage(java.awt.image.BufferedImage) * @param resource the URL of the picture to load as a compatible image * @return a new translucent compatibleBufferedImage
of the
* specified width and height
* @throws java.io.IOException if the image cannot be read or loaded
*/
public static BufferedImage loadCompatibleImage(URL resource)
throws IOException {
BufferedImage image = ImageIO.read(resource);
return toCompatibleImage(image);
}
/**
* Return a new compatible image that contains a copy of the specified * image. This method ensures an image is compatible with the hardware, * and therefore optimized for fast blitting operations.
* * @see #createCompatibleImage(java.awt.image.BufferedImage) * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int) * @see #createCompatibleImage(int, int) * @see #createTranslucentCompatibleImage(int, int) * @see #loadCompatibleImage(java.net.URL) * @param image the image to copy into a new compatible image * @return a new compatible copy, with the * same width and height and transparency and content, ofimage
*/
public static BufferedImage toCompatibleImage(BufferedImage image) {
if (image.getColorModel().equals(CONFIGURATION.getColorModel())) {
return image;
}
BufferedImage compatibleImage = CONFIGURATION.createCompatibleImage(
image.getWidth(), image.getHeight(), image.getTransparency());
Graphics g = compatibleImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return compatibleImage;
}
/**
* Returns a thumbnail of a source image. newSize
defines
* the length of the longest dimension of the thumbnail. The other
* dimension is then computed according to the dimensions ratio of the
* original picture.
This method favors speed over quality. When the new size is less than * half the longest dimension of the source image, * {@link #createThumbnail(BufferedImage, int)} or * {@link #createThumbnail(BufferedImage, int, int)} should be used instead * to ensure the quality of the result without sacrificing too much * performance.
* * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image the source image * @param newSize the length of the largest dimension of the thumbnail * @return a new compatibleBufferedImage
containing a
* thumbnail of image
* @throws IllegalArgumentException if newSize
is larger than
* the largest dimension of image
or <= 0
*/
public static BufferedImage createThumbnailFast(BufferedImage image,
int newSize) {
float ratio;
int width = image.getWidth();
int height = image.getHeight();
if (width > height) {
if (newSize >= width) {
throw new IllegalArgumentException("newSize must be lower than" +
" the image width");
} else if (newSize <= 0) {
throw new IllegalArgumentException("newSize must" +
" be greater than 0");
}
ratio = (float) width / (float) height;
width = newSize;
height = (int) (newSize / ratio);
} else {
if (newSize >= height) {
throw new IllegalArgumentException("newSize must be lower than" +
" the image height");
} else if (newSize <= 0) {
throw new IllegalArgumentException("newSize must" +
" be greater than 0");
}
ratio = (float) height / (float) width;
height = newSize;
width = (int) (newSize / ratio);
}
BufferedImage temp = createCompatibleImage(image, width, height);
Graphics2D g2 = temp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
g2.dispose();
return temp;
}
/**
* Returns a thumbnail of a source image.
*This method favors speed over quality. When the new size is less than * half the longest dimension of the source image, * {@link #createThumbnail(BufferedImage, int)} or * {@link #createThumbnail(BufferedImage, int, int)} should be used instead * to ensure the quality of the result without sacrificing too much * performance.
* * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image the source image * @param newWidth the width of the thumbnail * @param newHeight the height of the thumbnail * @return a new compatibleBufferedImage
containing a
* thumbnail of image
* @throws IllegalArgumentException if newWidth
is larger than
* the width of image
or if code>newHeight is larger
* than the height of image
or if one of the dimensions
* is <= 0
*/
public static BufferedImage createThumbnailFast(BufferedImage image,
int newWidth, int newHeight) {
if (newWidth >= image.getWidth() ||
newHeight >= image.getHeight()) {
throw new IllegalArgumentException("newWidth and newHeight cannot" +
" be greater than the image" +
" dimensions");
} else if (newWidth <= 0 || newHeight <= 0) {
throw new IllegalArgumentException("newWidth and newHeight must" +
" be greater than 0");
}
BufferedImage temp = createCompatibleImage(image, newWidth, newHeight);
Graphics2D g2 = temp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(image, 0, 0, temp.getWidth(), temp.getHeight(), null);
g2.dispose();
return temp;
}
/**
* Returns a thumbnail of a source image. newSize
defines
* the length of the longest dimension of the thumbnail. The other
* dimension is then computed according to the dimensions ratio of the
* original picture.
This method offers a good trade-off between speed and quality. * The result looks better than * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when * the new size is less than half the longest dimension of the source * image, yet the rendering speed is almost similar.
* * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnail(java.awt.image.BufferedImage, int, int) * @param image the source image * @param newSize the length of the largest dimension of the thumbnail * @return a new compatibleBufferedImage
containing a
* thumbnail of image
* @throws IllegalArgumentException if newSize
is larger than
* the largest dimension of image
or <= 0
*/
public static BufferedImage createThumbnail(BufferedImage image,
int newSize) {
int width = image.getWidth();
int height = image.getHeight();
boolean isWidthGreater = width > height;
if (isWidthGreater) {
if (newSize >= width) {
throw new IllegalArgumentException("newSize must be lower than" +
" the image width");
}
} else if (newSize >= height) {
throw new IllegalArgumentException("newSize must be lower than" +
" the image height");
}
if (newSize <= 0) {
throw new IllegalArgumentException("newSize must" +
" be greater than 0");
}
float ratioWH = (float) width / (float) height;
float ratioHW = (float) height / (float) width;
BufferedImage thumb = image;
do {
if (isWidthGreater) {
width /= 2;
if (width < newSize) {
width = newSize;
}
height = (int) (width / ratioWH);
} else {
height /= 2;
if (height < newSize) {
height = newSize;
}
width = (int) (height / ratioHW);
}
BufferedImage temp = createCompatibleImage(image, width, height);
Graphics2D g2 = temp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
g2.dispose();
thumb = temp;
} while (newSize != (isWidthGreater ? width : height));
return thumb;
}
/**
* Returns a thumbnail of a source image.
*This method offers a good trade-off between speed and quality. * The result looks better than * {@link #createThumbnailFast(java.awt.image.BufferedImage, int)} when * the new size is less than half the longest dimension of the source * image, yet the rendering speed is almost similar.
* * @see #createThumbnailFast(java.awt.image.BufferedImage, int) * @see #createThumbnailFast(java.awt.image.BufferedImage, int, int) * @see #createThumbnail(java.awt.image.BufferedImage, int) * @param image the source image * @param newWidth the width of the thumbnail * @param newHeight the height of the thumbnail * @return a new compatibleBufferedImage
containing a
* thumbnail of image
* @throws IllegalArgumentException if newWidth
is larger than
* the width of image
or if code>newHeight is larger
* than the height of image or if one the dimensions is not > 0
*/
public static BufferedImage createThumbnail(BufferedImage image,
int newWidth, int newHeight) {
int width = image.getWidth();
int height = image.getHeight();
if (newWidth >= width || newHeight >= height) {
throw new IllegalArgumentException("newWidth and newHeight cannot" +
" be greater than the image" +
" dimensions");
} else if (newWidth <= 0 || newHeight <= 0) {
throw new IllegalArgumentException("newWidth and newHeight must" +
" be greater than 0");
}
BufferedImage thumb = image;
do {
if (width > newWidth) {
width /= 2;
if (width < newWidth) {
width = newWidth;
}
}
if (height > newHeight) {
height /= 2;
if (height < newHeight) {
height = newHeight;
}
}
BufferedImage temp = createCompatibleImage(image, width, height);
Graphics2D g2 = temp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(thumb, 0, 0, temp.getWidth(), temp.getHeight(), null);
g2.dispose();
thumb = temp;
} while (width != newWidth || height != newHeight);
return thumb;
}
/**
* Returns an array of pixels, stored as integers, from a
* BufferedImage
. The pixels are grabbed from a rectangular
* area defined by a location and two dimensions. Calling this method on
* an image of type different from BufferedImage.TYPE_INT_ARGB
* and BufferedImage.TYPE_INT_RGB
will unmanage the image.
pixels
if non-null, a new array of integers
* otherwise
* @throws IllegalArgumentException is pixels
is non-null and
* of length < w*h
*/
public static int[] getPixels(BufferedImage img,
int x, int y, int w, int h, int[] pixels) {
if (w == 0 || h == 0) {
return new int[0];
}
if (pixels == null) {
pixels = new int[w * h];
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length" +
" >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_INT_ARGB ||
imageType == BufferedImage.TYPE_INT_RGB) {
Raster raster = img.getRaster();
return (int[]) raster.getDataElements(x, y, w, h, pixels);
}
// Unmanages the image
return img.getRGB(x, y, w, h, pixels, 0, w);
}
/**
* Writes a rectangular area of pixels in the destination
* BufferedImage
. Calling this method on
* an image of type different from BufferedImage.TYPE_INT_ARGB
* and BufferedImage.TYPE_INT_RGB
will unmanage the image.
pixels
is non-null and
* of length < w*h
*/
public static void setPixels(BufferedImage img,
int x, int y, int w, int h, int[] pixels) {
if (pixels == null || w == 0 || h == 0) {
return;
} else if (pixels.length < w * h) {
throw new IllegalArgumentException("pixels array must have a length" +
" >= w*h");
}
int imageType = img.getType();
if (imageType == BufferedImage.TYPE_INT_ARGB ||
imageType == BufferedImage.TYPE_INT_RGB) {
WritableRaster raster = img.getRaster();
raster.setDataElements(x, y, w, h, pixels);
} else {
// Unmanages the image
img.setRGB(x, y, w, h, pixels, 0, w);
}
}
}
/*
* Copyright (c) 2007, Romain Guy
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the TimingFramework project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
class SizeStepPanel extends JPanel {
private BufferedImage small;
private BufferedImage medium;
private BufferedImage large;
private boolean isLoaded;
private int smallHeight;
private int mediumHeight;
private int largeHeight;
private Rectangle largeImageBounds;
private Rectangle mediumImageBounds;
private Rectangle smallImageBounds;
public SizeStepPanel() {
setBackground(Color.BLACK);
setOpaque(false);
createThumbnails();
addMouseMotionListener(new CursorChanger());
addMouseListener(new SizeSelector());
}
private void createThumbnails() {
Thread loader = new Thread(new Runnable() {
public void run() {
final MainFrame mainFrame = Application.getMainFrame();
BufferedImage image = mainFrame.getImage();
small = GraphicsUtilities.createThumbnail(image, 90);
smallHeight = small.getHeight();
small = Reflection.createReflection(small);
medium = GraphicsUtilities.createThumbnail(image, 160);
mediumHeight = medium.getHeight();
medium = Reflection.createReflection(medium);
large = GraphicsUtilities.createThumbnail(image, 240);
largeHeight = large.getHeight();
large = Reflection.createReflection(large);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
mainFrame.hideWaitGlassPane();
}
});
isLoaded = true;
repaint();
}
});
loader.start();
}
public void dispose() {
if (large != null) {
large.flush();
large = null;
}
if (medium != null) {
medium.flush();
medium = null;
}
if (small != null) {
small.flush();
small = null;
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (!isLoaded) {
return;
}
int totalWidth = 24 * 2;
totalWidth += large.getWidth() + medium.getWidth() + small.getWidth();
int x = (getWidth() - totalWidth) / 2;
int y = (getHeight() - large.getHeight()) / 2;
y += 42;
g.drawImage(large, x, y, null);
if (largeImageBounds == null) {
largeImageBounds = new Rectangle(x, y, large.getWidth(), largeHeight);
}
x += large.getWidth() + 24;
y += largeHeight - mediumHeight;
g.drawImage(medium, x, y, null);
if (mediumImageBounds == null) {
mediumImageBounds = new Rectangle(x, y, medium.getWidth(), mediumHeight);
}
x += medium.getWidth() + 24;
y += mediumHeight - smallHeight;
g.drawImage(small, x, y, null);
if (smallImageBounds == null) {
smallImageBounds = new Rectangle(x, y, small.getWidth(), smallHeight);
}
}
private static void saveImage(final BufferedImage image, final File file) {
Thread writer = new Thread(new Runnable() {
public void run() {
try {
ImageIO.write(image, "JPEG", file); // NON-NLS
} catch (IOException e) {
e.printStackTrace();
}
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Application.getMainFrame().showDoneStep();
}
});
}
});
writer.start();
}
private class SizeSelector extends MouseAdapter {
@Override
public void mouseClicked(MouseEvent e) {
Point point = e.getPoint();
for (final Rectangle r : new Rectangle[] { largeImageBounds, mediumImageBounds, smallImageBounds }) {
if (r != null && r.contains(point)) {
Application.getMainFrame().showWaitGlassPane();
Thread sizer = new Thread(new Runnable() {
public void run() {
int width;
if (r == largeImageBounds) {
width = 1024;
} else if (r == mediumImageBounds) {
width = 800;
} else {
width = 640;
}
final BufferedImage toSave =
GraphicsUtilities.createThumbnail(
Application.getMainFrame().getImage(), width);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(new File(
"x",Application
.getMainFrame().getFileName()
)
);
dispose();
if (chooser.showSaveDialog(Application.getMainFrame()) == JFileChooser.APPROVE_OPTION) {
File selectedFile = chooser.getSelectedFile();
if (!selectedFile.getPath().toLowerCase().endsWith(".jpg")) { // NON-NLS
selectedFile = new File(selectedFile.getPath() + ".jpg"); // NON-NLS
}
saveImage(toSave, selectedFile);
} else {
Application.getMainFrame().showDragAndDropStep();
}
}
});
}
});
sizer.start();
}
}
}
}
private class CursorChanger extends MouseAdapter {
@Override
public void mouseMoved(MouseEvent e) {
Point point = e.getPoint();
if ((largeImageBounds != null && largeImageBounds.contains(point)) ||
(mediumImageBounds != null && mediumImageBounds.contains(point)) ||
(smallImageBounds != null && smallImageBounds.contains(point))) {
setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
} else if (!getCursor().equals(Cursor.getDefaultCursor())) {
setCursor(Cursor.getDefaultCursor());
}
}
}
}
/*
* Copyright (c) 2007, Romain Guy
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the TimingFramework project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
//package org.progx.artemis;
//import java.util.ResourceBundle;
//import javax.swing.SwingUtilities;
//import javax.swing.UIManager;
//import javax.swing.UnsupportedLookAndFeelException;
//import org.progx.artemis.ui.MainFrame;
class Application {
private static MainFrame mainFrame = new MainFrame();
private static ResourceBundle bundle;
private Application() {
}
public static MainFrame getMainFrame() {
mainFrame.setVisible(true);
return mainFrame;
}
public static void main(String[] args) {
}
public static synchronized ResourceBundle getResourceBundle() {
if (bundle == null) {
bundle = ResourceBundle.getBundle("org.progx.artemis.messages"); // NON-NLS
}
return bundle;
}
}
Filthy-Rich-Clients-Blur.zip( 199 k)