Java Code Examples for com.alee.utils.ImageUtils#createCompatibleImage()

The following examples show how to use com.alee.utils.ImageUtils#createCompatibleImage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AlphaLayerBackground.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns new {@link BufferedImage} containg alpha background texture of the specified size and colors.
 *
 * @param size       {@link Dimension} of a single cell, texture size will be double of this
 * @param darkColor  dark cells {@link Color}
 * @param lightColor light cells {@link Color}
 * @return new {@link BufferedImage} containg alpha background texture of the specified size and colors
 */
@NotNull
public static BufferedImage createAlphaBackgroundTexture ( @NotNull final Dimension size, @NotNull final Color darkColor,
                                                           @NotNull final Color lightColor )
{
    final BufferedImage image = ImageUtils.createCompatibleImage ( size.width * 2, size.height * 2, Transparency.OPAQUE );
    final Graphics2D g2d = image.createGraphics ();

    g2d.setPaint ( darkColor );
    g2d.fillRect ( 0, 0, size.width, size.height );
    g2d.fillRect ( size.width, size.height, size.width, size.height );

    g2d.setPaint ( lightColor );
    g2d.fillRect ( size.width, 0, size.width, size.height );
    g2d.fillRect ( 0, size.height, size.width, size.height );

    g2d.dispose ();
    return image;
}
 
Example 2
Source File: WebImageDrop.java    From weblaf with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Updates image preview.
 */
protected void updatePreview ()
{
    if ( image != null )
    {
        // Creating image preview
        image = ImageUtils.createImageThumbnail ( actualImage, width, height );

        // Restore decoration
        final BufferedImage f = ImageUtils.createCompatibleImage ( image.getWidth (), image.getHeight (), Transparency.TRANSLUCENT );
        final Graphics2D g2d = f.createGraphics ();
        GraphicsUtils.setupAntialias ( g2d );
        g2d.setPaint ( Color.WHITE );
        g2d.fillRoundRect ( 0, 0, image.getWidth (), image.getHeight (), round * 2, round * 2 );
        g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
        g2d.drawImage ( image, 0, 0, null );
        g2d.dispose ();

        image.flush ();
        image = f;
    }
}
 
Example 3
Source File: SimpleDragViewHandler.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
@NotNull
@Override
public BufferedImage getView ( @NotNull final T object, @NotNull final DragSourceDragEvent event )
{
    final Icon icon = getIcon ( object );
    final Color foreground = getForeground ( object );
    final String title = LM.get ( getText ( object ) );

    final FontMetrics fm = getFontMetrics ( object );
    final int tm = margin.left + ( icon != null ? icon.getIconWidth () + 4 : textSideSpacing );
    final int em = margin.right + textSideSpacing;
    final int w = tm + fm.stringWidth ( title ) + em;
    final int h = margin.top + Math.max ( icon != null ? icon.getIconHeight () : 0, fm.getHeight () ) + margin.bottom;

    final BufferedImage image = ImageUtils.createCompatibleImage ( w, h, Transparency.TRANSLUCENT );
    final Graphics2D g2d = image.createGraphics ();
    GraphicsUtils.setupAlphaComposite ( g2d, 0.8f );
    GraphicsUtils.setupFont ( g2d, fm.getFont () );
    SwingUtils.setupTextAntialias ( g2d, TextRasterization.subpixel );
    g2d.setPaint ( Color.WHITE );
    g2d.fillRect ( 0, 0, w, h );
    g2d.setPaint ( Color.LIGHT_GRAY );
    g2d.drawRect ( 0, 0, w - 1, h - 1 );
    if ( icon != null )
    {
        icon.paintIcon ( null, g2d, margin.left, margin.top );
    }
    g2d.setPaint ( foreground != null ? foreground : Color.BLACK );
    g2d.drawString ( title, tm, margin.top + ( h - margin.top - margin.bottom ) / 2 + LafUtils.getTextCenterShiftY ( fm ) );
    g2d.dispose ();
    return image;
}
 
Example 4
Source File: WebShadow.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns shadow image based on provided shape.
 *
 * @param bounds  shadow bounds
 * @param width   shadow width
 * @param opacity shadow opacity
 * @param color   shadow color
 * @param shape   shadow shape
 * @return shadow image based on provided shape
 */
