/** * Returns the reflection of the source image. The appearance of the reflection is defined by the * opacity, the length and the blur properties. * * * <p>The width of the generated image will be augmented when {@link #isBlurEnabled()} is true. * The generated image will have the width of the source image plus twice the effective blur * radius (see {@link #getEffectiveBlurRadius()}). The default blur radius is 1 so the width will * be augmented by 6. You might need to take this into account at drawing time. * * <p>The returned image height depends on the value returned by {@link #getLength()} and {@link * #getEffectiveBlurRadius()}. For instance, if the length is 0.5 (or 50%) and the source image is * 480 pixels high, then the reflection will be 246 (480 * 0.5 + 3 * 2) pixels high. * * <p>The returned image contains <strong>only</strong> the reflection. You will have to append it * to the source image to produce the illusion of a reflective environement. The method {@link * #appendReflection(java.awt.image.BufferedImage)} provides an easy way to create an image * containing both the source and the reflection. * * @param image the source image * @return the reflection of the source image * @see #appendReflection(java.awt.image.BufferedImage) */ public BufferedImage createReflection(BufferedImage image) { if (length == 0.0f) { return GraphicsUtilities.createCompatibleTranslucentImage(1, 1); } int blurOffset = isBlurEnabled() ? stackBlurFilter.getEffectiveRadius() : 0; int height = (int) (image.getHeight() * length); BufferedImage buffer = GraphicsUtilities.createCompatibleTranslucentImage( image.getWidth() + blurOffset * 2, height + blurOffset * 2); Graphics2D g2 = buffer.createGraphics(); try { g2.translate(0, image.getHeight()); g2.scale(1.0, -1.0); g2.drawImage(image, blurOffset, -blurOffset, null); g2.scale(1.0, -1.0); g2.translate(0, -image.getHeight()); g2.setComposite(AlphaComposite.DstIn); g2.setPaint( new GradientPaint( 0.0f, 0.0f, new Color(0.0f, 0.0f, 0.0f, getOpacity()), 0.0f, buffer.getHeight(), new Color(0.0f, 0.0f, 0.0f, 0.0f), true)); g2.fillRect(0, 0, buffer.getWidth(), buffer.getHeight()); } finally { g2.dispose(); } return isBlurEnabled() ? stackBlurFilter.filter(buffer, null) : buffer; }
public static Shape generateShapeFromText(Font font, String string) { BufferedImage img = GraphicsUtilities.createCompatibleTranslucentImage(1, 1); Graphics2D g2 = img.createGraphics(); try { GlyphVector vect = font.createGlyphVector(g2.getFontRenderContext(), string); Shape shape = vect.getOutline(0f, (float) -vect.getVisualBounds().getY()); return shape; } finally { g2.dispose(); } }
/** * Returns the source image and its reflection. The appearance of the reflection is defined by the * opacity, the length and the blur properties. * * * <p>The width of the generated image will be augmented when {@link #isBlurEnabled()} is true. * The generated image will have the width of the source image plus twice the effective blur * radius (see {@link #getEffectiveBlurRadius()}). The default blur radius is 1 so the width will * be augmented by 6. You might need to take this into account at drawing time. * * <p>The returned image height depends on the value returned by {@link #getLength()} and {@link * #getEffectiveBlurRadius()}. For instance, if the length is 0.5 (or 50%) and the source image is * 480 pixels high, then the reflection will be 246 (480 * 0.5 + 3 * 2) pixels high. * * <p>You can create only the reflection by calling {@link * #createReflection(java.awt.image.BufferedImage)}. * * @param image the source image * @return the source image with its reflection below * @see #createReflection(java.awt.image.BufferedImage) */ public BufferedImage appendReflection(BufferedImage image) { BufferedImage reflection = createReflection(image); BufferedImage buffer = GraphicsUtilities.createCompatibleTranslucentImage( reflection.getWidth(), image.getHeight() + reflection.getHeight()); Graphics2D g2 = buffer.createGraphics(); try { int effectiveRadius = isBlurEnabled() ? stackBlurFilter.getEffectiveRadius() : 0; g2.drawImage(image, effectiveRadius, 0, null); g2.drawImage(reflection, 0, image.getHeight() - effectiveRadius, null); } finally { g2.dispose(); } reflection.flush(); return buffer; }