@NotNull
public static BufferedImage createShadowImage ( @NotNull final Rectangle bounds, final int width, final float opacity,
                                                @NotNull final Color color, @NotNull final Shape shape )
{
    // Creating template image
    final BufferedImage bi = ImageUtils.createCompatibleImage ( bounds.width, bounds.height, Transparency.TRANSLUCENT );
    final Graphics2D ig = bi.createGraphics ();
    GraphicsUtils.setupAntialias ( ig );
    ig.translate ( -bounds.x, -bounds.y );
    ig.setPaint ( Color.BLACK );
    ig.fill ( shape );
    ig.dispose ();

    // Creating shadow image
    final ShadowFilter sf = new ShadowFilter ( width, 0, 0, opacity );
    sf.setShadowColor ( Color.BLACK.getRGB () );
    final BufferedImage shadow = sf.filter ( bi, null );

    // Clipping shadow image
    final Graphics2D g2d = shadow.createGraphics ();
    GraphicsUtils.setupAntialias ( g2d );
    g2d.translate ( -bounds.x, -bounds.y );
    g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
    g2d.setPaint ( ColorUtils.transparent () );
    g2d.fill ( shape );
    g2d.setPaint ( color );
    g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
    g2d.fillRect ( 0, 0, bounds.width, bounds.height );
    g2d.dispose ();

    return shadow;
}
 
Example 5
Source File: WebShadow.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns inner shadow image based on provided shape.
 *
 * @param bounds  shadow bounds
 * @param width   shadow width
 * @param opacity shadow opacity
 * @param color   shadow color
 * @param shape   shadow shape
 * @return inner shadow image based on provided shape
 */
@NotNull
public static BufferedImage createInnerShadowImage ( @NotNull final Rectangle bounds, final int width, final float opacity,
                                                     @NotNull final Color color, @NotNull final Shape shape )
{
    final Rectangle b = new Rectangle ( bounds.x - width * 2, bounds.y - width * 2,
            bounds.width + width * 4, bounds.height + width * 4 );

    // Creating template image
    final BufferedImage bi = ImageUtils.createCompatibleImage ( b.width, b.height, Transparency.TRANSLUCENT );
    final Graphics2D ig = bi.createGraphics ();
    GraphicsUtils.setupAntialias ( ig );
    ig.translate ( -b.x, -b.y );
    final Area area = new Area ( new Rectangle ( b.x, b.y, b.width, b.height ) );
    area.exclusiveOr ( new Area ( shape ) );
    ig.setPaint ( Color.BLACK );
    ig.fill ( area );
    ig.dispose ();

    // Creating inner shadow image
    final ShadowFilter sf = new ShadowFilter ( width, 0, 0, opacity );
    sf.setShadowColor ( Color.BLACK.getRGB () );
    final BufferedImage shadow = sf.filter ( bi, null );

    // Clipping inner shadow image
    final Graphics2D g2d = shadow.createGraphics ();
    GraphicsUtils.setupAntialias ( g2d );
    g2d.translate ( -b.x, -b.y );
    g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
    g2d.setPaint ( ColorUtils.transparent () );
    g2d.fill ( area );
    g2d.setPaint ( color );
    g2d.setComposite ( AlphaComposite.getInstance ( AlphaComposite.SRC_IN ) );
    g2d.fillRect ( 0, 0, b.width, b.height );
    g2d.dispose ();

    return shadow.getSubimage ( width * 2, width * 2, b.width - width * 4, b.height - width * 4 );
}
 
Example 6
Source File: PaletteColorChooser.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
private Cursor createLoopCursor ()
{
    final Dimension dimension = Toolkit.getDefaultToolkit ().getBestCursorSize ( 14, 14 );

    final BufferedImage bufferedImage =
            ImageUtils.createCompatibleImage ( dimension.width, dimension.height, Transparency.TRANSLUCENT );

    final Graphics2D g2d = bufferedImage.createGraphics ();
    g2d.drawImage ( LOOP_ICON.getImage (), 0, 0, LOOP_ICON.getImageObserver () );
    g2d.dispose ();

    return Toolkit.getDefaultToolkit ().createCustomCursor ( bufferedImage, new Point ( 7, 7 ), "Loop Cursor" );
}
 
Example 7
Source File: NinePatchIcon.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Constructs new NinePatchIcon using the specified nine-patch image.
 *
 * @param image        {@link BufferedImage}
 * @param parsePatches whether or not information about patches should be parsed from the image
 */
public NinePatchIcon ( @NotNull final BufferedImage image, final boolean parsePatches )
{
    // Parsing patches or creating new 9-patch icon
    if ( parsePatches )
    {
        // Incorrect image
        if ( image.getWidth () < 3 || image.getHeight () < 3 )
        {
            throw new IllegalArgumentException ( "Buffered image must be atleast 3x3 pixels size" );
        }

        // Creating actual image in a compatible format
        final int w = image.getWidth () - 2;
        final int h = image.getHeight () - 2;
        rawImage = ImageUtils.createCompatibleImage ( w, h, image.getTransparency () );
        final Graphics2D g2d = rawImage.createGraphics ();
        g2d.drawImage ( image, 0, 0, w, h, 1, 1, image.getWidth () - 1, image.getHeight () - 1, null );
        g2d.dispose ();

        // Parsing stretch variables
        horizontalStretch = NinePatchUtils.parseIntervals ( image, NinePatchIntervalType.horizontalStretch );
        verticalStretch = NinePatchUtils.parseIntervals ( image, NinePatchIntervalType.verticalStretch );

        // Incorrect image
        if ( !( ( horizontalStretch.size () > 1 || horizontalStretch.size () == 1 && !horizontalStretch.get ( 0 ).isPixel () ) &&
                ( verticalStretch.size () > 1 || verticalStretch.size () == 1 && !verticalStretch.get ( 0 ).isPixel () ) ) )
        {
            throw new IllegalArgumentException ( "There must be stretch constraints specified on image" );
        }

        // Parsing content margins
        final List<NinePatchInterval> vc = NinePatchUtils.parseIntervals ( image, NinePatchIntervalType.verticalContent );
        final List<NinePatchInterval> hc = NinePatchUtils.parseIntervals ( image, NinePatchIntervalType.horizontalContent );
        final int top = vc.size () == 0 ? 0 : vc.get ( 0 ).getStart ();
        final int bottom = vc.size () == 0 ? 0 : rawImage.getHeight () - vc.get ( 0 ).getEnd () - 1;
        final int left = hc.size () == 0 ? 0 : hc.get ( 0 ).getStart ();
        final int right = hc.size () == 0 ? 0 : rawImage.getWidth () - hc.get ( 0 ).getEnd () - 1;
        margin = new Insets ( top, left, bottom, right );

        // Forcing cached data calculation on initialization
        getFixedPixelsWidth ( true );
        getFixedPixelsWidth ( false );
        getFixedPixelsHeight ( true );
        getFixedPixelsHeight ( false );
    }
    else
    {
        // Actual image
        this.rawImage = image;

        // Stretch variables
        horizontalStretch = new ArrayList<NinePatchInterval> ();
        verticalStretch = new ArrayList<NinePatchInterval> ();

        // Empty margin
        margin = new Insets ( 0, 0, 0, 0 );
    }
}
 
Example 8
Source File: WebDecoratedImage.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
public void updatePreview ()
{
    if ( icon != null )
    {
        // Source image
        BufferedImage image = ImageUtils.copyToBufferedImage ( icon );

        // Applying filters
        if ( grayscale )
        {
            new GrayscaleFilter ().filter ( image, image );
        }
        if ( blur )
        {
            new GaussianFilter ( blurFactor ).filter ( image, image );
        }
        if ( zoomBlur && rotationBlur )
        {
            new MotionBlurFilter ( 0f, 0f, rotationBlurFactor, zoomBlurFactor, blurAlignX, blurAlignY ).filter ( image, image );
        }
        else if ( zoomBlur )
        {
            new MotionBlurFilter ( 0f, 0f, 0f, zoomBlurFactor, blurAlignX, blurAlignY ).filter ( image, image );
        }
        else if ( rotationBlur )
        {
            new MotionBlurFilter ( 0f, 0f, rotationBlurFactor, 0f, blurAlignX, blurAlignY ).filter ( image, image );
        }

        // Applying rounded corners
        if ( round > 0 )
        {
            image = ImageUtils.cutImage (
                    image, new RoundRectangle2D.Double ( 0, 0, icon.getIconWidth (), icon.getIconHeight (), round * 2, round * 2 )
            );
        }

        // Creating additional effects
        if ( shadeWidth > 0 || drawGlassLayer || drawBorder )
        {
            final Dimension ps = getPreferredSize ();
            final BufferedImage img = ImageUtils.createCompatibleImage ( ps.width, ps.height, Transparency.TRANSLUCENT );
            final Graphics2D g2d = img.createGraphics ();
            GraphicsUtils.setupAntialias ( g2d );
            final Shape bs = getBorderShape ();

            // Shade
            GraphicsUtils.drawShade ( g2d, bs, WebDecoratedImageStyle.shadeType, new Color ( 90, 90, 90 ), shadeWidth );

            // Image itself
            g2d.drawImage ( image, shadeWidth, shadeWidth, null );

            // Glass-styled shade
            if ( drawGlassLayer )
            {
                g2d.setPaint ( new GradientPaint ( 0, shadeWidth, new Color ( 255, 255, 255, 160 ), 0,
                        shadeWidth + ( ps.height - shadeWidth * 2 ) / 2, new Color ( 255, 255, 255, 32 ) ) );
                g2d.fill ( getGlanceShape () );
            }

            // Border
            if ( drawBorder )
            {
                g2d.setPaint ( borderColor );
                g2d.draw ( bs );
            }

            g2d.dispose ();
            image = img;
        }

        // Updating preview
        previewIcon = new ImageIcon ( image );
    }
    else
    {
        // No preview available
        previewIcon = null;
    }

    // Updating component view
    repaint ();
}
 
Example 9
Source File: PaletteColorChooser.java    From weblaf with GNU General Public License v3.0 4 votes vote down vote up
public PaletteColorChooser ()
{
    super ();

    paletteColorChooserPaint = new PaletteColorChooserPaint ( 0, 0, 256, 256, sideColor );
    image = ImageUtils.createCompatibleImage ( 256, 256, Transparency.TRANSLUCENT );
    coordinate = new Point ( 2, 2 );
    repaintImage ();

    setLayout ( new TableLayout ( new double[][]{ { TableLayout.PREFERRED }, { 3, TableLayout.PREFERRED, 3 } } ) );

    colorChooser = new JComponent ()
    {
        @Override
        protected void paintComponent ( final Graphics g )
        {
            super.paintComponent ( g );

            final Graphics2D g2d = ( Graphics2D ) g;

            final Shape old = g2d.getClip ();
            final Area clip = new Area ( new Rectangle2D.Double ( 2, 2, getWidth () - 4, getHeight () - 4 ) );
            clip.intersect ( new Area ( old ) );
            g2d.setClip ( clip );

            g2d.drawImage ( image, 2, 2, null );
            g2d.drawImage ( LOOP_ICON.getImage (), coordinate.x - LOOP_ICON.getIconWidth () / 2,
                    coordinate.y - LOOP_ICON.getIconHeight () / 2, LOOP_ICON.getImageObserver () );

            g2d.setClip ( old );
        }
    };
    colorChooser.setBorder ( BorderFactory.createCompoundBorder ( BorderFactory.createLineBorder ( Color.GRAY, 1 ),
            BorderFactory.createLineBorder ( Color.WHITE, 1 ) ) );
    //        colorChooser.setBorder ( BorderFactory
    //                .createBevelBorder ( BevelBorder.LOWERED, Color.WHITE, Color.WHITE,
    //                        new Color ( 160, 160, 160 ), new Color ( 178, 178, 178 ) ) );
    colorChooser.setPreferredSize ( new Dimension ( 260, 260 ) );

    final ColorChooserMouseAdapter adapter = new ColorChooserMouseAdapter ();
    colorChooser.addMouseListener ( adapter );
    colorChooser.addMouseMotionListener ( adapter );

    add ( colorChooser, "0,1" );
}